문제

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)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top