Computes the expected spend on future transactions from a fitted gamma-gamma model, given each customer's observed history. For a customer with x past transactions (spends z_1, ..., z_x and covariates w_1, ..., w_x) and a future transaction with covariates w_k, the expected spend is
Arguments
- fit
A fitted model of class
"GGcov"fromGGcov_MLE.- newdata
Data frame with one row per future transaction to predict: the subject column and the covariates (original, unstandardized values; factors are coded as in the fit) in effect for that transaction.
- history
Optional data frame of observed transactions, with the subject column, the spend column and the covariates, laid out like the data used for
GGcov_MLE. IfNULL, every customer is treated as having no past transactions.
Value
A data frame with one row per row of newdata: the subject
identifier, n_trans (number of past transactions used) and
expected_spend. Entries are NA where the formula requires
a condition that fails (\(p x + q \le 1\)).
Details
$$E(Z_k \mid \cdot) = \frac{p \, (\gamma + B(x))}{p x + q - 1} e^{w_k \beta'}, \qquad B(x) = \sum_{i=1}^{x} z_i e^{-w_i \beta'},$$
which requires \(p x + q > 1\) (Fader and Hardie, 2024). One formula covers all cases:
time-varying covariates: each past and future transaction has its own covariate values;
static covariates: repeat each customer's values on every row, and B(x) reduces to the total spend times \(e^{-w\beta'}\);
no past transactions (x = 0, or a customer absent from
history): B(x) = 0, giving the prior expectation \(p \gamma e^{w_k\beta'} / (q - 1)\), which requires q > 1;a model without covariates: \(e^{w_k\beta'} = 1\).
References
Fader, P. S., & Hardie, B. G. S. (2024). Incorporating Covariates in the Gamma-Gamma Spend Model (Research Note No. 045). https://brucehardie.com/notes/045/
Examples
fit <- GGcov_MLE(
Price ~ discount + gender + channel,
data = spend_data_sample,
subject = "Id"
)
last_rows <- spend_data_sample[!duplicated(spend_data_sample$Id, fromLast = TRUE), ]
future <- last_rows[1:5, c("Id", "discount", "gender", "channel")]
future$discount <- 0.1
GGcov_expected_spend(fit, newdata = future, history = spend_data_sample)
#> Id n_trans expected_spend
#> 1 1 2 32.23626
#> 2 5 12 49.09745
#> 3 6 3 84.06568
#> 4 9 2 25.53577
#> 5 10 2 48.92893