Rollapply 在时间序列的每个向量上使用不同的滚动窗口

发布于 2025-01-10 13:06:11 字数 1276 浏览 0 评论 0原文

假设您有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飘逸的'云 2025-01-17 13:06:11

1) 转换为宽格式动物园对象 z,然后转换为动物园对象 L 的列表,z 的每列一个,将 rollfun 应用于 L 的每个组件,创建动物园对象列表,然后合并回来转换为宽格式动物园对象 zroll 并使用它或选择转换为长格式数据框 droll。

library(zoo)

z <- read.zoo(dat, split = "fact")
L <- as.list(z)

rollfun <- function(x) rollapplyr(x, length(na.omit(x)) - 252, mean)
zroll <- do.call("merge", Map(rollfun, L))
droll <- fortify.zoo(zroll, melt = TRUE)

2) 这也可以表示为一个管道,其中 rollfun 来自上面。

droll2 <- dat |>
  read.zoo(split = "fact") |>
  as.list() |>
  Map(f = rollfun) |>
  do.call(what = "merge") |>
  fortify.zoo(melt = TRUE)

3) 使用 dplyr

library(dplyr, exclude = c("lag", "filter"))
library(zoo)

dat %>%
 group_by(fact) %>%
 mutate(roll = rollapplyr(value, n() - 252, mean, fill = NA)) %>%
 ungroup
 

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.

library(zoo)

z <- read.zoo(dat, split = "fact")
L <- as.list(z)

rollfun <- function(x) rollapplyr(x, length(na.omit(x)) - 252, mean)
zroll <- do.call("merge", Map(rollfun, L))
droll <- fortify.zoo(zroll, melt = TRUE)

2) This could also be expressed as a pipeline where rollfun is from above.

droll2 <- dat |>
  read.zoo(split = "fact") |>
  as.list() |>
  Map(f = rollfun) |>
  do.call(what = "merge") |>
  fortify.zoo(melt = TRUE)

3) With dplyr

library(dplyr, exclude = c("lag", "filter"))
library(zoo)

dat %>%
 group_by(fact) %>%
 mutate(roll = rollapplyr(value, n() - 252, mean, fill = NA)) %>%
 ungroup
 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文