在分组数据上使用 slide_dbl() 滚动 window

Rolling window with slide_dbl() on grouped data

这是对以下问题的扩展:

我想用 slide_dbl() 改变我分组的 tibble 的一列,即对所有组应用 slide_dbl(),但只在它们内部,而不是在它们之间。

当 运行 链接问题的解决方案时,我收到以下错误消息:

Error: Problem with `mutate()` input `rollreg`.
x Inapplicable method for 'mutate_' applied to object of class "c('double', 'numeric')".

我的小标题结构如下:

tibble [450,343 x 3] (S3: grouped_df/tbl_df/tbl/data.frame)
 $ company: num [1:450343] 1 1 1 1 1 ...
 $ date: Date[1:450343], format: "2011-11-30" "2011-12-31" "2012-01-31" "2012-02-29" ...
 $ result: num [1:450343] NA NA NA 12.5981 -2.9023 ...
 - attr(*, "groups")= tibble [3,339 x 2] (S3: tbl_df/tbl/data.frame)
  ..$ company: num [1:3339] 1 2 3 4 5 ...
  ..$ .rows : list<int> [1:3339] 

为了完成,这是我运行根据链接解决方案的代码:

testtest <- data %>%
  group_by(company) %>% nest() %>%
  mutate(rollreg = map(data, ~ .x %>% mutate(result_2 = slide_dbl(.x = .$result, .f = ~prod(1+.)-1, .before = 11, .after = -1, complete=TRUE)))) %>%
  select(-data) %>% unnest(rollreg)

此处,出现上述错误信息。我想这是因为数据结构。然而,我想不出任何解决方案(也没有像 group_map() 或 group_modify() 这样的类似功能)。谁能帮忙?提前致谢!

分组列的选项是 group_split(在示例中,使用 'case',使用 map 循环数据集的 list,在中创建新列mutate 通过应用 slide_dbl

library(dplyr)
library(tidyr)
library(purrr)
data %>% 
   group_split(case) %>%
   map_dfr(~ .x %>% 
      mutate(out = slide_dbl(r1, .f = ~ prod(1 + .x) - 1, 
          .before = 5, .after = -1, complete = TRUE)))

-输出

# A tibble: 30 x 6
#       t case       r1      r2     r3    out
#   <int> <chr>   <dbl>   <dbl>  <dbl>  <dbl>
# 1     1 a     -0.294  -0.164   1.33   0    
# 2     2 a      0.761   1.01    0.115 -0.294
# 3     3 a     -0.781  -0.499   0.290  0.243
# 4     4 a     -0.0732 -0.110   0.289 -0.728
# 5     5 a     -0.528   0.707   0.181 -0.748
# 6     6 a     -1.35   -0.411  -1.47  -0.881
# 7     7 a     -0.397  -1.28    0.172 -1.06 
# 8     8 a      1.68    0.956  -2.81  -1.02 
# 9     9 a     -0.0167 -0.0727 -1.08  -1.24 
#10    10 a      1.25   -0.326   1.61  -1.26 
## … with 20 more rows

或者如果我们需要使用nest_by,它会创建一个属性rowwise,所以,最好在应用

之前ungroup
out1 <- data %>%
    select(-t) %>% 
    nest_by(case) %>%
    ungroup %>%
    mutate(data = map(data, ~ .x %>% 
             mutate(out = slide_dbl(r1, .f = ~ prod(1 + .x) - 1, 
         .before = 5, .after = -1, complete = TRUE))))

-输出

out1
# A tibble: 3 x 2
#  case  data             
#  <chr> <list>           
#1 a     <tibble [10 × 4]>
#2 b     <tibble [10 × 4]>
#3 c     <tibble [10 × 4]>   

现在,我们unnest结构

 out1 %>% 
    unnest(data)
# A tibble: 30 x 5
#   case       r1      r2     r3    out
#   <chr>   <dbl>   <dbl>  <dbl>  <dbl>
# 1 a     -0.294  -0.164   1.33   0    
# 2 a      0.761   1.01    0.115 -0.294
# 3 a     -0.781  -0.499   0.290  0.243
# 4 a     -0.0732 -0.110   0.289 -0.728
# 5 a     -0.528   0.707   0.181 -0.748
# 6 a     -1.35   -0.411  -1.47  -0.881
# 7 a     -0.397  -1.28    0.172 -1.06 
# 8 a      1.68    0.956  -2.81  -1.02 
# 9 a     -0.0167 -0.0727 -1.08  -1.24 
#10 a      1.25   -0.326   1.61  -1.26 
# … with 20 more rows

数据

data <- tibble(t = rep(1:10, 3), 
               case = c(rep("a", 10), rep("b", 10), rep("c", 10)),
               r1 = rnorm(30),
               r2 = rnorm(30),
               r3 = rnorm(30))

我还有一个关于 slide_Dbl 函数的问题。我想查看其他滚动回归。我的数据已经固定为 8 个弱周,但是如果我想查看例如 16 或 24 周,我应该将 (before= ) 从 8 更改为 16 吗?我问的原因是我没有原始数据集,但它已经固定了 8 周,所以如果我添加 (before= ) 和额外的 8 会是 16 吗?

new8 <- new%>%mutate( across(
where(is.double),
function(x) slide_dbl(x, mean, na.rm = TRUE, .before = 8L) %>% lag()))

或者我应该把

new16 <- new%>%mutate(across(
where(is.double),
function(x) slide_dbl(x, mean, na.rm = TRUE, .before = 16L) %>% lag()))