Rollapply 在时间序列的每个向量上使用不同的滚动窗口
假设您有 3 个不同大小的时间序列向量:
xdate = seq(as.Date("2010/1/1"), by = "day", length.out = 2789)
ydate = seq(as.Date("2010/1/1"), by = "day", length.out = 1289)
zdate = seq(as.Date("2010/1/1"), by = "day", length.out = 2245)
xf = as.factor(rep("x",2789))
yf = as.factor(rep("y",1289))
zf = as.factor(rep("z",2245))
x = rnorm(2789)
y = rnorm(1289)
z = rnorm(2245)
date = c(xdate,ydate,zdate)
value = c(x,y,z)
fact = c(xf,yf,zf)
library(tidyverse)
library(quantmod)
dat = tibble(date = as.Date(date),fact,value);head(dat)
结果:
# A tibble: 6 x 3
date fact value
<date> <fct> <dbl>
1 2010-01-01 x 1.15
2 2010-01-02 x -0.830
3 2010-01-03 x -1.93
4 2010-01-04 x -0.957
5 2010-01-05 x -0.174
6 2010-01-06 x 0.288
现在您可以看到每个时间序列都有不同的长度。我想使用 R 中不同滚动窗口的 rollapply 函数进行滚动计算。滚动窗口的大小必须是每个向量的大小减去 252。
YEAR = 252
dat%>%
group_by(fact)%>%
summarise(ROLL=n()-YEAR)
# A tibble: 3 x 2
fact ROLL
<fct> <dbl>
1 x 2537
2 y 1037
3 z 1993
我要问并且我想要有人的帮助是 x 的滚动窗口对于 y 1037 和 z 1993 是 2537 。
是否可以以某种方式在 dplyr 管道中组合包含 rollapply 函数?
如果我分别对每个向量取每个滚动窗口很容易,但想象一下如果我有 200 个向量,那就很困难了。
有什么帮助吗?
Let's say that you have 3 time-series vectors with different sizes:
xdate = seq(as.Date("2010/1/1"), by = "day", length.out = 2789)
ydate = seq(as.Date("2010/1/1"), by = "day", length.out = 1289)
zdate = seq(as.Date("2010/1/1"), by = "day", length.out = 2245)
xf = as.factor(rep("x",2789))
yf = as.factor(rep("y",1289))
zf = as.factor(rep("z",2245))
x = rnorm(2789)
y = rnorm(1289)
z = rnorm(2245)
date = c(xdate,ydate,zdate)
value = c(x,y,z)
fact = c(xf,yf,zf)
library(tidyverse)
library(quantmod)
dat = tibble(date = as.Date(date),fact,value);head(dat)
With result:
# A tibble: 6 x 3
date fact value
<date> <fct> <dbl>
1 2010-01-01 x 1.15
2 2010-01-02 x -0.830
3 2010-01-03 x -1.93
4 2010-01-04 x -0.957
5 2010-01-05 x -0.174
6 2010-01-06 x 0.288
Now each time series as you can see has different lengths. I want to roll calculate with rollapply function in R with different rolling windows. The rolling window must be the size of each vector minus 252.
YEAR = 252
dat%>%
group_by(fact)%>%
summarise(ROLL=n()-YEAR)
# A tibble: 3 x 2
fact ROLL
<fct> <dbl>
1 x 2537
2 y 1037
3 z 1993
What I am asking and I want someone's help is that the rolling window for x is 2537 for y 1037 and for z 1993.
Is that possible to be combined somehow in a dplyr pipeline containing the rollapply function?
Separetely if i take each roll window on each vector is easy but imagine if i have 200 vectors that will be difficult.
Any help ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1) 转换为宽格式动物园对象 z,然后转换为动物园对象 L 的列表,z 的每列一个,将 rollfun 应用于 L 的每个组件,创建动物园对象列表,然后合并回来转换为宽格式动物园对象 zroll 并使用它或选择转换为长格式数据框 droll。
2) 这也可以表示为一个管道,其中 rollfun 来自上面。
3) 使用 dplyr
1) Convert to a wide form zoo object z and then to a list of zoo objects L, one per column of z, apply rollfun to each component of L creating a list of zoo objects and then merge back into a wide form zoo object zroll and either use that or optionally convert to long form data frame droll.
2) This could also be expressed as a pipeline where rollfun is from above.
3) With dplyr