[R] Help with permutation / loops
Ferreira, Thiago Alves
thiago.alves.ferreira at citi.com
Thu Aug 12 16:15:48 CEST 2010
Hello Nikhil, hope you are well today.
I am sorry to be a pain but I have one follow up question, I am trying to express my results in a grid, which would look like a 6 by 6 matrix and would have just Yes or NO in each grid..
So what I am thinking is a way to store every result I get on " apply(b,2,coint) " in an array or vector, and express them is this matrix. So that I can just look at it and see whether assets swap2 and vol are cointegrated..
Do you reckon you point me in the right direction as to how to do that?
Thank you!
Thiago
-----Original Message-----
From: Nikhil Kaza [mailto:nikhil.list at gmail.com]
Sent: 11 August 2010 12:35
To: Ferreira, Thiago Alves [ICG-MKTS]
Cc: 'r-help at R-project.org'
Subject: Re: [R] Help with permutation / loops
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