rolling window
On 7/19/07, Jordi Molins <jordi.molins.coronado at gmail.com> wrote:
I want to do a rolling window calculation for multivariate data. In other words: rollFun in fMultivar makes univariate calculations, ie, given for example mydata[1:5], mydata[2:6], ? I get one number (the function applied to mydata[1:5]) every time. rollingFunction in PerformanceAnalytics does the same but for possibly multivariate data, but one column at a time. rapply in zoo also does rolling window for univariate data (at least, all examples are with univariate data).
rollapply (which was called rapply in old versions of zoo but was renamed to avoid collisions with a new function of the same name in the core of R) in zoo can handle multivariate zoo series either one column at a time (default) or combined (via by.column = FALSE):
library(zoo) z <- zoo(matrix(1:24, 12)) z
1 1 13 2 2 14 3 3 15 4 4 16 5 5 17 6 6 18 7 7 19 8 8 20 9 9 21 10 10 22 11 11 23 12 12 24
rollapply(z, 3, mean, by.column = TRUE) # can omit by.column here
2 2 14 3 3 15 4 4 16 5 5 17 6 6 18 7 7 19 8 8 20 9 9 21 10 10 22 11 11 23
rollapply(z, 3, mean, by.column = FALSE)
2 3 4 5 6 7 8 9 10 11 8 9 10 11 12 13 14 15 16 17