【问题标题】:using lag results within the same mutate function dplyr在同一个变异函数 dplyr 中使用滞后结果
【发布时间】:2021-09-02 02:18:44
【问题描述】:

我想使用dplyr + lag 函数复制以下公式 R。代码工作到每组的第二行,然后给我 0s

预测 = lag(value,1)*(1-lag(Attrition)/52)

条件:

  1. 预测的第一个值应该为空,因为我们已经有了该值。
  2. 第二行根据 Attrition 和 Value 列的先前值进行计算。
  3. 第三行之前的值应分别从预测(不是值列)和损耗列中选取。

我从第 3 行开始得到 0。下面是我的复制代码。

data <- data %>% group_by(Patch) %>% mutate(id = row_number())
data <- data %>% group_by(Patch) %>% mutate(forecast = lag(Value,1)*(1-lag(Attrition,1)/52))

tbl_df(data)
# A tibble: 12 x 6
   Patch Week       Value Attrition    id forecast
   <chr> <date>     <dbl>     <dbl> <int>    <dbl>
 1 11P11 2021-06-14     2     0.075     1   NA    
 2 11P11 2021-06-21     0     0.075     2    2.00 
 3 11P11 2021-06-28     0     0.075     3    0    
 4 11P12 2021-06-14     3     0.075     1   NA    
 5 11P12 2021-06-21     0     0.075     2    3.00 
 6 11P12 2021-06-28     0     0.075     3    0    
 7 11P12 2021-07-05     0     0.075     4    0    
 8 11P13 2021-06-14     1     0.075     1   NA    
 9 11P13 2021-06-21     0     0.075     2    0.999
10 11P13 2021-06-28     0     0.075     3    0    
11 11P13 2021-07-05     0     0.075     4    0    
12 11P13 2021-07-12     0     0.075     5    0   


> dput(data)
structure(list(Patch = c("11P11", "11P11", "11P11", "11P12", 
"11P12", "11P12", "11P12", "11P13", "11P13", "11P13", "11P13", 
"11P13"), Week = structure(c(18792, 18799, 18806, 18792, 18799, 
18806, 18813, 18792, 18799, 18806, 18813, 18820), class = "Date"), 
    Value = c(2, 0, 0, 3, 0, 0, 0, 1, 0, 0, 0, 0), Attrition = c(0.075, 
    0.075, 0.075, 0.075, 0.075, 0.075, 0.075, 0.075, 0.075, 0.075, 
    0.075, 0.075), id = c(1L, 2L, 3L, 1L, 2L, 3L, 4L, 1L, 2L, 
    3L, 4L, 5L), forecast = c(NA, 1.99711538461538, 0, NA, 2.99567307692308, 
    0, 0, NA, 0.998557692307692, 0, 0, 0)), row.names = c(NA, 
-12L), groups = structure(list(Patch = c("11P11", "11P12", "11P13"
), .rows = structure(list(1:3, 4:7, 8:12), ptype = integer(0), class = c("vctrs_list_of", 
"vctrs_vctr", "list"))), row.names = c(NA, -3L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame")) 

【问题讨论】:

  • 请与dput(YOUR_DATASET) 共享您的数据集以复制粘贴格式或与dput(head(YOUR_DATASET)) 共享较小的样本。
  • 添加了 dput 部分
  • 安基特!没有任何答案符合您的目的吗?

标签: r dplyr data-analysis rolling-computation accumulate


【解决方案1】:

更新的解决方案

这是一个使用base::Reduce的简单解决方案:

do.call(rbind, lapply(split(df, df$Patch), function(x) {
  x$forecast <- c(NA, Reduce(function(a, b) {
    a * (1 - (x$Attrition[b]/52))
  }, 2:(nrow(x)-1), init = x$Value[1], accumulate = TRUE))
  x
}))

   Patch       Week Value Attrition id  forecast
1  11P11 2021-06-14     2     0.075  1        NA
2  11P11 2021-06-21     0     0.075  2 2.0000000
3  11P11 2021-06-28     0     0.075  3 1.9971154
4  11P12 2021-06-14     3     0.075  1        NA
5  11P12 2021-06-21     0     0.075  2 3.0000000
6  11P12 2021-06-28     0     0.075  3 2.9956731
7  11P12 2021-07-05     0     0.075  4 2.9913524
8  11P13 2021-06-14     1     0.075  1        NA
9  11P13 2021-06-21     0     0.075  2 1.0000000
10 11P13 2021-06-28     0     0.075  3 0.9985577
11 11P13 2021-07-05     0     0.075  4 0.9971175
12 11P13 2021-07-12     0     0.075  5 0.9956793

早期方法

您也可以使用以下方法。为此,我首先将您的公式与 mutate 应用于您的数据集,以获得我的forecast 系列的第一个值。然后,我将包含NA 值的每个组的第一行切片为forecast。之后,我使用accumulate 函数使用第一个forecast 值作为.init 参数的值来计算您想要的系列。然后我将结果数据集与包含NA 值的数据集绑定:

library(dplyr)
library(purrr)

df %>%
  group_by(Patch) %>%
  mutate(forecast = lag(Value)*(1-(lag(Attrition)/52))) %>%
  filter(between(row_number(), 2, n())) %>%
  mutate(forecast = accumulate(Attrition[-1], .init = forecast[1], ~ ..1 * (1-(..2/52)))) %>%
  bind_rows(df %>% group_by(Patch) %>%
              mutate(forecast = lag(Value)*(1-(lag(Attrition)/52))) %>%
              slice_head()) %>%
  ungroup() %>%
  arrange(Patch, Week)

# A tibble: 12 x 6
   Patch Week       Value Attrition    id forecast
   <chr> <date>     <dbl>     <dbl> <int>    <dbl>
 1 11P11 2021-06-14     2     0.075     1   NA    
 2 11P11 2021-06-21     0     0.075     2    2.00 
 3 11P11 2021-06-28     0     0.075     3    1.99 
 4 11P12 2021-06-14     3     0.075     1   NA    
 5 11P12 2021-06-21     0     0.075     2    3.00 
 6 11P12 2021-06-28     0     0.075     3    2.99 
 7 11P12 2021-07-05     0     0.075     4    2.99 
 8 11P13 2021-06-14     1     0.075     1   NA    
 9 11P13 2021-06-21     0     0.075     2    0.999
10 11P13 2021-06-28     0     0.075     3    0.997
11 11P13 2021-07-05     0     0.075     4    0.996
12 11P13 2021-07-12     0     0.075     5    0.994

【讨论】:

  • 非常酷!我试图让accumulate()为此工作。
  • 非常感谢亲爱的@ktiu。我看了一眼您的解决方案,并试图想出一种不同的方法并想出了这个。尽管听起来可能很奇怪,但它可以工作,因为第一行的forecast 值始终是NA。您的解决方案非常微妙和技术性。我也是你的编码风格的忠实粉丝。
【解决方案2】:

这方面的棘手之处在于您需要连续构建forecast 变量,这就是为什么它在标准的mutate() 调用中不起作用的原因。

这是我使用purrrmap()reduce() 进行数据整合的方法:

library(tidyverse)

data %>%
  mutate(forecast = NA) %>%
  split(~ Patch) %>%
  map(~ .x %>%
          pmap(~ tibble(...)) %>%
          reduce(\(.x, .y) {
            prev <- slice_tail(.x)
            base_value <- ifelse(prev$Value != 0, prev$Value, prev$forecast)
            bind_rows(.x,
                      mutate(.y,
                             forecast = base_value * 1 - prev$Attrition / 5))
          })) %>%
  reduce(bind_rows)

返回:

# A tibble: 12 x 6
   Patch Week       Value Attrition    id forecast
   <chr> <date>     <dbl>     <dbl> <int>    <dbl>
 1 11P11 2021-06-14     2     0.075     1   NA
 2 11P11 2021-06-21     0     0.075     2    1.98
 3 11P11 2021-06-28     0     0.075     3    1.97
 4 11P12 2021-06-14     3     0.075     1   NA
 5 11P12 2021-06-21     0     0.075     2    2.98
 6 11P12 2021-06-28     0     0.075     3    2.97
 7 11P12 2021-07-05     0     0.075     4    2.95
 8 11P13 2021-06-14     1     0.075     1   NA
 9 11P13 2021-06-21     0     0.075     2    0.985
10 11P13 2021-06-28     0     0.075     3    0.97
11 11P13 2021-07-05     0     0.075     4    0.955
12 11P13 2021-07-12     0     0.075     5    0.94

使用的数据:

data <- structure(list(Patch = c("11P11", "11P11", "11P11", "11P12", "11P12", "11P12", "11P12", "11P13", "11P13", "11P13", "11P13", "11P13"), Week = structure(c(18792, 18799, 18806, 18792, 18799, 18806, 18813, 18792, 18799, 18806, 18813, 18820), class = "Date"), Value = c(2, 0, 0, 3, 0, 0, 0, 1, 0, 0, 0, 0), Attrition = c(0.075, 0.075, 0.075, 0.075, 0.075, 0.075, 0.075, 0.075, 0.075, 0.075, 0.075, 0.075), id = c(1L, 2L, 3L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 5L), forecast = c(NA, 1.99711538461538, 0, NA, 2.99567307692308, 0, 0, NA, 0.998557692307692, 0, 0, 0)), row.names = c(NA, -12L), groups = structure(list(Patch = c("11P11", "11P12", "11P13"), .rows = structure(list(1:3, 4:7, 8:12), ptype = integer(0), class = c("vctrs_list_of", "vctrs_vctr", "list"))), row.names = c(NA, -3L), class = c("tbl_df", "tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", "tbl_df", "tbl", "data.frame")) 

【讨论】:

    【解决方案3】:

    如果我对您的理解正确,也许您只需要来自purrraccumulate(您不需要lag 值,而是需要accumulated 值)-

    • 我根据给定的公式计算了FORECAST
    • 仅在参数中使用attrition,因为我们只需要Value 的第一个值,我们可以通过.init 提供给accumulate
    • 现在结果向量将比期望的长度多一个长度,因此去掉了它的最后一个 -n() 值。
    • 但您的进一步要求是将第一个结果作为 NA,因此通过将累加子集为 [-c(1, n()] 来剥离另一个值的结果,即第一个值
    • 现在将结果与开头的NA 连接起来
    library(tidyverse)
    
    df %>% group_by(Patch) %>%
      mutate(FORECAST = c(NA, accumulate(Attrition, .init = first(Value), ~ .x * (1 - .y/52))[-c(1, n())]))
    
    #> # A tibble: 12 x 7
    #> # Groups:   Patch [3]
    #>    Patch Week       Value Attrition    id forecast FORECAST
    #>    <chr> <date>     <dbl>     <dbl> <int>    <dbl>    <dbl>
    #>  1 11P11 2021-06-14     2     0.075     1   NA       NA    
    #>  2 11P11 2021-06-21     0     0.075     2    2.00     2.00 
    #>  3 11P11 2021-06-28     0     0.075     3    0        1.99 
    #>  4 11P12 2021-06-14     3     0.075     1   NA       NA    
    #>  5 11P12 2021-06-21     0     0.075     2    3.00     3.00 
    #>  6 11P12 2021-06-28     0     0.075     3    0        2.99 
    #>  7 11P12 2021-07-05     0     0.075     4    0        2.98 
    #>  8 11P13 2021-06-14     1     0.075     1   NA       NA    
    #>  9 11P13 2021-06-21     0     0.075     2    0.999    0.999
    #> 10 11P13 2021-06-28     0     0.075     3    0        0.997
    #> 11 11P13 2021-07-05     0     0.075     4    0        0.996
    #> 12 11P13 2021-07-12     0     0.075     5    0        0.993
    

    reprex package (v2.0.0) 于 2021-06-18 创建

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-21
      • 1970-01-01
      • 1970-01-01
      • 2015-02-16
      • 1970-01-01
      • 2020-12-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多