Fit and extract the Customer By Statistics (CBS) STAN Model
Source:R/fit_customer_model.R
fit_customer_model.RdFits the individual-level customer model and returns a list with results.
Usage
extract_customer_model_results(fit, cbs, config, draws_dir = NULL)
fit_customer_model(cbs, config, ...)
cust_process_draws_forecast(draw_subsets)
cust_process_draws_individual(cbs, draw_subsets, draws_dir, config)Arguments
- fit
A fitted model object resulting from the customer model fitting procedure.
- cbs
List with the CBS data and Stan data generated by
elog_to_stan().- config
Configuration list created by
appraise_configure().- draws_dir
Directory where the draws are stored.
- ...
further arguments supplied to
cmdstanr::optimize()- draw_subsets
A subsetted list of draws created by
draws2list().- draws
The results of fit$draws().
Value
Returns a list with the following objects:
Fit. A fitted STAN model objectDraws. A Draws object to subset usingposterior::subset_draws()Forecasts. A data frame with forecasts per week for the entire customer cohort. This includestx = transactions,spendandclv.Customer Level Data. A data frame with individual customer data, consisting of their transaction statistics and predictions.cust– Customer IDfirst_purchase– Customer's first purchaseyear_acquired– Year the customer was acquiredunified_horizon_end– Ending date of the customers' unified horizonobserved_tx– Observed transactions of a customerobserved_tx_unified– Observed transactions of a customer in the unified horizonpredicted_tx_unified– Predicted transactions of a customer in the unified horizontotal_tx_unified– Total combined transactions of a customer in the unified horizonobserved_clv– Observed sales of a customerobserved_clv_unified– Observed sales of a customer in the unified horizonpredicted_clv_unified– Predicted sales of a customer in the unified horizontotal_clv_unified– Total combined sales of a customeritems– Number of observed items bought by a customerreturns– Number of observed items returned by a customerp_alive– Chance a customer is alive as of the current datep_return– Chance a customer will return an iteme_aov– Expected average order volume of a transactionexpected_tx_pred_period_[n]– Expected transactions per forecast periodexpected_clv_pred_period_[n]– Expected CLV per forecast period
Examples
if (FALSE) { # \dontrun{
set.seed(123)
config <- appraise_configure(sample = TRUE,
cpa = c(25, 50, 75, 100, 125, 150, 200),
cm = c(.2, .3, .432, .5),
currency = '$',
n_forecast = 5)
full_cbs <-
elog_to_stan(elog_validated$elog,
config,
items_and_returns_validated$items_and_returns)
# Fitting the model is comprised of multiple small pieces. You can either
# apply all of the functions one by one, or you can use the workflow function.
#### Automated flow ----
fit <- fit_customer_model(full_cbs, config)
automated_results <- extract_customer_model_results(fit, full_cbs, config)
#### Individual flow ----
fit <- fit_customer_model(full_cbs, config)
csv_file <- fit$output_files()
draws_dir <- NULL
if (is.null(draws_dir)) {
draws_dir <- tempfile(pattern = "draws_")
on.exit(unlink(draws_dir))
}
decompose_draws_csv_into_dir(
csv_file, out_dir = draws_dir,
model_constants = full_cbs$stan
)
# Forecasts ---------------------------------------------------------------
forecast_vars <- c("e_tx_pred_all", "clv_pred_all", "e_uncon_tx_pred",
"e_clv_pred", "e_aov")
forecast_subsets <- draws2list(draws_dir, forecast_vars)
forecasts <- cust_process_draws_forecast(forecast_subsets)
# Individual level data ---------------------------------------------------
individual_customer_vars <- c("e_aov", "p_alive_x", "p_return",
"unified_horizon_tx", "unified_horizon_clv")
individual_customer_subsets <- draws2list(draws_dir, individual_customer_vars)
customer_level_data <- cust_process_draws_individual(
full_cbs, individual_customer_subsets, draws_dir, config
)
# Building list output ----------------------------------------------------
manual_results <- list()
manual_results[["forecast_subsets"]] <- forecast_subsets
manual_results[["forecasts"]] <- forecasts
manual_results[["customer_level_data_subsets"]] <- individual_customer_subsets
manual_results[["customer_level_data"]] <- customer_level_data
identical(automated_results$customer_level_data, manual_results$customer_level_data)
} # }