Rollapply time series in R (zoo) according to reverse data

I would like to use the rollupply function for zoo to apply a function (e.g. average value) to a time series, but only using the last N known points. For instance:

x = zoo(c(1,2,3,4), order.by=c(10,11,12,13))

rollmean(x,2)

It produces:

10 11 12

1.5 2.5 3.5

I would like to create a series in which there would be records of the dates 11, 12, 13 and the values ​​1.5, 2.5, 3.5. The values ​​seem to be correct, but the dates when the rollmean output does not seem to match what I would like. I'm a little worried about just setting the dates I want for the zoo object using time(x)<- , because I'm not sure rollapply really does the right thing. Help is appreciated, as always.

+4
source share
1 answer

Indicate align="right" or just use rollmeanr (only in the latest versions of the zoo).

 > rollmean(x,2,align="right") 11 12 13 1.5 2.5 3.5 > rollmeanr(x,2) 11 12 13 1.5 2.5 3.5 
+5
source

Source: https://habr.com/ru/post/1384506/


All Articles