I'm new in programming, so it might be something which is done easily. 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