rollapply time series in R (zoo) on backward looking data

只谈情不闲聊 提交于 2019-12-10 07:04:20

问题


I would like to use the zoo function rollapply to apply a function (for example mean) on a time series but only using the last N known points. For example:

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

rollmean(x,2)

Produces:

10 11 12

1.5 2.5 3.5

I would like to produce a series that would have date entries of 11, 12, 13 and values of 1.5, 2.5, 3.5. The values seem correct but the dates that rollmean outputs don't seem to correspond to what I would like. I'm a bit worried about just assigning the dates I want to the zoo object using time(x)<- because I'm not sure that rollapply is actually doing the right thing. Help is appreciated as always.


回答1:


Specify align="right" or just use rollmeanr (only in recent versions of zoo though).

> 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 


来源:https://stackoverflow.com/questions/8370999/rollapply-time-series-in-r-zoo-on-backward-looking-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!