[R] Help with permutation / loops

Nikhil Kaza nikhil.list at gmail.com
Wed Aug 11 13:34:35 CEST 2010


How about this?

untested since no data is provided.

a <- paste("x",1:6,sep="")
b<- combn(a,2)

> coint<-function (x)
> {
	x1 <- get(x[1])
	x2 <- get(x[2])
>        adfdata(x1)
>        adfdata(x2)
>
>        engle<-lm(x1~x2)
>        residual<-resid(engle)
>        adfresd(residual, k=1)
>
>                                par(mfrow=c(2,1))
>                                ts.plot(x1)
>                                ts.plot(x2)
>                                ts.plot(residual)
> }


apply(b,2,coint)


Careful with the plots, you may just overwrite them on default device.  
One way to overcome it is to plot them to a pdf and name them  
appropriately for each iteration.

Nikhil Kaza
Asst. Professor,
City and Regional Planning
University of North Carolina

nikhil.list at gmail.com

On Aug 11, 2010, at 6:34 AM, Ferreira, Thiago Alves wrote:

> Hi everyone,
> I am writing a code for cointegration between n (n=6 initially) pairs.
>
> I have done the whole thing for 2 variables. There is a function  
> coint(x1,x2) that takes 2 inputs x1 and x2 and does the following:
>
> coint<-function (x1,x2)
> {
>        adfdata(x1)
>        adfdata(x2)
>
>        engle<-lm(x1~x2)
>        residual<-resid(engle)
>        adfresd(residual, k=1)
>
>                                par(mfrow=c(2,1))
>                                ts.plot(x1)
>                                ts.plot(x2)
>                                ts.plot(residual)
> }
>
> Where X1,x2,..,x6 are time series of length 250 or more
>
> Where adfdata() is a function that calculates the adf test for x1  
> and x2. There are 6 variables in total (x1,x2,...,x6) and I want to  
> calculate this function coint for the permutation of these  
> variables. That is coint(x1,x2); coint(x1,x3);  
> coint(x1,x4);...coint(x6,x5) (without repetition because x1,x1 are  
> cointegrated already)
>
> I thought about creating an array with the combinations Xi,Xj and  
> apply the function to each combination in the array but I could not  
> get it to work...
> I would really appreciate if someone could help me on this!
>
> Thank you,
> Kind regards,
> Thiago
>
> ______________________________________________
> 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.



More information about the R-help mailing list