i am making a research in R project. Data is stored in 3 zoo objects with 9 columns each. I want to make granger-test of each column on each and store results (Pr(>F) values) in 3 matrices 9×9.
I try to use for() loop and have the following problem
for(i in 1:ncol(dfvola.pre)) {
grangertest(order=4,
dfvola.pre[,2],
dfvola.pre[,i])$'Pr(>F)'[2]
}
produces error Error in solve(vc[ovar, ovar]) : subscript out of bounds
At the same time
grangertest(order=4, dfvola.pre[,2],
dfvola.pre[,3])$'Pr(>F)'[2]
works fine, i.e. I can store Pr(>F) in variable or matrix cell. But to make 3 matrices 9×9 I had to write 81*3 lines of code and I wanted to know is there more elegant solution in R.
I am new to R and have a feeling that perhaps there should be one, even more elegant that nested loops 🙂
Just in case, this is my data (zoo object):
head(dfvola.pre)
CH HK SI RU EN
2002-02-08 -0.04105356 -0.021797675 -0.03411227 -0.06821175 -0.1728407
2002-02-11 -0.07383230 -0.034095749 -0.05364423 -0.12425684 -0.1656535
2002-02-12 -0.09345455 0.011086639 -0.06846780 -0.14499508 -0.0169436
2002-02-13 -0.09492381 0.009393133 -0.08113240 -0.13592678 2.4487733
2002-02-14 -0.08875820 0.026173698 -0.08273877 -0.07034832 0.4948738
2002-02-15 -0.07895699 -0.041345747 -0.07501171 -0.01333353 0.2248262
US GR FR JP
2002-02-08 0.015261541 -0.02886118 -0.01236761 -0.03934752
2002-02-11 -0.102323758 -0.11697662 -0.12982389 -0.08438561
2002-02-12 0.001706797 -0.11818056 -0.14477277 -0.09312417
2002-02-13 -0.085467795 -0.10278221 -0.07810301 -0.11467290
2002-02-14 -0.082738612 -0.13175354 -0.12782784 -0.12296171
2002-02-15 -0.098760674 -0.08439060 -0.08903754 -0.11871091
The problem occurs when
ibecomes equal to 2. Then you are testing the difference between two identical dataset, which leads to the error:I’m not familiar with
grangertest, but it might be that a covariance matrix becomes singular. The solution is to skip i = 2: