简体   繁体   中英

Combine same-name columns and apply Johansen test in R

I have two data bases (data is multicolumn before and after treatment):

Before treatment Data1<-read.csv("before.csv")

    X1        X2    X3
1   0.21    0.32    0.42
2   0.34    0.23    0.33
3   0.42    0.14    0.11
4   0.35    0.25    0.35
5   0.25    0.41    0.44

After treatment Data2<-read.csv("after.csv")

    X1       X2      X3
1   0.33    0.43    0.7
2   0.28    0.51    0.78
3   0.11    0.78    0.34
4   0.54    0.34    0.34
5   0.42    0.64    0.22

I would like to combine the data by columns (ie x1 in Data1 and x1 in Data2 similarly: x2 in Data1 and x2 in Data2 and so on) and perform Johansen Cointegration test for each pair.

What I tried is to make:

library("urca")
x1<-cbind(Data1$x1, Data2$x1)
Jo1<-ca.jo(x1, type="trace",K=2,ecdet="none", spec="longrun")
summary(Jo1)

x2<-cbind(Data1$x1, Data2$x2)
Jo2<-ca.jo(x2, type="trace",K=2,ecdet="none", spec="longrun")
summary(Jo2)

This gives me what I want but I would like to automate the process, ie instead of manually combining data, to have all pair-wise combinations.

You can loop through the columns name and find the Johansen Cointegration as follows:

# Create a sample data frame
Data1<- data.frame(X1 = rnorm(10, 0, 1), X2 = rnorm(10, 0, 1),  X3 = rnorm(10, 0, 1))
Data2 <-data.frame(X1 = rnorm(10, 0, 1), X2 = rnorm(10, 0, 1),  X3 = rnorm(10, 0, 1))

library("urca")
 # loop through all columns index
for(i in ncol(Data1)) {
  col <- paste0("X", as.character(i)) # find the column name
  data <- cbind(Data1[, col], Data2[, col]) # get the data from Data1 and Data2, all rows of a column = col
  # Your method for finding Ca.Jo ...
  Jo<- ca.jo(data, type="trace",K=2,ecdet="none", spec="longrun")
  summary(Jo)
}

You can also use colnames for looping as:

for(col in colnames(Data1)) {
  print(col)
  data <- cbind(Data1[, col], Data2[, col])
  print(data)
  #Jo<- ca.jo(data, type="trace",K=2,ecdet="none", spec="longrun")
  #summary(Jo)
}

Hope this will help you.

Based on krishna's answere, but modified the loop:

for(i in 1:ncol(Data1)) {
  col <- paste0("X", as.character(i)) 
  data <- cbind(Data1[, col], Data2[, col])
  colnames(data) <- c(paste0("Data1_",col),paste0("Data2_",col)) # add column names

  Jo<- ca.jo(data, type="trace",K=2,ecdet="none", spec="longrun")
  print(summary(Jo)) # print the summary to the console
}

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