From help(grangertest)
in {lmtest}
library(lmtest)
#> Loading required package: zoo
#>
#> Attaching package: 'zoo'
#> The following objects are masked from 'package:base':
#>
#> as.Date, as.Date.numeric
## Which came first: the chicken or the egg?
data(ChickEgg)
ChickEgg
#> Time Series:
#> Start = 1930
#> End = 1983
#> Frequency = 1
#> chicken egg
#> 1930 468491 3581
#> 1931 449743 3532
#> 1932 436815 3327
#> 1933 444523 3255
#> 1934 433937 3156
#> 1935 389958 3081
#> 1936 403446 3166
#> 1937 423921 3443
#> 1938 389624 3424
#> 1939 418591 3561
#> 1940 438288 3640
#> 1941 422841 3840
#> 1942 476935 4456
#> 1943 542047 5000
#> 1944 582197 5366
#> 1945 516497 5154
#> 1946 523227 5130
#> 1947 467217 5077
#> 1948 499644 5032
#> 1949 430876 5148
#> 1950 456549 5404
#> 1951 430988 5322
#> 1952 426555 5323
#> 1953 398156 5307
#> 1954 396776 5402
#> 1955 390708 5407
#> 1956 383690 5500
#> 1957 391363 5442
#> 1958 374281 5442
#> 1959 387002 5542
#> 1960 369484 5339
#> 1961 366082 5358
#> 1962 377392 5403
#> 1963 375575 5345
#> 1964 382262 5435
#> 1965 394118 5474
#> 1966 393019 5540
#> 1967 428746 5836
#> 1968 425158 5777
#> 1969 422096 5629
#> 1970 433280 5704
#> 1971 421763 5806
#> 1972 404191 5742
#> 1973 408769 5502
#> 1974 394101 5461
#> 1975 379754 5382
#> 1976 378361 5377
#> 1977 386518 5408
#> 1978 396933 5608
#> 1979 400585 5777
#> 1980 392110 5825
#> 1981 384838 5625
#> 1982 378609 5800
#> 1983 364584 5656
r <- grangertest(egg ~ chicken, order = 3, data = ChickEgg)
r
#> Granger causality test
#>
#> Model 1: egg ~ Lags(egg, 1:3) + Lags(chicken, 1:3)
#> Model 2: egg ~ Lags(egg, 1:3)
#> Res.Df Df F Pr(>F)
#> 1 44
#> 2 47 -3 0.5916 0.6238
This is how grangertest
is supposed to workâit takes a time series and columns within it as an argument and returns a value of class()
anova
data frame
.
You can either use a bivariant
series like ChickEggs as its principal argument or two univariant
observations. In the example above, let's assume we are doing that in a for
loop from 1:15. Looking at egg[,i]
what happens. We just need to look at egg[,i]
First, i = 1 , and second, i = 2 work, but i =3 is straight out.
library(lmtest)
#> Loading required package: zoo
#>
#> Attaching package: 'zoo'
#> The following objects are masked from 'package:base':
#>
#> as.Date, as.Date.numeric
data(ChickEgg)
ChickEgg[,1]
#> Time Series:
#> Start = 1930
#> End = 1983
#> Frequency = 1
#> [1] 468491 449743 436815 444523 433937 389958 403446 423921 389624 418591
#> [11] 438288 422841 476935 542047 582197 516497 523227 467217 499644 430876
#> [21] 456549 430988 426555 398156 396776 390708 383690 391363 374281 387002
#> [31] 369484 366082 377392 375575 382262 394118 393019 428746 425158 422096
#> [41] 433280 421763 404191 408769 394101 379754 378361 386518 396933 400585
#> [51] 392110 384838 378609 364584
ChickEgg[,2]
#> Time Series:
#> Start = 1930
#> End = 1983
#> Frequency = 1
#> [1] 3581 3532 3327 3255 3156 3081 3166 3443 3424 3561 3640 3840 4456 5000 5366
#> [16] 5154 5130 5077 5032 5148 5404 5322 5323 5307 5402 5407 5500 5442 5442 5542
#> [31] 5339 5358 5403 5345 5435 5474 5540 5836 5777 5629 5704 5806 5742 5502 5461
#> [46] 5382 5377 5408 5608 5777 5825 5625 5800 5656
ChickEgg[,3]
#> Error in `[.default`(ChickEgg, , 3): subscript out of bounds
The OP example cannot have gone beyond $i = 2$, or that would have been the message, so the error message seen must have something to do with the relationship between
COLand
ALL, which I can't comment on without a
reprex. [See the FAQ: How to do a minimal reproducible example
reprex` for beginners](FAQ: How to do a minimal reproducible example ( reprex ) for beginners)