2

My question refers to the following (simplified) panel data, for which I would like to create some sort of 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

For a new variable called xrd_stock I'd like to apply the following mechanics:

  1. each firm_id should be handled separately: group_by(firm_id)
  2. where fyear is at minimum, calculate xrd_stock as: xrd/(growth + depr)
  3. otherwise, calculate xrd_stock as: xrd + (1-depr) * [xrd_stock from previous row]

With the following code, I already succeeded with step 1. and 2. and parts of step 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))))

Difficulties occur in the else part of the function, such that R returns:

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.

From this error message, I understand that R cannot refer to the just created xrd_stock in the previous row (logical when considering/assuming that R is not strictly working from top to bottom); however, when simply putting a 9 in the else part, my above code runs without any errors.

Can anyone help me with this problem so that results look eventually as shown below. I am more than happy to answer additional questions if required. Thank you very much to everyone in advance, who looks at my question :-)

Target results (Excel-calculated):

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         …
chuesker
  • 25
  • 2

1 Answers1

0

arrange the data by fyear so minimum year is always the 1st row, you can then use accumulate to calculate.

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.
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Worked excellent! ´accumulate()` is actually new to me, so I'll check out the syntax and arguments on my own! Thanks a lot for this prompt solution! – chuesker Oct 27 '20 at 09:03