问题描述
我想使用 zoo 函数 rollapply 在时间序列上应用函数(例如平均值),但仅使用最后 N 个已知点.例如:
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)
产生:
10 11 12
1.5 2.5 3.5
1.5 2.5 3.5
我想制作一个包含 11、12、13 日期条目和 1.5、2.5、3.5 值的系列.这些值似乎正确,但 rollmean 输出的日期似乎与我想要的不符.我有点担心只是使用 time(x)<-
将我想要的日期分配给动物园对象,因为我不确定 rollapply 是否真的在做正确的事情.一如既往地感谢您的帮助.
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.
推荐答案
Specify align="right"
或者只使用 rollmeanr
(不过只在最近版本的 zoo 中).
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
这篇关于在 R (zoo) 中对后视数据滚动应用时间序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!