简体   繁体   中英

for loop: performing cointegration test for all possible combinations in R

I am trying to figure out if I can find cointegration for each combination of non-stationary time series with the ADF test. This can be done with the following function:

coint <- function(x,y) {
vals <- data.frame(x,y)
beta <- coef(lm(vals[,2] ~ vals[,1] + 0, data = vals))[1]
(tseries::adf.test(vals[,2]- beta*vals[,1], alternative = "stationary", k = 0))$p.value
}

If i run the function coint(df1, df2), I get a p-value. However, I'd like to do this for every possible combination (and store it in a dataframe). I already know i can make all possible combinations with the combn() function. However, i just can't get my for loop right to do this for every possible combination. Maybe this operation can also be done with a function from the purrr package?

Any suggestions would be much appreciated! I also added a sample dataframe below.

   # A tibble: 18 x 5
       `1` `2` `3` `4` `5` `

 1    416 850 53  78  66     
 2    407 922 43  82  67    
 3    410 901 37  84  71     
 4    412 945 53  95  77     
 5    409 998 101 83  86     
 6    375 947 53  86  84     
 7    364 908 43  87  71     
 8    377 952 39  95  64     
 9    387 961 18  109 69  
10    352 932 11  102 69     
11    332 920 12  108 69    
12    318 987 22  121 83     
13    320 961 17  124 88     
14    325 931 15  145 64     
15    328 816 6   169 44     
16    315 925 8   156 55     
17    309 737 4   176 49     
18    273 626 4   193 59    

Not the best and efficient loop but it will be okay i think: The input would be a dataframe with all Variables to check

 coint <- function(vars) {
    d<-as.matrix(vars) #convert data frame to Matrix
    n<-length(colnames(vars)) #calculate the total number of variables
    m<-combn(n,2) #calculate all possible combinations of pairs for all variables
    col_m<-dim(m)[2] #number of all possible combinations
    result<-matrix(NA,nrow=col_m,ncol=3) #empty result matrix
    colnames(result)<-c("Var_1","Var_2","p_Value")
    for (i in 1:col_m){
      Var_1<-m[1,i] 
      Var_2<-m[2,i]
      res <- lm(d[,Var_1] ~ d[,Var_2] + 0)$residuals 
      p<-tseries::adf.test(res, alternative = "stationary", k = 0)$p.value
      result[i,1]<-colnames(vars)[Var_1]
      result[i,2]<-colnames(vars)[Var_2]
      result[i,3]<-p
    }
    return(result)
  }

The cointegration approach is about the residuals of 2 variables needs to be stationary. I immediately took them out of the estimate instead of recalculating them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM