【问题标题】:Rolling window with slide_dbl() on grouped data分组数据上带有 slide_dbl() 的滚动窗口
【发布时间】:2021-04-02 08:43:27
【问题描述】:

这是对以下问题的扩展:Rolling window slider::slide() with 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())。任何人都可以帮忙吗?提前致谢!

【问题讨论】:

  • 在链接中,示例'data'列名不同
  • 这是我自己的数据。列名会影响代码的“功能”吗?
  • 事实并非如此。正如你提到的那个链接,我认为数据来自那个帖子
  • 您可以查看以下解决方案。希望对你有帮助

标签: r slide dplyr


【解决方案1】:

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

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))

【讨论】:

  • 有效!非常感谢您的快速帮助,祝您有愉快的一天:))
【解决方案2】:

我还有一个关于 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()))
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-03
    • 1970-01-01
    • 2018-07-22
    • 2013-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多