Re: [R] looping problem

2017-02-02 Thread greg holly
Thanks so much Peter. I do appreciate for this.

Greg

On Thu, Feb 2, 2017 at 10:28 AM, PIKAL Petr <petr.pi...@precheza.cz> wrote:

> Hi.
>
> Your messages are rather confusing. Well, if you could get correct final
> data.frame in loop why not just add inside of loop new column(s) by
>
> psT$chr <- i
>
> Maybe it is time to read R intro as good source for starting with R. It
> has about 100 pages, but you can pick as start only pages 2-40 which are
> basic for data input and manipulation.
>
> Cheers
> Petr
>
>
> > -Original Message-
> > From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of greg
> holly
> > Sent: Thursday, February 2, 2017 4:13 PM
> > To: Rui Barradas <ruipbarra...@sapo.pt>
> > Cc: r-help mailing list <r-help@r-project.org>
> > Subject: Re: [R] looping problem
> >
> > Thanks so much for this. Unfortunately, cbind did not work. Basically, I
> like to
> > put an extra column named "chr" in the combined file from 22 chr.
> > So chr colum will be "1" for the portion of chr1 in the combined file, 2
> for the
> > portion of chr2 in the combined file and so on.
> >
> > Regards,
> >
> > Greg
> >
> > On Thu, Feb 2, 2017 at 9:39 AM, Rui Barradas <ruipbarra...@sapo.pt>
> wrote:
> >
> > > Hello,
> > >
> > > If I understand correctly, just use ?cbind.
> > >
> > > Rui Barradas
> > >
> > > Em 02-02-2017 13:33, greg holly escreveu:
> > >
> > >> Hi Rui;
> > >>
> > >> Is there any way to insert the chr ids in numeric as 1,2..,22 in
> > >> the final output. Here is output from str(temp). So I need also chr
> > >> ids in a column.
> > >> 1  rs58108140 10583 G A -0.070438 0.059903
> > >> 2 rs189107123 10611 C G -0.044916 0.085853
> > >>
> > >> Regards,
> > >> Greg
> > >>
> > >>
> > >> On Wed, Feb 1, 2017 at 1:32 PM, Rui Barradas <ruipbarra...@sapo.pt
> > >> <mailto:ruipbarra...@sapo.pt>> wrote:
> > >>
> > >> Hello,
> > >>
> > >> If what you want is to combine the files into one data.frame then
> > >> there are 2 things you should see:
> > >>
> > >> 1) You create a variable named 'temp' and don't ever use it.
> > >> 2) You never combine the data.frames you read in.
> > >>
> > >> Try instead the following.
> > >>
> > >> temp <- data.frame()
> > >> for(i in 1:22) {
> > >>  infile<-paste("chr",i,"/Z-score.imputed",sep="")
> > >>  psT<-read.table(infile,header=T,as.is <http://as.is
> >=T,sep="\t")
> > >>  temp <- rbind(temp, psT)
> > >> }
> > >>
> > >> str(temp)  # to see what you have
> > >>
> > >> Hope this helps,
> > >>
> > >> Rui Barradas
> > >>
> > >>
> > >>
> > >>
> > >> Em 01-02-2017 17:25, greg holly escreveu:
> > >>
> > >> Hi all;
> > >>
> > >> I have 22 directories named chr1, chr2,,chr22. Each
> > >> directory has a
> > >> file named "Z-score.imputed". I would like to combine
> > >> Z-score.imputed
> > >> files into one. I wrote the following loop but did not get any
> > >> results.
> > >> Your helps are highly appreciated.
> > >>
> > >> regards,
> > >>
> > >> Greg
> > >>
> > >> temp<-c()
> > >>
> > >> for(i in 1:22) {
> > >> infile<-paste("chr",i,"/Z-score.imputed",sep="")
> > >> psT<-read.table(as.character(infile),header=T,as.is
> > >> <http://as.is>=T,sep="\t")
> > >> ps<-psT[psT$Var>0.6,]
> > >> ratio=nrow(ps)/nrow(psT)
> > >> print(ratio)
> > >> }
> > >>
> > >>  [[alternative HTML version deleted]]
> > >>
> > >> __
> > >> R-help@r-project.org <mailto:R-help@r-project.org> mailing
&g

Re: [R] looping problem

2017-02-02 Thread PIKAL Petr
Hi.

Your messages are rather confusing. Well, if you could get correct final 
data.frame in loop why not just add inside of loop new column(s) by

psT$chr <- i

Maybe it is time to read R intro as good source for starting with R. It has 
about 100 pages, but you can pick as start only pages 2-40 which are basic for 
data input and manipulation.

Cheers
Petr


> -Original Message-
> From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of greg holly
> Sent: Thursday, February 2, 2017 4:13 PM
> To: Rui Barradas <ruipbarra...@sapo.pt>
> Cc: r-help mailing list <r-help@r-project.org>
> Subject: Re: [R] looping problem
>
> Thanks so much for this. Unfortunately, cbind did not work. Basically, I like 
> to
> put an extra column named "chr" in the combined file from 22 chr.
> So chr colum will be "1" for the portion of chr1 in the combined file, 2 for 
> the
> portion of chr2 in the combined file and so on.
>
> Regards,
>
> Greg
>
> On Thu, Feb 2, 2017 at 9:39 AM, Rui Barradas <ruipbarra...@sapo.pt> wrote:
>
> > Hello,
> >
> > If I understand correctly, just use ?cbind.
> >
> > Rui Barradas
> >
> > Em 02-02-2017 13:33, greg holly escreveu:
> >
> >> Hi Rui;
> >>
> >> Is there any way to insert the chr ids in numeric as 1,2..,22 in
> >> the final output. Here is output from str(temp). So I need also chr
> >> ids in a column.
> >> 1  rs58108140 10583 G A -0.070438 0.059903
> >> 2 rs189107123 10611 C G -0.044916 0.085853
> >>
> >> Regards,
> >> Greg
> >>
> >>
> >> On Wed, Feb 1, 2017 at 1:32 PM, Rui Barradas <ruipbarra...@sapo.pt
> >> <mailto:ruipbarra...@sapo.pt>> wrote:
> >>
> >> Hello,
> >>
> >> If what you want is to combine the files into one data.frame then
> >> there are 2 things you should see:
> >>
> >> 1) You create a variable named 'temp' and don't ever use it.
> >> 2) You never combine the data.frames you read in.
> >>
> >> Try instead the following.
> >>
> >> temp <- data.frame()
> >> for(i in 1:22) {
> >>  infile<-paste("chr",i,"/Z-score.imputed",sep="")
> >>  psT<-read.table(infile,header=T,as.is <http://as.is>=T,sep="\t")
> >>  temp <- rbind(temp, psT)
> >> }
> >>
> >> str(temp)  # to see what you have
> >>
> >> Hope this helps,
> >>
> >> Rui Barradas
> >>
> >>
> >>
> >>
> >> Em 01-02-2017 17:25, greg holly escreveu:
> >>
> >> Hi all;
> >>
> >> I have 22 directories named chr1, chr2,,chr22. Each
> >> directory has a
> >> file named "Z-score.imputed". I would like to combine
> >> Z-score.imputed
> >> files into one. I wrote the following loop but did not get any
> >> results.
> >> Your helps are highly appreciated.
> >>
> >> regards,
> >>
> >> Greg
> >>
> >> temp<-c()
> >>
> >> for(i in 1:22) {
> >> infile<-paste("chr",i,"/Z-score.imputed",sep="")
> >> psT<-read.table(as.character(infile),header=T,as.is
> >> <http://as.is>=T,sep="\t")
> >> ps<-psT[psT$Var>0.6,]
> >> ratio=nrow(ps)/nrow(psT)
> >> print(ratio)
> >> }
> >>
> >>  [[alternative HTML version deleted]]
> >>
> >> __
> >> R-help@r-project.org <mailto:R-help@r-project.org> mailing list
> >> -- To UNSUBSCRIBE and more, see
> >> https://stat.ethz.ch/mailman/listinfo/r-help
> >> <https://stat.ethz.ch/mailman/listinfo/r-help>
> >> PLEASE do read the posting guide
> >> http://www.R-project.org/posting-guide.html
> >> <http://www.R-project.org/posting-guide.html>
> >> and provide commented, minimal, self-contained, reproducible code.
> >>
> >>
> >>
>
>   [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>

Re: [R] looping problem

2017-02-02 Thread greg holly
Thanks so much for this. Unfortunately, cbind did not work. Basically, I
like to put an extra column named "chr" in the combined file from 22 chr.
So chr colum will be "1" for the portion of chr1 in the combined file, 2
for the portion of chr2 in the combined file and so on.

Regards,

Greg

On Thu, Feb 2, 2017 at 9:39 AM, Rui Barradas  wrote:

> Hello,
>
> If I understand correctly, just use ?cbind.
>
> Rui Barradas
>
> Em 02-02-2017 13:33, greg holly escreveu:
>
>> Hi Rui;
>>
>> Is there any way to insert the chr ids in numeric as 1,2..,22 in the
>> final output. Here is output from str(temp). So I need also chr ids in a
>> column.
>> 1  rs58108140 10583 G A -0.070438 0.059903
>> 2 rs189107123 10611 C G -0.044916 0.085853
>>
>> Regards,
>> Greg
>>
>>
>> On Wed, Feb 1, 2017 at 1:32 PM, Rui Barradas > > wrote:
>>
>> Hello,
>>
>> If what you want is to combine the files into one data.frame then
>> there are 2 things you should see:
>>
>> 1) You create a variable named 'temp' and don't ever use it.
>> 2) You never combine the data.frames you read in.
>>
>> Try instead the following.
>>
>> temp <- data.frame()
>> for(i in 1:22) {
>>  infile<-paste("chr",i,"/Z-score.imputed",sep="")
>>  psT<-read.table(infile,header=T,as.is =T,sep="\t")
>>  temp <- rbind(temp, psT)
>> }
>>
>> str(temp)  # to see what you have
>>
>> Hope this helps,
>>
>> Rui Barradas
>>
>>
>>
>>
>> Em 01-02-2017 17:25, greg holly escreveu:
>>
>> Hi all;
>>
>> I have 22 directories named chr1, chr2,,chr22. Each
>> directory has a
>> file named "Z-score.imputed". I would like to combine
>> Z-score.imputed
>> files into one. I wrote the following loop but did not get any
>> results.
>> Your helps are highly appreciated.
>>
>> regards,
>>
>> Greg
>>
>> temp<-c()
>>
>> for(i in 1:22) {
>> infile<-paste("chr",i,"/Z-score.imputed",sep="")
>> psT<-read.table(as.character(infile),header=T,as.is
>> =T,sep="\t")
>> ps<-psT[psT$Var>0.6,]
>> ratio=nrow(ps)/nrow(psT)
>> print(ratio)
>> }
>>
>>  [[alternative HTML version deleted]]
>>
>> __
>> R-help@r-project.org  mailing list
>> -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> 
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> 
>> and provide commented, minimal, self-contained, reproducible code.
>>
>>
>>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] looping problem

2017-02-02 Thread Rui Barradas

Hello,

If I understand correctly, just use ?cbind.

Rui Barradas

Em 02-02-2017 13:33, greg holly escreveu:

Hi Rui;

Is there any way to insert the chr ids in numeric as 1,2..,22 in the
final output. Here is output from str(temp). So I need also chr ids in a
column.
1  rs58108140 10583 G A -0.070438 0.059903
2 rs189107123 10611 C G -0.044916 0.085853

Regards,
Greg


On Wed, Feb 1, 2017 at 1:32 PM, Rui Barradas > wrote:

Hello,

If what you want is to combine the files into one data.frame then
there are 2 things you should see:

1) You create a variable named 'temp' and don't ever use it.
2) You never combine the data.frames you read in.

Try instead the following.

temp <- data.frame()
for(i in 1:22) {
 infile<-paste("chr",i,"/Z-score.imputed",sep="")
 psT<-read.table(infile,header=T,as.is =T,sep="\t")
 temp <- rbind(temp, psT)
}

str(temp)  # to see what you have

Hope this helps,

Rui Barradas




Em 01-02-2017 17:25, greg holly escreveu:

Hi all;

I have 22 directories named chr1, chr2,,chr22. Each
directory has a
file named "Z-score.imputed". I would like to combine
Z-score.imputed
files into one. I wrote the following loop but did not get any
results.
Your helps are highly appreciated.

regards,

Greg

temp<-c()

for(i in 1:22) {
infile<-paste("chr",i,"/Z-score.imputed",sep="")
psT<-read.table(as.character(infile),header=T,as.is
=T,sep="\t")
ps<-psT[psT$Var>0.6,]
ratio=nrow(ps)/nrow(psT)
print(ratio)
}

 [[alternative HTML version deleted]]

__
R-help@r-project.org  mailing list
-- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help

PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html

and provide commented, minimal, self-contained, reproducible code.




__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] looping problem

2017-02-02 Thread greg holly
Hi Rui;

Is there any way to insert the chr ids in numeric as 1,2..,22 in the
final output. Here is output from str(temp). So I need also chr ids in a
column.
1  rs58108140 10583 G A -0.070438 0.059903
2 rs189107123 10611 C G -0.044916 0.085853

Regards,
Greg


On Wed, Feb 1, 2017 at 1:32 PM, Rui Barradas  wrote:

> Hello,
>
> If what you want is to combine the files into one data.frame then there
> are 2 things you should see:
>
> 1) You create a variable named 'temp' and don't ever use it.
> 2) You never combine the data.frames you read in.
>
> Try instead the following.
>
> temp <- data.frame()
> for(i in 1:22) {
> infile<-paste("chr",i,"/Z-score.imputed",sep="")
> psT<-read.table(infile,header=T,as.is=T,sep="\t")
> temp <- rbind(temp, psT)
> }
>
> str(temp)  # to see what you have
>
> Hope this helps,
>
> Rui Barradas
>
>
>
>
> Em 01-02-2017 17:25, greg holly escreveu:
>
>> Hi all;
>>
>> I have 22 directories named chr1, chr2,,chr22. Each directory has a
>> file named "Z-score.imputed". I would like to combine  Z-score.imputed
>> files into one. I wrote the following loop but did not get any results.
>> Your helps are highly appreciated.
>>
>> regards,
>>
>> Greg
>>
>> temp<-c()
>>
>> for(i in 1:22) {
>> infile<-paste("chr",i,"/Z-score.imputed",sep="")
>> psT<-read.table(as.character(infile),header=T,as.is=T,sep="\t")
>> ps<-psT[psT$Var>0.6,]
>> ratio=nrow(ps)/nrow(psT)
>> print(ratio)
>> }
>>
>> [[alternative HTML version deleted]]
>>
>> __
>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posti
>> ng-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] looping problem

2017-02-01 Thread greg holly
Hi Rui;

I do appreciate for this. Thanks so much. I will try ASAP.

Regards,

Greg

On Wed, Feb 1, 2017 at 1:32 PM, Rui Barradas  wrote:

> Hello,
>
> If what you want is to combine the files into one data.frame then there
> are 2 things you should see:
>
> 1) You create a variable named 'temp' and don't ever use it.
> 2) You never combine the data.frames you read in.
>
> Try instead the following.
>
> temp <- data.frame()
> for(i in 1:22) {
> infile<-paste("chr",i,"/Z-score.imputed",sep="")
> psT<-read.table(infile,header=T,as.is=T,sep="\t")
> temp <- rbind(temp, psT)
> }
>
> str(temp)  # to see what you have
>
> Hope this helps,
>
> Rui Barradas
>
>
>
>
> Em 01-02-2017 17:25, greg holly escreveu:
>
>> Hi all;
>>
>> I have 22 directories named chr1, chr2,,chr22. Each directory has a
>> file named "Z-score.imputed". I would like to combine  Z-score.imputed
>> files into one. I wrote the following loop but did not get any results.
>> Your helps are highly appreciated.
>>
>> regards,
>>
>> Greg
>>
>> temp<-c()
>>
>> for(i in 1:22) {
>> infile<-paste("chr",i,"/Z-score.imputed",sep="")
>> psT<-read.table(as.character(infile),header=T,as.is=T,sep="\t")
>> ps<-psT[psT$Var>0.6,]
>> ratio=nrow(ps)/nrow(psT)
>> print(ratio)
>> }
>>
>> [[alternative HTML version deleted]]
>>
>> __
>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posti
>> ng-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] looping problem

2017-02-01 Thread Rui Barradas

Hello,

If what you want is to combine the files into one data.frame then there 
are 2 things you should see:


1) You create a variable named 'temp' and don't ever use it.
2) You never combine the data.frames you read in.

Try instead the following.

temp <- data.frame()
for(i in 1:22) {
infile<-paste("chr",i,"/Z-score.imputed",sep="")
psT<-read.table(infile,header=T,as.is=T,sep="\t")
temp <- rbind(temp, psT)
}

str(temp)  # to see what you have

Hope this helps,

Rui Barradas



Em 01-02-2017 17:25, greg holly escreveu:

Hi all;

I have 22 directories named chr1, chr2,,chr22. Each directory has a
file named "Z-score.imputed". I would like to combine  Z-score.imputed
files into one. I wrote the following loop but did not get any results.
Your helps are highly appreciated.

regards,

Greg

temp<-c()

for(i in 1:22) {
infile<-paste("chr",i,"/Z-score.imputed",sep="")
psT<-read.table(as.character(infile),header=T,as.is=T,sep="\t")
ps<-psT[psT$Var>0.6,]
ratio=nrow(ps)/nrow(psT)
print(ratio)
}

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] looping problem

2017-02-01 Thread greg holly
Hi all;

I have 22 directories named chr1, chr2,,chr22. Each directory has a
file named "Z-score.imputed". I would like to combine  Z-score.imputed
files into one. I wrote the following loop but did not get any results.
Your helps are highly appreciated.

regards,

Greg

temp<-c()

for(i in 1:22) {
infile<-paste("chr",i,"/Z-score.imputed",sep="")
psT<-read.table(as.character(infile),header=T,as.is=T,sep="\t")
ps<-psT[psT$Var>0.6,]
ratio=nrow(ps)/nrow(psT)
print(ratio)
}

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Looping Problem

2011-03-22 Thread armstrwa
Hi all,

I am trying to write a script that will automate the task of running a
Kendall's Tau correlation test on 75 time series that I am interested in.  

The code I have written is:

for(i in 1:length(gagehandles)){
dates-get(paste(gagehandles[i],_amsd,sep=))
q-get(paste(gagehandles[i],_amsq,sep=))

taup-Kendall(dates,q)
print(gagehandles[i])
print(taup)

So basically, I have inputted all the records I am concerned with into R
using the scan function.  These are all represented by a four letter
identifier followed by _amsd or _amsq (to distinguish the two variables). 
For example, a variable might be hoea_amsd, which has a corresponding
hoea_amsq.

I am trying to write a function that will run through a list of the gage
identifiers ('gagehandles' in the code) and will then runn and Kendall's tau
correlation test on the two variables (suffixes '_amsd' and '_amsq').

It was working when I was using a small subset of my data, but now that I am
trying to look at all 75 series, I am getting the error:

Error in get(paste(gagehandles[i], _amsd, sep = )) : 
  variable names are limited to 256 bytes

It didn't have a problem with the variable names before, so I'm not sure
what's going on now.  Does anyone have an idea?

Thank you very much for your help.

Billy

--
View this message in context: 
http://r.789695.n4.nabble.com/Looping-Problem-tp3397077p3397077.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Looping Problem

2011-03-22 Thread Dieter Menne

armstrwa wrote:
 
 for(i in 1:length(gagehandles)){
 
 dates-get(paste(gagehandles[i],_amsd,sep=))
 Error in get(paste(gagehandles[i], _amsd, sep = )) : 
   variable names are limited to 256 bytes
 
 It didn't have a problem with the variable names before, so I'm not sure
 what's going on now.  Does anyone have an idea?
 
 

Add a print(gagehandes[i]) after the { to find out what was wrong.

Dieter


--
View this message in context: 
http://r.789695.n4.nabble.com/Looping-Problem-tp3397077p3397325.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] LOOping problem with R

2010-08-30 Thread David Winsemius


On Aug 30, 2010, at 1:27 AM, Nam Lethanh wrote:


Dear Guys,

I do converting codes from Fortran into R and got stuck in solving  
LOOPING
procedure with R. In FORTRAN, it is (DO and END DO) for looping in  
the net.

In R, it is (FOR with {   }).


Looking at the results  (namely  the 1's from the fortran code) it  
appears there are some conditions that cause FORTRAN not to enter the  
loop whereas in R the inner loop is always entered. The numeric(0)'s  
occur when you attempt to use a zero as an index. If you have a more  
specific question about what the indices are at times that surprise,  
then maybe you ought to put in more debugging code.




I believe there is something wrong with my coding in R, do hope that  
you can

help me solving following problems.

It seems easy, but results are not the same.

***
WITH R

theta-c(0.08,0.06,0.09,0)
for (i in 1:4){
for (j in 1:4){
a-1.0
for (k in i:(j-1)){
a-a*theta[k]
}
print(a)
}
--
HERE GOES the results
-
numeric(0)
[1] 0.08
[1] 0.0048
[1] 0.000432
numeric(0)
[1] 0.0048
[1] 0.06
[1] 0.0054
numeric(0)
[1] 0.000432
[1] 0.0054
[1] 0.09
numeric(0)
[1] 0
[1] 0
[1] 0
*
IN FORTRAN, the results is totally different.

program calculating_a
implicit none
integer i, j, k
double precision a, theta(4)
theta(1)=0.06; theta(2)=0.08; theta(3)=0.09; theta(4)=0
do i=1, 4
do j=1, 4
a=1
do k=i,j-1
a=a*theta(k)
end do

print*, a

end do
end do
end

Here goes the results with FOTRAN

-
1
0.06
4.79E-003
4.31E-004
1
1
0.08
7.2E-0.03
1
1
1
0.09
1
1
1
1
-



Thank you!



Nam

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] LOOping problem with R

2010-08-30 Thread Mario Valle
When j==1 for loops from i down to zero. 5:0 is valid and means c(5,4,3,2,1,0)
Hope it helps
mario

Nam Lethanh wrote:
 Dear Guys,
 
 I do converting codes from Fortran into R and got stuck in solving LOOPING
 procedure with R. In FORTRAN, it is (DO and END DO) for looping in the net.
 In R, it is (FOR with {   }).
 
 I believe there is something wrong with my coding in R, do hope that you can
 help me solving following problems.
 
 It seems easy, but results are not the same.
 
 ***
 WITH R
 
 theta-c(0.08,0.06,0.09,0)
 for (i in 1:4){
 for (j in 1:4){
 a-1.0
 for (k in i:(j-1)){
 a-a*theta[k]
 }
 print(a)
 }
 --
 HERE GOES the results
 -
 numeric(0)
 [1] 0.08
 [1] 0.0048
 [1] 0.000432
 numeric(0)
 [1] 0.0048
 [1] 0.06
 [1] 0.0054
 numeric(0)
 [1] 0.000432
 [1] 0.0054
 [1] 0.09
 numeric(0)
 [1] 0
 [1] 0
 [1] 0
 *
 IN FORTRAN, the results is totally different.
 
 program calculating_a
 implicit none
 integer i, j, k
 double precision a, theta(4)
 theta(1)=0.06; theta(2)=0.08; theta(3)=0.09; theta(4)=0
 do i=1, 4
 do j=1, 4
 a=1
 do k=i,j-1
 a=a*theta(k)
 end do
 
 print*, a
 
 end do
 end do
 end
 
 Here goes the results with FOTRAN
 
 -
 1
 0.06
 4.79E-003
 4.31E-004
 1
 1
 0.08
 7.2E-0.03
 1
 1
 1
 0.09
 1
 1
 1
 1
 -
 
 
 
 Thank you!
 
 
 
 Nam
 
   [[alternative HTML version deleted]]
 
 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.

-- 
Ing. Mario Valle
Data Analysis and Visualization Group| http://www.cscs.ch/~mvalle
Swiss National Supercomputing Centre (CSCS)  | Tel:  +41 (91) 610.82.60
v. Cantonale Galleria 2, 6928 Manno, Switzerland | Fax:  +41 (91) 610.82.82

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] LOOping problem with R

2010-08-30 Thread Joshua Wiley
Hi,

I would also like to point out that you flipped the first two the
values of theta in the R vs. FORTRAN versions.  This fixes part of
your differences (and makes it easy to see that the differences occur
when j  i, as David and Mario point out).

Josh

On Sun, Aug 29, 2010 at 10:27 PM, Nam Lethanh namkyo...@gmail.com wrote:
 Dear Guys,

 I do converting codes from Fortran into R and got stuck in solving LOOPING
 procedure with R. In FORTRAN, it is (DO and END DO) for looping in the net.
 In R, it is (FOR with {   }).

 I believe there is something wrong with my coding in R, do hope that you can
 help me solving following problems.

 It seems easy, but results are not the same.

 ***
 WITH R

 theta-c(0.08,0.06,0.09,0)
 for (i in 1:4){
 for (j in 1:4){
 a-1.0
 for (k in i:(j-1)){
 a-a*theta[k]
 }
 print(a)
 }
 --
 HERE GOES the results
 -
 numeric(0)
 [1] 0.08
 [1] 0.0048
 [1] 0.000432
 numeric(0)
 [1] 0.0048
 [1] 0.06
 [1] 0.0054
 numeric(0)
 [1] 0.000432
 [1] 0.0054
 [1] 0.09
 numeric(0)
 [1] 0
 [1] 0
 [1] 0
 *
 IN FORTRAN, the results is totally different.

 program calculating_a
 implicit none
 integer i, j, k
 double precision a, theta(4)
 theta(1)=0.06; theta(2)=0.08; theta(3)=0.09; theta(4)=0
 do i=1, 4
 do j=1, 4
 a=1
 do k=i,j-1
 a=a*theta(k)
 end do

 print*, a

 end do
 end do
 end

 Here goes the results with FOTRAN

 -
 1
 0.06
 4.79E-003
 4.31E-004
 1
 1
 0.08
 7.2E-0.03
 1
 1
 1
 0.09
 1
 1
 1
 1
 -



 Thank you!



 Nam

        [[alternative HTML version deleted]]

 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.




-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] LOOping problem with R

2010-08-30 Thread Berend Hasselman

You made a mistake with theta

theta-c(0.08,0.06,0.09,0)

This should be (see the fortran)

theta-c(0.06,0.08,0.09,0)

The innermost loop (for( k in ...) is better written as while loop to take
into account how Fortran handles loops (see the previous replies):

k - i
while( k = j-1 ){
   a-a*theta[k]
   k - k + 1
}

Berend



-- 
View this message in context: 
http://r.789695.n4.nabble.com/LOOping-problem-with-R-tp2399596p2399709.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] LOOping problem with R

2010-08-29 Thread Nam Lethanh
Dear Guys,

I do converting codes from Fortran into R and got stuck in solving LOOPING
procedure with R. In FORTRAN, it is (DO and END DO) for looping in the net.
In R, it is (FOR with {   }).

I believe there is something wrong with my coding in R, do hope that you can
help me solving following problems.

It seems easy, but results are not the same.

***
WITH R

theta-c(0.08,0.06,0.09,0)
for (i in 1:4){
for (j in 1:4){
a-1.0
for (k in i:(j-1)){
a-a*theta[k]
}
print(a)
}
--
HERE GOES the results
-
numeric(0)
[1] 0.08
[1] 0.0048
[1] 0.000432
numeric(0)
[1] 0.0048
[1] 0.06
[1] 0.0054
numeric(0)
[1] 0.000432
[1] 0.0054
[1] 0.09
numeric(0)
[1] 0
[1] 0
[1] 0
*
IN FORTRAN, the results is totally different.

program calculating_a
implicit none
integer i, j, k
double precision a, theta(4)
theta(1)=0.06; theta(2)=0.08; theta(3)=0.09; theta(4)=0
do i=1, 4
do j=1, 4
a=1
do k=i,j-1
a=a*theta(k)
end do

print*, a

end do
end do
end

Here goes the results with FOTRAN

-
1
0.06
4.79E-003
4.31E-004
1
1
0.08
7.2E-0.03
1
1
1
0.09
1
1
1
1
-



Thank you!



Nam

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] looping problem

2010-02-12 Thread Roslina Zakaria
Hi r-users,

 
I have this code below but I don't understand the error message:
 
cumdensity - function(z)
{ alp  - 2.0165;
  rho  - 0.868;
 
# simplified expressions
  a  - alp-0.5
  c1 - sqrt(pi)/(gamma(alp)*(1-rho)^alp)
  c2 - sqrt(rho)/(1-rho)
  t1 - exp(-z/(1-rho))
  t2     - (z/(2*c2))^a
  bes1   - besselI(z*c2,a)
  t1bes1 - t1*bes1
  cden   - c1*t1bes1*t2
  cden
}
 
pdensity - function(z)
{ alp  - 2.0165;  rho  - 0.868;  a - alp-0.5;
  k1   - sqrt(pi)/(gamma(alp)*(1-rho)^alp)
  k2   - sqrt(rho)/(1-rho)
  t1   - exp(-z/(1-rho))
  t2   - (z/(2*k2))^a
  bes1 - besselI(z*k2,a)
  bes2 - besselI(z*k2,alp+0.5)
  pp   - k1*t1*t2*(bes1/(rho-1) + a*bes1/z + k2*bes2 + a*bes1/z)
  pp
}
 
## Newton iteration
newton_gam - function(z)
{ n   - length(z)
  r   - runif(n)
  tol - 1E-6
  cdf - vector(length=n, mode=numeric)
  fprime - vector(length=n, mode=numeric)
  f - vector(length=n, mode=numeric)
 
 ## Newton loop
  for (i in 1:1000)
  { cdf    - cumdensity(z)
    fprime - pdensity(z)
    f  - cdf - r
    # Newton method
    z  - z - f/fprime
 
    if (f  tol) break
   }
  cbind(z,cdf)
}
 
alp  - 2.0165
bt1 - 29.107 ; bt2 - 41.517
r1 - rgamma(10, shape=alp, scale = bt1)
r2 - rgamma(10, shape=alp, scale = bt2)
z - (r1/bt1)+(r2/bt2); sort(z)
 
OUTPUT
  z - (r1/bt1)+(r2/bt2); sort(z)
 [1] 0.9344932 1.3117707 1.4514991 2.2637735 2.2795669 3.0548586 3.4485707 
4.1837583 4.2139719 4.5334232
 
 newton_gam(z)
   z   cdf
 [1,] -25.540473 0.1883006
 [2,]  -2.079104 0.1725552
 [3,]  -2.878791 0.1331209
 [4,] -81.317382 0.1881811
 [5,]   8.395880 0.1376294
 [6,] -20.381830 0.1601000
 [7,]   7.124515 0.1682288
 [8,]  16.099559 0.1755200
 [9,]  -2.635536 0.1218351
[10,]   6.438700 0.1341994
Warning message:
In if (f  tol) break :
  the condition has length  1 and only the first element will be used
 
What does this mean?
 
Thank you for any help given.



  
[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] looping problem

2010-02-12 Thread jim holtman
Exactly what are you expecting the 'if' statement to do?  By
definition, the  'if' only takes a single value and your expression is
probably a vector with many values and the error message is just
saying it will use only the first value to determine what to do.  Are
you trying to check if 'any' or 'all' of the values are out of
tolerance?  If so, check out the 'any' and 'all' functions.

On Fri, Feb 12, 2010 at 6:49 AM, Roslina Zakaria zrosl...@yahoo.com wrote:
 Hi r-users,


 I have this code below but I don't understand the error message:

 cumdensity - function(z)
 { alp  - 2.0165;
   rho  - 0.868;

 # simplified expressions
   a  - alp-0.5
   c1 - sqrt(pi)/(gamma(alp)*(1-rho)^alp)
   c2 - sqrt(rho)/(1-rho)
   t1 - exp(-z/(1-rho))
   t2     - (z/(2*c2))^a
   bes1   - besselI(z*c2,a)
   t1bes1 - t1*bes1
   cden   - c1*t1bes1*t2
   cden
 }

 pdensity - function(z)
 { alp  - 2.0165;  rho  - 0.868;  a - alp-0.5;
   k1   - sqrt(pi)/(gamma(alp)*(1-rho)^alp)
   k2   - sqrt(rho)/(1-rho)
   t1   - exp(-z/(1-rho))
   t2   - (z/(2*k2))^a
   bes1 - besselI(z*k2,a)
   bes2 - besselI(z*k2,alp+0.5)
   pp   - k1*t1*t2*(bes1/(rho-1) + a*bes1/z + k2*bes2 + a*bes1/z)
   pp
 }

 ## Newton iteration
 newton_gam - function(z)
 { n   - length(z)
   r   - runif(n)
   tol - 1E-6
   cdf - vector(length=n, mode=numeric)
   fprime - vector(length=n, mode=numeric)
   f - vector(length=n, mode=numeric)

  ## Newton loop
   for (i in 1:1000)
   { cdf    - cumdensity(z)
     fprime - pdensity(z)
     f  - cdf - r
     # Newton method
     z  - z - f/fprime

     if (f  tol) break
    }
   cbind(z,cdf)
 }

 alp  - 2.0165
 bt1 - 29.107 ; bt2 - 41.517
 r1 - rgamma(10, shape=alp, scale = bt1)
 r2 - rgamma(10, shape=alp, scale = bt2)
 z - (r1/bt1)+(r2/bt2); sort(z)

 OUTPUT
   z - (r1/bt1)+(r2/bt2); sort(z)
  [1] 0.9344932 1.3117707 1.4514991 2.2637735 2.2795669 3.0548586 3.4485707 
 4.1837583 4.2139719 4.5334232

 newton_gam(z)
    z   cdf
  [1,] -25.540473 0.1883006
  [2,]  -2.079104 0.1725552
  [3,]  -2.878791 0.1331209
  [4,] -81.317382 0.1881811
  [5,]   8.395880 0.1376294
  [6,] -20.381830 0.1601000
  [7,]   7.124515 0.1682288
  [8,]  16.099559 0.1755200
  [9,]  -2.635536 0.1218351
 [10,]   6.438700 0.1341994
 Warning message:
 In if (f  tol) break :
   the condition has length  1 and only the first element will be used

 What does this mean?

 Thank you for any help given.




        [[alternative HTML version deleted]]


 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.





-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] looping problem

2010-02-09 Thread Roslina Zakaria
Hi R-users,
 
I have this code here:

library(numDeriv)
 
fprime - function(z)
{ alp  - 2.0165;
  rho  - 0.868;
 
# simplified expressions
  a  - alp-0.5
  c1 - sqrt(pi)/(gamma(alp)*(1-rho)^alp)
  c2 - sqrt(rho)/(1-rho)
  t1 - exp(-z/(1-rho))
  t2 - (z/(2*c2))^a
  bes1   - besselI(z*c2,a)
  t1bes1 - t1*bes1
  c1*t1bes1*t2
}
 
## Newton iteration
newton_gam - function(z)
{ n   - length(z)
  r   - runif(n)
  tol - 1E-6
  cdf - vector(length=n, mode=numeric)
 
  for (i in 1:1000)
  { # numerical intergration to find the cdf
    cdf  - integrate(fprime, lower = 0, upper = z)$value
 
    # Newton method
    z    - z - (cdf - r)/fprime(z)
 
    if (tol  1e-10) break
   }
  cbind(z,cdf)
}
 
bt1 - 29.107 ; bt2 - 41.517
x1  - 30; x2 - 10;
 
z - (x1/bt1)+(x2/bt2); z
newton_gam(z)
 
 bt1 - 29.107 ; bt2 - 41.517
 x1  - 30; x2 - 10;
 z - (x1/bt1)+(x2/bt2); z
[1] 1.271545
 newton_gam(z)
    z   cdf
[1,] 4.128138 0.6065616
 
But when I try with different values of X1 and X2, it gives
 
 x1  - 1; x2 - 5;
 z - (x1/bt1)+(x2/bt2); z
[1] 0.1547886
 newton_gam(z)
Error in integrate(fprime, lower = 0, upper = z) : 
  non-finite function value
In addition: There were 22 warnings (use warnings() to see them)
 
warnings()
Warning messages:
1: In besselI(z * c2, a) : value out of range in 'bessel_i'
2: In besselI(z * c2, a) : value out of range in 'bessel_i'
3: In besselI(z * c2, a) : value out of range in 'bessel_i'
4: In besselI(z * c2, a) : value out of range in 'bessel_i'
5: In besselI(z * c2, a) : value out of range in 'bessel_i'
 
I try checking the besselI(z * c2, a) values, it seem that no problem.  Why it 
gives such warning? Any idea?
 
 x1  - 0.5; x2 - 0.8
 
 c2 - sqrt(rho)/(1-rho)
 
 z  - (x1/bt1)+(x2/bt2)
 
 besselI(z*c2,a)
[1] 0.03337589
 source(.trPaths[5], echo=TRUE, max.deparse.length=1)
 
 x1  - 1; x2 - 5
 
 c2 - sqrt(rho)/(1-rho)
 
 z  - (x1/bt1)+(x2/bt2)
 
 besselI(z*c2,a)
[1] 0.3339742
 
 x1  - 10; x2 - 50
 
 c2 - sqrt(rho)/(1-rho)
 
 z  - (x1/bt1)+(x2/bt2)
 
 besselI(z*c2,a)
[1] 6076.817
 
 x1  - 100; x2 - 200
 
 c2 - sqrt(rho)/(1-rho)
 
 z  - (x1/bt1)+(x2/bt2)
 
 besselI(z*c2,a)
[1] 1.018641e+24
 
Thank you.
 


  
[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] looping problem

2009-06-23 Thread Roslina Zakaria
Hi R-users,
 
I have this code below and use some code from sn package for multivariate 
skew t (rmst) to generate a set of random numbers.   I try to do a looping to 
produce ‘rand’ for different values of degrees of freedom (degf).  
Actually, I’m not sure how to specify the attribute for the rand.  As we 
know, rand will give us a set of results + all the parameters used, so just 
wonder whether we can do the same for this iteration.
 
rand_skewt - function(dt,degf,n)
{ t1    - length(degf)
  rand  - matrix(dt,nrow=n)
  xi    - colMeans(dt)
  Omega - var(dt)
  alpha - skew(dt,na.rm = TRUE)
  for (i in 1:t1)
{  rand[i] - rmst(n,xi, Omega, alpha, df=degf[i])[1:n,1:2] }
   rand
}
 
dt - yypos[1:10,]; degf - c(4,4.5,5,6); n - 5
 yypos[1:10,]
       [,1]  [,2]
 [1,]  49.7  23.4
 [2,] 141.6 136.8
 [3,] 119.7 141.5
 [4,]  27.2  47.2
 [5,]  56.0  20.6
 [6,]  58.1  62.4
 [7,]  59.2  28.2
 [8,]  22.8  22.4
 [9,]  28.0  48.6
[10,]  69.9  85.2
 
rand_skewt(dt,degf,n) 
 
this is the output I got, but I think something is incorrect…By right every 
rand will have n by 2 matrix…I only have this…
 
 rand_skewt(dt,degf,n)
          [,1] [,2]  [,3] [,4]
[1,]  97.53849 58.1  23.4 62.4
[2,]  96.79773 59.2 136.8 28.2
[3,] 155.26630 22.8 141.5 22.4
[4,]  57.34792 28.0  47.2 48.6
[5,]  56.0 69.9  20.6 85.2
Warning messages:
1: In rand[i] - rmst(n, xi, Omega, alpha, df = degf[i])[1:n, 1:2] :
  number of items to replace is not a multiple of replacement length
2: In rand[i] - rmst(n, xi, Omega, alpha, df = degf[i])[1:n, 1:2] :
  number of items to replace is not a multiple of replacement length
3: In rand[i] - rmst(n, xi, Omega, alpha, df = degf[i])[1:n, 1:2] :
  number of items to replace is not a multiple of replacement length
4: In rand[i] - rmst(n, xi, Omega, alpha, df = degf[i])[1:n, 1:2] :
  number of items to replace is not a multiple of replacement length
 
 
Thank you so much for any help given.
 
Regards.


  
[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] looping problem

2008-04-24 Thread Roslina Zakaria
Dear r-expert,
I would like to generate 30 sets of random numbers from uniform distribution 
(0,1). Each set of random numbers should have 90 data.  Here is my code:
rand.no - function(n,itr)
{ for (i in 1:itr)
  {rand.1 - runif(n,0,1)
  if (i ==1) rand.2 - rand.1
  else rand.2 - cbind(rand.2,rand.1)
  }
  rand.2
}
Question: The code is okay is just that it give me 31 sets instead of 30 sets. 
Can anybody any adjustment to my code.
Your help is really appreciated.
Thank you.


  

Be a better friend, newshound, and 
know-it-all with Yahoo! Mobile.  Try it now.  
http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] looping problem

2008-04-24 Thread Uwe Ligges


Roslina Zakaria wrote:
 Dear r-expert,
 I would like to generate 30 sets of random numbers from uniform distribution 
 (0,1). Each set of random numbers should have 90 data.  Here is my code:
 rand.no - function(n,itr)
 { for (i in 1:itr)
   {rand.1 - runif(n,0,1)
   if (i ==1) rand.2 - rand.1
   else rand.2 - cbind(rand.2,rand.1)
   }
   rand.2
 }
 Question: The code is okay is just that it give me 31 sets instead of 30 
 sets. 

Works for me as expected, but really, why don't you simply say:

rand.no - function(n,itr){
 matrix(runif(n*itr, 0, 1), nrow=n, ncol=itr)
}

which does the same, just much quicker...

Uwe Ligges




 Can anybody any adjustment to my code.
 Your help is really appreciated.
 Thank you.
 
 
   
 
 Be a better friend, newshound, and 
 know-it-all with Yahoo! Mobile.  Try it now.  
 http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ
 
 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] looping problem

2008-04-14 Thread Richard . Cotton
 I would like to do looping for this process below to estimate alpha 
 beta from gamma distribution:
 Here are my data:
 day_data1 - 
 123456 789   10   11   12 
 13   14  15   16   17   18   19   20   21   22   23
 1943 48.3 18.5  0.0  0.0 18.3  0.0   0.0  0.0  0.0  0.0  0.0  0.0 
 2.0  0.0 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.8  2.8
 1944  0.0  0.0  0.0  0.0  0.0  0.0   0.0  0.0  0.0  0.0  0.0  0.0 
 0.0  0.0 0.0  0.0  6.6  0.0  0.0  0.0  0.0  0.0  0.0
 1945  5.3  0.0  0.0  0.0  0.0  2.5   0.0  0.0  0.0  0.0  0.0  0.0 
 0.0  0.0 0.0  0.0  5.3  0.0  0.0  0.0  0.0  0.0  0.0
 1946  0.0  0.0  0.0  0.0  0.0  2.3   0.0  0.0  0.0  0.0  4.8  0.3 
 1.5  0.0 0.8  0.0  0.0  5.8 70.6 12.4  0.5 23.6  0.0
 1947  0.0  0.0  0.0  0.0  0.0  0.0   0.0  0.0  0.0  0.0  0.0  0.0 
 0.0  0.0 0.0  2.0  0.0  0.0  0.0  2.8  0.0  0.0  0.0
 1948  0.3 20.1  0.0  0.0  0.0  0.0   0.0  0.0  0.0  0.0  1.5  0.5 
 0.0  0.0 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 
 library(MASS)
 ## Example January Charleville 1943-2007
 
 Here is my code: 
 
 alpha.beta - function(data,n)
 {  tol - 1E-6
   { for (i in 1:n)
 xi - data[,i]
 ai - xi [xi  tol]
 fit - fitdistr(ai,dgamma, list(shape = 1, rate = 0.1), lower = 
0.01)
   }
   fit
 }
 
 I?m not sure what went wrong since it gives only one output by right
 31 outputs.

A few points about your code
1. The opening brace for the for loop is in the wrong place.  It should be
for (i in 1:n)
{

2. In each iteration of this for loop, you overwrite the value of fit.  To 
assign different values to fit in the loop, you could make it a list, e.g.
alpha.beta - function(data,n)
{ 
  tol - 1E-6
  fit - list()
  for (i in 1:n)
  {
xi - data[,i]
ai - xi [xi  tol]
fit[[i]] - fitdistr(ai,dgamma, list(shape = 1, rate = 0.1), lower = 
0.01)
  }
  fit
}

3. You could tidy up the code a lot by getting rid of the explicit for 
loop, and using apply instead.
tol - 1e-6
myfitdistr - function(x)
{
  x - x[x  tol]
  if(length(x)==0) fit - NULL else fit - fitdistr(x,dgamma, list(shape = 
1, rate = 0.1), lower = 0.01)
}
apply(day_data, 2, myfitdistr) 

(You'll need to play about with the parameters given to fitdistr, since 
this code curerntly fails on column 16, because fitdistr can't optimise.)

Regards,
Richie.

Mathematical Sciences Unit
HSL


ATTENTION:

This message contains privileged and confidential inform...{{dropped:21}}

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] looping problem

2008-04-13 Thread Roslina Zakaria
Hi R-users,

I would like to do looping for this process below to estimate alpha beta from 
gamma distribution:
Here are my data:
day_data1 - 
123456 789   10   11   12   13   14  15 
  16   17   18   19   20   21   22   23
1943 48.3 18.5  0.0  0.0 18.3  0.0   0.0  0.0  0.0  0.0  0.0  0.0  2.0  0.0 0.0 
 0.0  0.0  0.0  0.0  0.0  0.0  0.8  2.8
1944  0.0  0.0  0.0  0.0  0.0  0.0   0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0 0.0 
 0.0  6.6  0.0  0.0  0.0  0.0  0.0  0.0
1945  5.3  0.0  0.0  0.0  0.0  2.5   0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0 0.0 
 0.0  5.3  0.0  0.0  0.0  0.0  0.0  0.0
1946  0.0  0.0  0.0  0.0  0.0  2.3   0.0  0.0  0.0  0.0  4.8  0.3  1.5  0.0 0.8 
 0.0  0.0  5.8 70.6 12.4  0.5 23.6  0.0
1947  0.0  0.0  0.0  0.0  0.0  0.0   0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0 0.0 
 2.0  0.0  0.0  0.0  2.8  0.0  0.0  0.0
1948  0.3 20.1  0.0  0.0  0.0  0.0   0.0  0.0  0.0  0.0  1.5  0.5  0.0  0.0 0.0 
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0

## To extract all the positive values
x1 - day_data1[,1]  
x2 - day_data1[,2]
x3 - day_data1[,3]
x4 - day_data1[,4]
x5 - day_data1[,5]
tol - 1E-6  
a1 - x1[x1tol]
a2 - x2[x2tol]
a3 - x3[x3tol]
a4 - x4[x4tol]
a5 - x5[x5tol]

library(MASS)
## Example January Charleville 1943-2007
fitdistr(a1,dgamma, list(shape = 1, rate = 0.1), lower = 0.01)
fitdistr(a2,dgamma, list(shape = 1, rate = 0.1), lower = 0.01)
fitdistr(a3,dgamma, list(shape = 1, rate = 0.1), lower = 0.01)
fitdistr(a4,dgamma, list(shape = 1, rate = 0.1), lower = 0.01)
fitdistr(a5,dgamma, list(shape = 1, rate = 0.1), lower = 0.01)

Here is my code: 

alpha.beta - function(data,n)
{  tol - 1E-6
  { for (i in 1:n)
xi - data[,i]
ai - xi [xi  tol]
fit - fitdistr(ai,dgamma, list(shape = 1, rate = 0.1), lower = 0.01)
  }
  fit
}

I’m not sure what went wrong since it gives only one output by right 31 outputs.
Thank you for your attention. 

__
t spam protection around 
http://mail.yahoo.com

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.