在R中用ID计算复合回报

时间:2016-07-11 17:13:56

标签: r dplyr summary split-apply-combine

我正在尝试计算一个CAGR值,定义为(结束/开始)^(1 /年数)-1。

我有一个df,其中包含“Stock”,“date”,“Annual.Growth.Rate”列。要快速注意:我试图使用滞后函数来执行此操作,但是,我无法在每个股票的开头更改递归公式。看看dput会更有意义:

structure(list(Stock = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L, 2L), .Label = c("A", "B"), class = "factor"), 
    date = structure(c(6L, 2L, 3L, 4L, 5L, 1L, 12L, 8L, 9L, 10L, 
    11L, 7L), .Label = c("3/28/16", "3/29/12", "3/29/13", "3/29/14", 
    "3/29/15", "3/30/11", "6/28/16", "6/29/12", "6/29/13", "6/29/14", 
    "6/29/15", "6/30/11"), class = "factor"), Annual.Growth.Rate = c(0.1, 
    0.2, 0.1, 0.1, 0.1, 0.1, 0.3, 0.2, 0.14, 0.14, 0.14, 0.14
    ), Growth = c(110, 132, 145.2, 159.72, 175.692, 193.2612, 
    130, 156, 177.84, 202.7376, 231.120864, 263.477785), CAGR = c(0.098479605, 
    0.098479605, 0.098479605, 0.098479605, 0.098479605, 0.098479605, 
    0.125, 0.125, 0.125, 0.125, 0.125, 0.125)), .Names = c("Stock", 
"date", "Annual.Growth.Rate", "Growth.on.100", "CAGR"), class = "data.frame", row.names = c(NA, 
-12L)) 

这是预期的输出。在有股票,日期和增长之前。 100的增长并不是以前的“滞后”。由于第一个可用日期乘以给定的启动器,在这种情况下为100,(1 + .1)* 100,然后随后的增长值是未来值(110)*下一个增长率。我可以弄清楚如何使用dplyr进行CAGR,但我真的坚持100增长。

1 个答案:

答案 0 :(得分:1)

You could use cumprod in a mutate. Also the starting 100 value is arbitrary. It is all a product. You can calculate the rest of the product then multiply by the starter.

starter <- 100
my.data <- data.frame(stock=c('a','a','a','b','b','b'), growth = c(.1,.2,.1,.1,.1,.1), date = c(1,2,3,1,2,3)) #example Data
my.data
my.data %>%
  group_by(stock) %>%
  mutate(growth.unit =  order_by(date,cumprod(1+growth)),
         growth = growth.unit*starter) -> new.data