Rollapply for time series

爱⌒轻易说出口 提交于 2019-12-02 22:39:17

You need to use align='right' instead of using the default which is align='center', or instead of using rollapply, use the rollapplyr wrapper which has align='right' as the default.

From ?rollapply:

align specifyies whether the index of the result should be left- or right-aligned or centered (default) compared to the rolling window of observations. This argument is only used if width represents widths.

Although, for this, personally, I'd use runSD from the TTR package because it uses compiled code and will be faster.

Either of these should do what you expect, but the second one will be faster.

library(zoo)
rollapply(x, 20, sd, fill=NA, align='right')

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