I am attempting to do a pixel-wise granger causal test on two raster stacks with 60 rasters each. The example below has only 20 rasters:
library(raster)
library(lmtest)
r <- raster(ncol=10, nrow=10)
r[]=1:ncell(r)
S <- stack(r,r,r,r,r,r,r,r,r,r,r,r)
R <- stack(r,r,r,r,r,r,r,r,r,r,r,r)
FNO2<-stack(S,R)
The original function using th "lmtest" package is:
D<- grangertest(degg ~ dchick, order=4)
Here's a modification I did to run the original grangertest function on raster stacks?
funG <- function(x) { if (is.na(x[1])){ NA } else { grangertest(x[13:24] ~ x[1:12],order=1 )}}
granger<-calc(FNO2,funG)
Where FNO2 is the stack of both raster stacks. I get the error below:
Error in `colnames<-`(`*tmp*`, value = c("x", "y", "x_1", "y_1")) :
length of 'dimnames' [2] not equal to array extent
How do I modify this function for rasters please?