Applying functions on rolling windows of zoo objects is normally quite straightfoward, e.g. a moving average:
z <- zoo(1:10, as.Date(31:40))
rollapply(z, 4, mean, align="right")
Now I want to do the same thing with a statistical test, i.e. apply a Augmented Dickey-Fuller test on each window and get the test statistic like I got the mean in the above example.
So basically I am looking for the equivalent of the following piece of code (which of course doesn't work!):
rollapply(z, 4, ADF.test, align="right")
The following works for me.
library(zoo)
library(tseries)
z <- zoo(rnorm(100), as.Date(1:100))
rollapplyr(z, 20, adf.test)
In case you just want the p-value:
rollapplyr(z, 20, function(u) adf.test(u)$p.value)