rreturnpanelpanel-datacontinuous

R: Continuous return for panel data


I have the following data:

structure(list(`Product Name` = c("A", "A", "A", "B", "B", "B", 
"C", "C", "C"), Year = c(2018L, 2019L, 2020L, 2018L, 2019L, 2020L, 
2018L, 2019L, 2020L), Price = c(200L, 300L, 250L, 304L, 320L, 
103L, 203L, 203L, 402L)), class = "data.frame", row.names = c(NA, 
-9L))

Now I would like to compute the yearly continuous return based on the following formula: ln(Price_t /Price_(t-1)) where t represents the year.

I came across the following question: Calculating monthly returns based on Panel Data R However, I have a different formula for the return.

Could someone help me with the code? Thanks a lot in advance.


Solution

  • You can use the lag function from dplyr to get the previous period returns and group by product name to perform your calculation.

    library(dplyr)
    df <- df %>% 
        group_by(`Product Name`) %>% 
        mutate(return = log(Price / dplyr::lag(Price)))
    # A tibble: 9 x 4
    # Groups:   Product Name [3]
    # `Product Name`  Year Price  return
    # <chr>          <int> <int>   <dbl>
    #     1 A        2018   200 NA     
    #       A        2019   300  0.405 
    #       A        2020   250 -0.182 
    #       B        2018   304 NA     
    #       B        2019   320  0.0513
    #       B        2020   103 -1.13  
    #       C        2018   203 NA     
    #       C        2019   203  0     
    #       C        2020   402  0.683