[R] programing problem with for( )
David Winsemius
dwinsemius at comcast.net
Mon Feb 1 16:34:42 CET 2010
On Feb 1, 2010, at 8:20 AM, marlene marchena wrote:
> Hi,
>
> Thanks a lot it works. But as Ted remarked it was a simple case of a
> more
> complex
> procedure.
>
> Here is what I trying to do (sorry I was not able to find another
> example)
>
> I wrote a fuction that I call bullexe(phi,theta,L) the code of this
> function
> is at the end of this mail.
>
> I want to run different models with my fuction for different values.
> I want
> to make a table.
>
> I started to write a code to get the results for L=1,2,3,4, and 5
> with phi
> and theta fixed.
>
> bull<-
> matrix
> (data
> =NA,nrow=5,ncol=1,dimnames=list(c("m1","m2","m3","m4","m5"),c("be")))
> bull
> VarLT<-
> matrix
> (data
> =
> NA
> ,nrow=5,ncol=1,dimnames=list(c("m1","m2","m3","m4","m5"),c("VarLT")))
> VarLT
>
> for(i in 1:5)
> { m=bullexe(0.75,0.1,i)
> bull[i,]<-m[1]
> VarLT[i,]<-m[3]
> }
> est<-matrix(data=c(bull,VarLT),nrow=5,ncol=2)
> est
>
>
> It works fine but I am wondering if I can do it directly to fill
> scp<-
> matrix
> (data
> =
> NA
> ,nrow
> =
> 5,ncol=2,dimnames=list(c("m1","m2","m3","m4","m5"),c("bull","VarLT")))
> scp
You might want to look at mapply or Vectorize if your function will
not take vector arguments
Might be as simple as:
scp <- mapply(bullexe, list(L= 1:5), MoreArgs=list(phi= 0.75,
theta=0.1) )
Since your are returning: bull=M,VarD=VarD,VarLT=VarLT ...
you could drop the unwanted column
scp[ , -2]
--
David.
>
> How could I solve this problem?
>
> and still how could I address the problem if I want to move the three
> parameters at the same time?
>
> I found this post https://stat.ethz.ch/pipermail/r-help/2008-May/163110.html
>
> but there the problem is quite different. Any idea?
>
> Here is the code of my fuction bullexe()
>
> bullexe=function(phi,theta,L)
> { arma=ARMAtoMA(ar=phi, ma=theta, 1000);
> VarD=sum(arma^2)+1;
> values=ARMAtoMA(ar=phi, ma=theta, L);
>
> total = 0
> for (i in 1:L)
> { valsum= sum (values[i:L]);
> if( i==1)
> {
> total = valsum;
> }
> else
> {
> total = total + values[i-1] * valsum;
> }
> }
>
> M=1+2*total/VarD;
>
> arma1=ARMAtoMA(ar=phi, ma=theta,L);
> arma2=arma1^2;
>
> totalLT = 0
> for (i in 1:L-1)
> { valsumLT= sum(arma2[1:(L-i)]);
> totalLT = totalLT + valsumLT;
> }
> VarLT=L+totalLT;
>
> sc=c(bull=M,VarD=VarD,VarLT=VarLT)
> return(sc)
> }
>
>
>
>
> 2010/2/1 Ted Harding <Ted.Harding at manchester.ac.uk>
>
>> On 01-Feb-10 11:29:40, marlene marchena wrote:
>>> Hi R-users
>>>
>>> I'm writing a code to run a fuction but I found an error that I
>>> can't
>>> fix. I
>>> reproduced the error with a simple example.
>>>
>>> The correct answer is k but I can't fill my s matrix. What I'm doing
>>> wrong?
>>>
>>>
>>> s<-matrix(data=NA,nrow=1,ncol=5 )
>>> s
>>>
>>> for(i in 1:5)
>>> {
>>> k=sqrt(i)
>>> s[,i]<-k[i]
>>> print(k)
>>> }
>>>
>>> s
>>>
>>> Thanks in advance,
>>> Marlene.
>>
>> Use s[,i]<-k instead of s[,i]<-k[i] since k=sqrt(i) assigns a
>> single value to k. Hence k[1] will be the same as k, but for
>> i>1 k[i] will always be NA.
>>
>> A better way to do the whole thing (in your example) is simply
>>
>> s <- sqrt(1:5)
>>
>> but your example may be a very simple case of a more complex
>> procedure!
>>
>> Hoping this helps,
>> Ted.
>>
>> --------------------------------------------------------------------
>> E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
>> Fax-to-email: +44 (0)870 094 0861
>> Date: 01-Feb-10 Time: 11:41:46
>> ------------------------------ XFMail ------------------------------
>>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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.
David Winsemius, MD
Heritage Laboratories
West Hartford, CT
More information about the R-help
mailing list