用“增长"滚动应用窗户 [英] rollapply with "growing" window
问题描述
伙计们,通常当您执行以下操作时:
Guys, normally when you do something like:
tmp = zoo(rnorm(100), 1:100)
rollapply(tmp, 10, function(x) quantile(x, 0.05), align="right")
完全正确 rollapply
将从 10 个元素可用的那一刻开始计算值.
Quite rightly rollapply
will start calculating the value from the moment 10 elements are available.
不幸的是,我需要在前 10 个观察中使用尽可能多的数据的东西,本质上是一个不断增长的数据窗口,直到有足够的数据来使用滑动窗口,例如1, 1:2, 1:3, 1:4 等,直到我们有至少 10 个元素,然后像往常一样滑动窗口.
Unfortunately I need something that uses as much data as possible for the fist 10 observations, essentially a growing window of data till there is enough data to use a sliding window, e.g. 1, 1:2, 1:3, 1:4, etc. till we have at least 10 elements and then slide the window as usual.
还有比丑陋的 for 循环更好的方法吗?
Is there a better way to do this than an ugly for loop?
推荐答案
为什么不一开始就用 9 个 NA 填充系列?绝对比丑陋的 for 循环"好:
Why not just pad the series with 9 NAs at the beginning? Definitely better than "ugly for loops":
tmp = zoo(c(rep(NA,9), rnorm(100)), 1:109)
zoo(rollapply(tmp, 10, function(x) quantile(x, 0.05, na.rm = TRUE),
align="right"), 1:100)
这篇关于用“增长"滚动应用窗户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!