3. Applying the customer model
Stefan Musch
2025-10-17
Source:vignettes/appraise_customer_model.Rmd
appraise_customer_model.RmdPreparing your data
As mentioned in Step 2. Building the Appraise Datasets, we’re going to need three main ingredients to run the Appraise customer model. These ingredients are:
- A validated Elog dataset:
g6tr.appraise::elog_validated - a validated items and returns:
g6tr.appraise::items_and_returns_validated - A configuration list created by
appraise_configure()
set.seed(123)
config <- appraise_configure(sample = TRUE,
unified_horizon = 5,
cpa = c(25, 50, 75, 100, 125, 150, 200),
cm = c(.2, .3, .432, .5),
currency = '$',
pred_periods = c(1, 2, 3, 4, 5),
n_forecast = 5)With the setup above, we’re setting the horizon of all customers to
5 years. We’re also predicting 5 years of data as our
forecasts. Note that we need to set this now, in order to use it in our
fit_forecast_model() later, which we’ll explain in Step 3. Applying the
forecasts model.
Transforming elog into a Customer by Statistic (CBS) dataset
The first thing we’ll need to do is to summarize our Elog to one row per customer, also known as the CBS. When the data is validated, and the configuration is set up, we’ve created a convenient wrapper to straight up calculated the CBS, and additionally, converted the results to a list we can use in our Stan model.
cbs <-elog_to_stan(elog = elog_validated$elog,
config,
items_and_returns = items_and_returns_validated$items_and_returns)
[32mElog conversion finished. Ready to start modeling.
[39m
The cbs list has two elements.
CBS
The first element is the actual CBS data frame. Most of this conversion is done by the BTYDPlus::elog2cbs() function, which is explained in more detail in this vignette.
The data frame looks as follows:
[38;5;246m# A tibble: 6 × 11
[39m
cust x t.x litt sales sales.x first T.cal items returns
[3m
[38;5;246m<chr>
[39m
[23m
[3m
[38;5;246m<dbl>
[39m
[23m
[3m
[38;5;246m<dbl>
[39m
[23m
[3m
[38;5;246m<dbl>
[39m
[23m
[3m
[38;5;246m<dbl>
[39m
[23m
[3m
[38;5;246m<dbl>
[39m
[23m
[3m
[38;5;246m<IDate>
[39m
[23m
[3m
[38;5;246m<dbl>
[39m
[23m
[3m
[38;5;246m<int>
[39m
[23m
[3m
[38;5;246m<int>
[39m
[23m
[38;5;250m1
[39m 001000000000… 2 14.3 3.82 205. 130. 2018-04-25 162. 3 0
[38;5;250m2
[39m 001000000000… 0 0 0 59.5 0 2019-07-04 100 3 0
[38;5;250m3
[39m 001000000000… 18 139. 23.8
[4m4
[24m072.
[4m3
[24m835. 2018-09-10 142. 72 0
[38;5;250m4
[39m 001000000000… 1 57.9 4.06 187. 150. 2018-10-13 138. 2 0
[38;5;250m5
[39m 001000000000… 1 48.1 3.87 401. 291. 2018-08-13 146. 5 0
[38;5;250m6
[39m 001000000000… 0 0 0 85.3 0 2021-05-17 2.43 1 0
[38;5;246m# ℹ 1 more variable: cust_to_adjust <lgl>
[39m
And here is an explanation of each of the variables:
- cust: Customer ID
- x: Number of repeat purchases (i.e., frequency)
- t.x: Time of the last recorded transaction (i.e., recency)
- litt: Sum over logarithmic intertransaction times (required for estimating regularity)
- sales: Total sales of the customer
- sales.x: Sales of the last recorded transaction
- T.cal: The duration between the first transaction and the end of the calibration period
Note, that transactions with identical cust and
date field are counted as a single transaction, but with
sales being summed up.
STAN model data
The second element of the list is a list itself, called
stan. This list uses the CBS and
config data to generate the inputs required for the model
to work. Most of the names of the list (which can be seen below) are
self explanatory, or explained in the previous sections. Some though,
are a little less intuitive:
names(cbs$stan) [1] "n_custs" "t_cal" "date_last_tx"
[4] "n_tx" "litt" "n_items_purchased"
[7] "n_items_returned" "aov" "units_in_year"
[10] "dropout_at_zero" "use_priors" "n_sample"
[13] "sample" "use_returns" "n_pred_periods"
[16] "pred_periods" "n_forecast_periods" "forecast_periods"
[19] "unified_horizon"
- aov: Average Order Value (AOV)
- dropout_at_zero: A logical parameter, which indicates if
- use_priors: A logical parameter which indicates if priors should be used (1) or not (0)
Applying the individual customer model
There are two ways to apply the individual customer model to the
prepared data. The first, most simple way is to use the workflow
function fit_customer_model(). However, if something fails,
you won’t be able to make adjustments. If that’s the case, it’s better
to go through the flow of smaller functions yourself and fix errors as
they come up.
Automated workflow
customer_pred_automated <- fit_customer_model(full_cbs, config)Step by step workflow
Forecast predictions
forecast_vars <- c("e_tx_pred_all", "clv_pred_all", "e_uncon_tx_pred",
"e_clv_pred", "e_aov")
forecast_subsets <- draws2list(draws, forecast_vars, as_vector = TRUE)
forecasts <- cust_process_draws_forecast(forecast_subsets)Customer predictions
individual_customer_vars <- c("e_aov", "p_alive_x", "p_return",
"unified_horizon_tx", "unified_horizon_clv")
individual_customer_subsets <- draws2list(draws, individual_customer_vars, as_vector = TRUE)
customer_level_data <- cust_process_draws_individual(full_cbs,
individual_customer_subsets,
draws,
config)Combining the outputs
customer_pred_individual <- list()
customer_pred_individual[["fit"]] <- fit
customer_pred_individual[["draws"]] <- draws
customer_pred_individual[["forecast_subsets"]] <- forecast_subsets
customer_pred_individual[["forecasts"]] <- forecasts
customer_pred_individual[["customer_level_data_subsets"]] <- individual_customer_subsets
customer_pred_individual[["customer_level_data"]] <- customer_level_dataOutputs of the individual customer model
The final individual model object has 6 elements. The
fit and draws elements are direct outputs from
the STAN model, and are generally only used if you want to process some
of the results different than the usual. By default, we’ll already have
subsetted the draws to extract the data we’ll need.
There are then two main outputs;
-
Customer Level Data- A data frame with predictions for each individual in the dataset -
Forecasts- A data frame with predictions for each time period
The first output (customer_level_data) can directly be
used or filtered to create summaries. The forecasts will go into the
fit_forecast_model() function later, which are explained in
detail in Step 4.
Applying the forecasts model.
Synthesizing the individual customer model
The customer level data output has predictions for each individual in the dataset. The list below explains what each of them does:
-
cust– Customer ID -
first_purchase– Customer’s first purchase -
year_acquired– Year the customer was acquired -
unified_horizon_end– Ending date of the customers’ unified horizon -
observed_tx– Observed transactions of a customer -
observed_tx_unified– Observed transactions of a customer in the unified horizon -
predicted_tx_unified– Predicted transactions of a customer in the unified horizon -
total_tx_unified– Total combined transactions of a customer in the unified horizon -
observed_clv– Observed sales of a customer -
observed_clv_unified– Observed sales of a customer in the unified horizon -
predicted_clv_unified– Predicted sales of a customer in the unified horizon -
total_clv_unified– Total combined sales of a customer -
items– Number of observed items bought by a customer -
returns– Number of observed items returned by a customer -
p_alive– Chance a customer is alive as of the current date -
p_return– Chance a customer will return an item -
e_aov– Expected average order volume of a transaction -
expected_tx_pred_period_[n]– Expected transactions per forecast period -
expected_clv_pred_period_[n]– Expected CLV per forecast period
For example chart outputs, please see the section below and refer to
the ?customer_level_output() function.
CLV
customer_level_output(
cust_data = customer_level_predictions$customer_level_data,
config = config,
type = "clv",
output = "plot"
) +
labs(title = "Customer lifetime value started to decline in 2019") +
appraise_theme(base_size = 14)
Transactions
customer_level_output(
cust_data = customer_level_predictions$customer_level_data,
config = config,
type = "total_tx",
output = "plot"
) +
labs(title = "Number of transactions has been steadily increasing") +
appraise_theme(base_size = 14)
AOV
customer_level_output(
cust_data = customer_level_predictions$customer_level_data,
config = config,
type = "e_aov",
output = "plot"
) +
labs(title = "After in increase between 2017 and 2019, AOV is now declining\nsharply") +
appraise_theme(base_size = 14)
MOIC
customer_level_output(
cust_data = customer_level_predictions$forecasts,
config = config,
type = "moic",
output = "plot"
) +
labs(title = "Customers are generally only profitable after multiple years, or at a\nlow CPA") +
appraise_theme(base_size = 14)
Grouped charts
The data can also be summarized by grouping variable, e.g., gender.
All you need to do is add an unquoted grouping variable to
the function, and the data will be summarized by group. The display is
based on the geomtextpath
package.
CLV by gender
gender_dat <- customer_level_predictions$customer_level_data %>%
mutate(gender = sample(c("Male", "Female"), size = nrow(.), replace = TRUE, prob = c(0.4,0.6)))
customer_level_output(
cust_data = gender_dat,
config = config,
type = "clv",
group = gender,
output = "plot"
) +
scale_color_manual(guide = F, values = appraise_pal()(2))+
labs(title = "Male customers declined more sharply") +
appraise_theme(base_size = 14)