Introduction

Simple example of transforming growth by volume by quarter over 5 years to a cumulative growth rate and value. Useful for CAGR or long-term growth rates.

Load the Libraries

#load Libraries
suppressWarnings(suppressMessages(library(dplyr))) #for the mutate and pipes
suppressWarnings(suppressMessages(library(zoo))) #for the cumprod & cumsum functions
suppressWarnings(suppressMessages(library(ggplot2)))#for plotting

Create a Data Frame

# Make fake dataset and group in 
#  Makes 2 models for 5 years divided in 4 qtrs
#  Uses a 100 "base" volume that grows 10% quarter over quarter
term_num = 40
year_num = 5
qtr_num =4
base_investment = 1000
model1_term_growth = 0.01
model1_name = "1% Growth"
model2_term_growth = 0.02
model2_name = "2% Growth"


model_id = as.factor(rep(1:2, each = term_num))
model_tag = as.factor(rep(c(model1_name,model2_name),each=term_num))
yr= rep(rep(1:year_num, each=qtr_num),length.out=term_num)
qtr = rep(rep(1:qtr_num, length.out=qtr_num*year_num),length.out=term_num)
base_vol = rep(base_investment, length.out=term_num) #base_vol = sample(c(100:200),20,replace=TRUE)
growth = c(rep(model1_term_growth,each=term_num),rep(model2_term_growth,each=term_num))
term=c(1:term_num,1:term_num)
df = data.frame(model_id,model_tag,term,yr,qtr,base_vol,growth)

Compute the Cumulative Growth

#use the mutate function to do various cumulative functions and add them as a column
df_sum <- df %>% group_by(model_id) %>% mutate(sum_cum = cumsum(growth)) #not really important, just an easy example
df_percent_cum <- df_sum %>% group_by(model_id) %>% mutate(percent_cum = cumprod(1+growth)) #cumulative growth (percent)
df_volume_cum <- df_percent_cum %>% group_by(model_id) %>% mutate(volume_cum = percent_cum*base_vol) #cumulative growth (volume)
tail(df_volume_cum,n=5)
## # A tibble: 5 x 10
## # Groups:   model_id [1]
##   model_id model_tag  term    yr   qtr base_vol growth sum_cum percent_cum
##     <fctr>    <fctr> <int> <int> <int>    <dbl>  <dbl>   <dbl>       <dbl>
## 1        2 2% Growth    36     4     4     1000   0.02    0.72    2.039887
## 2        2 2% Growth    37     5     1     1000   0.02    0.74    2.080685
## 3        2 2% Growth    38     5     2     1000   0.02    0.76    2.122299
## 4        2 2% Growth    39     5     3     1000   0.02    0.78    2.164745
## 5        2 2% Growth    40     5     4     1000   0.02    0.80    2.208040
## # ... with 1 more variables: volume_cum <dbl>

Final Value

## [1] "Final Value of Model 1 is: $ 1488.86"
## [1] "Final Value of Model 2 is: $ 2208.04"
## [1] "Difference of in final value between the two models is: $ 719.18"

Quick Visualization from GGplot

ggplot(data=df_volume_cum, aes(x=term, y=volume_cum, group=model_tag, color=model_tag)) + geom_line() + geom_point() + ggtitle("Growth of a theorectical investment of $1,000 \n experiencing 1% vs 2% growth over 40 terms") + xlab("Term Number") + ylab("Value of Investment ($)") + labs(color='Model')