rrolling-computationcausality

Rolling Granger Causality Test


I'm trying to use the rollapply function from the zoo package to estimate Granger causality with a rolling window, the grangertest function is from the package lmtest, I have monthly data span over the period 1976-1984.

            y       y1
Jan 1970 7.468513 7.672292
Feb 1970 7.475906 7.468513
Mar 1970 7.448334 7.475906
Apr 1970 7.351158 7.448334
May 1970 7.362011 7.351158
Jun 1970 7.326466 7.362011

I used the below-described codes but none of them seems to work

rol.c <- rollapply(mydata, width = 24,
FUN = function(z) coef(grangertest(mydata, order = 6)), 
by.column = FALSE, align = "right")

rol.cs <- function(x) c(granger.test(x, p = 6))
rollapplyr(mydata, 24, granger.test, by.column = FALSE )

Any help is deeply appreciated.


Solution

  • The function used in rollapply must return a vector or matrix.

    rollapplyr(z, 24, function(x) as.matrix(grangertest(x)), by.column = FALSE)