【问题标题】:R Panel data: Create new variable based on ifelse() statement and previous rowR面板数据:根据ifelse()语句和上一行创建新变量
【发布时间】:2021-02-09 12:28:37
【问题描述】:

我的问题是指以下(简化的)面板数据,我想为此创建某种xrd_stock

#Setup data
library(tidyverse)

firm_id <- c(rep(1, 5), rep(2, 3), rep(3, 4))
firm_name <- c(rep("Cosco", 5), rep("Apple", 3), rep("BP", 4))
fyear <- c(seq(2000, 2004, 1), seq(2003, 2005, 1), seq(2005, 2008, 1))
xrd <- c(49,93,121,84,37,197,36,154,104,116,6,21)
df <- data.frame(firm_id, firm_name, fyear, xrd)

#Define variables
growth = 0.08
depr = 0.15

对于名为xrd_stock 的新变量,我想应用以下机制:

  1. 每个firm_id应该单独处理:group_by(firm_id)
  2. 如果 fyear 最小,计算 xrd_stock 为:xrd/(growth + depr)
  3. 否则,将 xrd_stock 计算为:xrd + (1-depr) * [xrd_stock from previous row]

使用以下代码,我已经成功完成了第 1 步和第 2 步以及第 3 步的部分内容。

df2 <- df %>%
  ungroup() %>%
  group_by(firm_id) %>%
  arrange(firm_id, fyear, decreasing = TRUE) %>% #Ensure that data is arranged w/ in asc(fyear) order; not required in this specific example as df is already in correct order
  mutate(xrd_stock = ifelse(fyear == min(fyear), xrd/(growth + depr), xrd + (1-depr)*lag(xrd_stock))))

函数的else部分出现困难,使得R返回:

Error: Problem with `mutate()` input `xrd_stock`.
x object 'xrd_stock' not found
i Input `xrd_stock` is `ifelse(...)`.
i The error occured in group 1: firm_id = 1.
Run `rlang::last_error()` to see where the error occurred.

从这条错误消息中,我了解到 R 不能引用上一行中刚刚创建的 xrd_stock(在考虑/假设 R 没有严格从上到下工作时是合乎逻辑的);然而,当简单地将9 放入else 部分时,我上面的代码运行没有任何错误。

谁能帮我解决这个问题,以便最终结果如下所示。如果需要,我非常乐意回答其他问题。非常感谢大家提前看我的问题:-)

目标结果(Excel 计算):

id  name    fyear   xrd xrd_stock   Calculation for xrd_stock
1   Cosco   2000    49  213         =49/(0.08+0.15)
1   Cosco   2001    93  274         =93+(1-0.15)*213
1   Cosco   2002    121 354         …
1   Cosco   2003    84  385         …
1   Cosco   2004    37  364         …
2   Apple   2003    197 857         =197/(0.08+0.15)
2   Apple   2004    36  764         =36+(1-0.15)*857
2   Apple   2005    154 803         …
3   BP      2005    104 452         …
3   BP      2006    116 500         …
3   BP      2007    6   431         …
3   BP      2008    21  388         …

【问题讨论】:

    标签: r lag panel-data


    【解决方案1】:

    arrangefyear的数据,所以最小年份总是第一行,然后你可以用accumulate来计算。

    library(dplyr)
    df %>%
      arrange(firm_id, fyear) %>%
      group_by(firm_id) %>%
      mutate(xrd_stock = purrr::accumulate(xrd[-1], ~.y + (1-depr) * .x, 
                                    .init = first(xrd)/(growth + depr)))
    
    #   firm_id firm_name fyear   xrd xrd_stock
    #     <dbl> <chr>     <dbl> <dbl>     <dbl>
    # 1       1 Cosco      2000    49      213.
    # 2       1 Cosco      2001    93      274.
    # 3       1 Cosco      2002   121      354.
    # 4       1 Cosco      2003    84      385.
    # 5       1 Cosco      2004    37      364.
    # 6       2 Apple      2003   197      857.
    # 7       2 Apple      2004    36      764.
    # 8       2 Apple      2005   154      803.
    # 9       3 BP         2005   104      452.
    #10       3 BP         2006   116      500.
    #11       3 BP         2007     6      431.
    #12       3 BP         2008    21      388.
    

    【讨论】:

    • 工作出色! 'accumulate()' 实际上对我来说是新的,所以我将自己检查语法和参数!非常感谢这个及时的解决方案!
    猜你喜欢
    • 2019-12-03
    • 2016-06-27
    • 2022-12-03
    • 1970-01-01
    • 2021-02-02
    • 1970-01-01
    • 2021-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多