<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/ltv-prediction-for-a-recurring-subscription-with-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://analyzecore.com/2018/09/19/ltv-prediction-for-a-recurring-subscription-with-r/"> R language – AnalyzeCore by Sergey Bryl' – data is beautiful, data is a story</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><span>Customers lifetime value (LTV or CLV) is one of the cornerstones of product analytics because we need to make a lot of decisions for which the LTV is a necessary or at least very significant factor. In this article, we will focus on products/services/applications with <strong>recurring subscription payments</strong></span><span><span></span></span><span>Customers lifetime value (LTV or CLV) is one of the cornerstones of product analytics because we need to make a lot of decisions for which the LTV is a necessary or at least very significant factor. In this article, we will focus on products/services/applications with <strong>recurring subscription payments</strong>.</span></p>
<p><span>Predicting LTV is a common issue for a new, recently launched product/service/application when we don’t have a lot of historical data but want to calculate LTV as soon as possible. Even though we may have a lot of historical data on customer payments for a product that is active for years, we can’t really trust earlier stats since the churn curve and LTV can differ significantly between new customers and the current ones due to a variety of reasons.</span></p>
<p><span>Therefore, regardless of whether our product is new or “old”, we attract new subscribers and want to estimate what revenue they will generate during their lifetimes for business decision-making.</span></p>
<p><span>This topic is closely connected to the Cohort Analysis and if you are not familiar with the concept, I recommend that you read about it and look at other articles I wrote earlier on <a rel="nofollow" href="https://analyzecore.com/category/cohort-analysis/" target="_blank">this blog</a>.</span></p>
<p><span>As usual, in the article, we will review an example of LTV projection using the R language although this approach can be implemented even in MS Excel.</span></p>
<p><span>Ok, let’s start. In order to predict the average LTV for the product/service/application with a constant subscription payment, it suffices to know how many subscribers will churn (or be retained) at the end of each subscription period. </span><span>I’ve collected four examples: two from my practice and two from research that demonstrates the effectiveness of the approach. The above examples refer to different businesses and show both monthly and annual subscriptions, but for convenience, they are all monthly ones.</span></p>
<p><strong title="click to expand R code" tabindex="0">click to expand R code</strong></p>
<div>
<pre>
library(tidyverse)
library(reshape2)

# retention rate data
df_ret &lt;- data.frame(month_lt = c(0:7),
case01 = c(1, .531, .452, .423, .394, .375, .356, .346),
case02 = c(1, .869, .743, .653, .593, .551, .517, .491),
case03 = c(1, .677, .562, .486, .412, .359, .332, .310),
case04 = c(1, .631, .468, .382, .326, .289, .262, .241)
) %>%
melt(., id.vars = c('month_lt'), variable.name = 'example', value.name = 'retention_rate')

ggplot(df_ret, aes(x = month_lt, y = retention_rate, group = example, color = example)) +
theme_minimal() +
facet_wrap(~ example) +
scale_color_manual(values = c('#4e79a7', '#f28e2b', '#e15759', '#76b7b2')) +
geom_line() +
geom_point() +
theme(plot.title = element_text(size = 20, face = 'bold', vjust = 2, hjust = 0.5),
axis.text.x = element_text(size = 8, hjust = 0.5, vjust = .5, face = 'plain'),
strip.text = element_text(face = 'bold', size = 12)) +
ggtitle('Retention Rate')
</pre>
</div>
<p> </p>
<p><span>The initial data is as follows:</span></p>
<p><a rel="nofollow" href="https://i0.wp.com/analyzecore.com/wp-content/uploads/2018/09/plot_0_table1_1.png?ssl=1" target="_blank"><img sizes="(max-width: 324px) 100vw, 324px" src="https://i0.wp.com/analyzecore.com/wp-content/uploads/2018/09/plot_0_table1_1.png?resize=324%2C310&ssl=1" alt="" width="324" class="wp-image-2481" data-recalc-dims="1" height="310"></a></p>
<p><span><em>month_lt</em> – month (or subscription period) of customer’s lifetime,</span></p>
<p><span><em>example</em> – </span><span>the name of the example,</span></p>
<p><span><em>retention_rate</em> – </span><span>the percentage of subscribers who have maintained the subscription.</span></p>
<p><span>Next, the visualization of four retention curves. Looks very familiar, doesn’t it?</span></p>
<p><a rel="nofollow" href="https://i2.wp.com/analyzecore.com/wp-content/uploads/2018/09/plot_01_retention_viz_1.png?ssl=1" target="_blank"><img sizes="(max-width: 800px) 100vw, 800px" src="https://i2.wp.com/analyzecore.com/wp-content/uploads/2018/09/plot_01_retention_viz_1.png?resize=800%2C533&ssl=1" alt="" width="450" class="wp-image-2473" data-recalc-dims="1"></a></p>
<p><span>Let’s consider the approach for predicting LTV. As I noted earlier, when we deal with the constant subscription payment, we can predict average subscribers’ LTV by projecting a retention curve. In other words, we need to continue “drawing” the curve that starts with factual points we have for the future periods.</span></p>
<p><span>However, instead of using the traditional approach (i.e. a regression model), we can use an alternative </span><a rel="nofollow" href="https://faculty.wharton.upenn.edu/wp-content/uploads/2012/04/Fader_hardie_jim_07.pdf" target="_blank">probabilistic method that Peter Fader and Bruce Hardie suggested</a>.</p>
<p><span>The authors demonstrated the concept which, despite having simplified assumptions, shows excellent results in practice. There are a lot of materials on the Internet about the method, so we won’t go into details, we will just highlight the core ideas.</span></p>
<p><span>We assume that each subscriber has a certain constant churn probability and after the end of each period, they “flip a coin to decide whether to churn or to maintain the subscription”, where the probability works. Since the probability of retaining subscribers in each period is calculated by multiplying the previous probabilities, the subscriber’s lifetime is characterized by the <strong>Shifted Geometric Distribution</strong>.</span></p>
<p><span>Despite the fact that the specific probability for each subscriber is unknown to us, we assume that it is characterized by the <strong>Beta Distribution</strong>, which fits the goal ideally given two facts. Firstly, it has an interval from 0 to 1 (as probability). Secondly, it is flexible. Namely, we can change (tune, in our case) the distribution form by changing two parameters alpha and beta.</span></p>
<p><span>By combining these two assumptions through a mathematical apparatus, the authors came to the idea that subscriber’ lifetimes can be characterized by the <strong>Shifted-Beta-Geometric (sBG) distribution</strong> (which in turn is determined by two parameters: alpha and beta). However, alpha and beta are unknown to us. We can compute them by maximizing the likelihood estimation that they fit the observed factual data of subscribers’ retention and, accordingly, keep our retention curve for the future periods.</span></p>
<p><span>Let’s check the accuracy of the predictions on the available data. We will assume that we only know the first month’s retention, then the first and the second months, and so on until we predict all subsequent periods until the end of the 7th month. I wrote a simple loop for this, which you can apply to each example:</span></p>
<p><strong title="click to expand R code" tabindex="0">click to expand R code</strong></p>
<div>
<pre>
# functions for sBG distribution
churnBG &lt;- Vectorize(function(alpha, beta, period) {
t1 = alpha / (alpha + beta)
result = t1
if (period > 1) {
result = churnBG(alpha, beta, period - 1) * (beta + period - 2) / (alpha + beta + period - 1)
}
return(result)
}, vectorize.args = c(&quot;period&quot;))

survivalBG &lt;- Vectorize(function(alpha, beta, period) {
t1 = 1 - churnBG(alpha, beta, 1)
result = t1
if(period > 1){
result = survivalBG(alpha, beta, period - 1) - churnBG(alpha, beta, period)
}
return(result)
}, vectorize.args = c(&quot;period&quot;))

MLL &lt;- function(alphabeta) {
if(length(activeCust) != length(lostCust)) {
stop(&quot;Variables activeCust and lostCust have different lengths: &quot;,
length(activeCust), &quot; and &quot;, length(lostCust), &quot;.&quot;)
}
t = length(activeCust) # number of periods
alpha = alphabeta[1]
beta = alphabeta[2]
return(-as.numeric(
sum(lostCust * log(churnBG(alpha, beta, 1:t))) +
activeCust[t]*log(survivalBG(alpha, beta, t))
))
}

df_ret &lt;- df_ret %>%
group_by(example) %>%
mutate(activeCust = 1000 * retention_rate,
lostCust = lag(activeCust) - activeCust,
lostCust = ifelse(is.na(lostCust), 0, lostCust)) %>%
ungroup()

ret_preds01 &lt;- vector('list', 7)
for (i in c(1:7)) {

df_ret_filt &lt;- df_ret %>%
filter(between(month_lt, 1, i) == TRUE &amp; example == 'case01')

activeCust &lt;- c(df_ret_filt$activeCust)
lostCust &lt;- c(df_ret_filt$lostCust)

opt &lt;- optim(c(1, 1), MLL)
retention_pred &lt;- round(c(1, survivalBG(alpha = opt$par[1], beta = opt$par[2], c(1:7))), 3)

df_pred &lt;- data.frame(month_lt = c(0:7),
example = 'case01',
fact_months = i,
retention_pred = retention_pred)
ret_preds01[[i]] &lt;- df_pred
}

ret_preds01 &lt;- as.data.frame(do.call('rbind', ret_preds01))
</pre>
</div>
<p> </p>
<p><span>The visualization of the predicted retention curves and mean average percentage error (MAPE) is the following:</span></p>
<p><a rel="nofollow" href="https://i0.wp.com/analyzecore.com/wp-content/uploads/2018/09/plot_02_retention_projections_1.png?ssl=1" target="_blank"><img sizes="(max-width: 800px) 100vw, 800px" src="https://i0.wp.com/analyzecore.com/wp-content/uploads/2018/09/plot_02_retention_projections_1.png?resize=800%2C533&ssl=1" alt="" width="450" class="wp-image-2474" data-recalc-dims="1"></a><br>
<a rel="nofollow" href="https://i1.wp.com/analyzecore.com/wp-content/uploads/2018/09/plot_03_retention_mape_1.png?ssl=1" target="_blank"><img sizes="(max-width: 800px) 100vw, 800px" src="https://i1.wp.com/analyzecore.com/wp-content/uploads/2018/09/plot_03_retention_mape_1.png?resize=800%2C533&ssl=1" alt="" width="450" class="wp-image-2475" data-recalc-dims="1"></a></p>
<p><span>As you can see, in most cases, the accuracy of the predictions is very high even for the first month’s data only and it significantly improves when adding historical periods.</span></p>
<p><span>Now, in order to obtain the average LTV prediction, we need to multiply the retention rate by the subscription price and calculate the cumulative amount for the required period. Let’s suppose we want to calculate the average LTV for <em>case03</em> based on two historical months with a forecast horizon of 24 months and a subscription price of $1. We can do this using the following code:</span></p>
<p><strong title="click to expand R code" tabindex="0">click to expand R code</strong></p>
<div>
<pre>
### LTV prediction ###
df_ltv_03 &lt;- df_ret %>%
filter(between(month_lt, 1, 2) == TRUE &amp; example == 'case03')

activeCust &lt;- c(df_ltv_03$activeCust)
lostCust &lt;- c(df_ltv_03$lostCust)

opt &lt;- optim(c(1, 1), MLL)
retention_pred &lt;- round(c(survivalBG(alpha = opt$par[1], beta = opt$par[2], c(3:24))), 3)

df_pred &lt;- data.frame(month_lt = c(3:24),
retention_pred = retention_pred)

df_ltv_03 &lt;- df_ret %>%
filter(between(month_lt, 0, 2) == TRUE &amp; example == 'case03') %>%
select(month_lt, retention_rate) %>%
bind_rows(., df_pred) %>%
mutate(retention_rate_calc = ifelse(is.na(retention_rate), retention_pred, retention_rate),
ltv_monthly = retention_rate_calc * 1,
ltv_cum = round(cumsum(ltv_monthly), 2))
</pre>
</div>
<p> </p>
<p>We’ve obtained the average LTV of $9.33. Note that we’ve used actual data for the observed periods (from 0 to 2nd months) and the predicted retention for the future periods (from 3rd to 24th months).</p>
<p>A couple of tips from the practice:</p>
<ul>
<li>In order to get a retention or LTV prediction as soon as possible, for example, for a monthly subscription, you can create weekly/decadal (or even daily, if the number of subscribers is large enough) cohorts. Therefore, you can obtain the prediction after the month and the week (or decade or day) period instead of a two-month one.</li>
<li>In order to improve the accuracy of predictions, it makes sense to create more specific cohorts. We can take into account some additional subscribers’ characteristics which can affect the churn. For instance, when launching a mobile application, such characteristics can be the country of the customer, the subscription price, the type of device, etc. Thus, the cohort may look like a purchase-date + country + price + device.</li>
<li>Update the prediction once you have new factual data. This helps to increase the accuracy of the model.</li>
</ul>
<p>Other useful links:</p>
<ul>
<li><a rel="nofollow" href="http://www.brucehardie.com/talks.html" target="_blank">B. Hardie’s Tutorial &amp; Excel workbook</a></li>
<li><a rel="nofollow" href="https://rpubs.com/Koba/Customer-Retention-sBG" target="_blank">The example of implementing the R functions that were mentioned in the article</a></li>
<li><a rel="nofollow" href="http://danielweitzenfeld.github.io/passtheroc/blog/2015/01/19/s-b-g/" target="_blank">The demonstration of an alternative approach to defining model parameters</a></li>
</ul>
<p>The post <a rel="nofollow" href="https://analyzecore.com/2018/09/19/ltv-prediction-for-a-recurring-subscription-with-r/" target="_blank">LTV prediction for a recurring subscription with R</a> appeared first on <a rel="nofollow" href="https://analyzecore.com/" target="_blank">AnalyzeCore by Sergey Bryl' – data is beautiful, data is a story</a>.</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://analyzecore.com/2018/09/19/ltv-prediction-for-a-recurring-subscription-with-r/"> R language – AnalyzeCore by Sergey Bryl' – data is beautiful, data is a story</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/3C2cC7NdQVc" height="1"></a></div>