Skip to contents

Fits 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:

  1. Fit. A fitted STAN model object

  2. Draws. A Draws object to subset using posterior::subset_draws()

  3. Forecasts. A data frame with forecasts per week for the entire customer cohort. This includes tx = transactions, spend and clv.

  4. Customer Level Data. A data frame with individual customer data, consisting of their transaction statistics and predictions.

    • 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

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)

} # }