@@ -207,6 +207,8 @@ mcmc.plotPActiveDiagnostic <- function(cbs, xstar, title = "Diagnostic Plot for
207
207
# ' @param x Number of transactions for which probability is calculated. May also
208
208
# ' be a vector.
209
209
# ' @param sample_size Sample size for estimating the probability distribution.
210
+ # ' @param covariates (optional) Matrix of covariates, for Pareto/NBD (Abe)
211
+ # ' model, passed to \code{\link{abe.GenerateData}} for simulating data.
210
212
# ' @return \eqn{P(X(t)=x)}. If either \code{t} or \code{x} is a vector, then the
211
213
# ' output will be a vector as well. If both are vectors, the output will be a
212
214
# ' matrix.
@@ -218,7 +220,7 @@ mcmc.plotPActiveDiagnostic <- function(cbs, xstar, title = "Diagnostic Plot for
218
220
# ' mcmc = 200, burnin = 100, thin = 20, chains = 1) # short MCMC to run demo fast
219
221
# ' mcmc.pmf(param.draws, t = 52, x = 0:6)
220
222
# ' mcmc.pmf(param.draws, t = c(26, 52), x = 0:6)
221
- mcmc.pmf <- function (draws , t , x , sample_size = 10000 ) {
223
+ mcmc.pmf <- function (draws , t , x , sample_size = 10000 , covariates = NULL ) {
222
224
cohort_draws <- as.matrix(draws $ level_2 )
223
225
nr_of_draws <- nrow(cohort_draws )
224
226
# use posterior mean
@@ -238,7 +240,8 @@ mcmc.pmf <- function(draws, t, x, sample_size = 10000) {
238
240
p [" cov_log_lambda_log_mu" ],
239
241
p [" var_log_mu" ]),
240
242
ncol = 2 )
241
- abe.GenerateData(n = n , T .cal = 0 , T .star = unique(t ), params = params )$ cbs
243
+ abe.GenerateData(n = n , T .cal = 0 , T .star = unique(t ), params = params ,
244
+ covariates = covariates )$ cbs
242
245
}
243
246
}))
244
247
pmf <- sapply(1 : length(t ), function (idx ) {
@@ -302,6 +305,8 @@ mcmc.Expectation <- function(draws, t, sample_size = 10000) {
302
305
# ' @param n.periods.final Number of time periods in the calibration and holdout
303
306
# ' periods.
304
307
# ' @param sample_size Sample size for estimating the probability distribution.
308
+ # ' @param covariates (optional) Matrix of covariates, for Pareto/NBD (Abe)
309
+ # ' model, passed to \code{\link{abe.GenerateData}} for simulating data.
305
310
# ' @return Numeric vector of expected cumulative total repeat transactions by
306
311
# ' all customers.
307
312
# ' @export
@@ -314,7 +319,8 @@ mcmc.Expectation <- function(draws, t, sample_size = 10000) {
314
319
# ' # weeks, with every eigth week being reported.
315
320
# ' mcmc.ExpectedCumulativeTransactions(param.draws,
316
321
# ' T.cal = cbs$T.cal, T.tot = 104, n.periods.final = 104/8, sample_size = 1000)
317
- mcmc.ExpectedCumulativeTransactions <- function (draws , T .cal , T .tot , n.periods.final , sample_size = 10000 ) {
322
+ mcmc.ExpectedCumulativeTransactions <- function (draws , T .cal , T .tot , n.periods.final ,
323
+ sample_size = 10000 , covariates = NULL ) {
318
324
if (any(T .cal < 0 ) || ! is.numeric(T .cal ))
319
325
stop(" T.cal must be numeric and may not contain negative numbers." )
320
326
if (length(T .tot ) > 1 || T .tot < 0 || ! is.numeric(T .tot ))
@@ -333,13 +339,14 @@ mcmc.ExpectedCumulativeTransactions <- function(draws, T.cal, T.tot, n.periods.f
333
339
} else if (model == " abe" ) {
334
340
p <- as.list(cohort_draws [i , ])
335
341
params <- list ()
336
- params $ beta <- matrix (p [grepl(" ^log\\ _" , names(p ))], byrow = TRUE , ncol = 2 )
337
- params $ gamma <- matrix (c(p [" var_log_lambda" ],
342
+ params $ beta <- matrix (as.numeric( p [grepl(" ^log\\ _" , names(p ))]) , byrow = TRUE , ncol = 2 )
343
+ params $ gamma <- matrix (as.numeric( c(p [" var_log_lambda" ],
338
344
p [" cov_log_lambda_log_mu" ],
339
345
p [" cov_log_lambda_log_mu" ],
340
- p [" var_log_mu" ]),
346
+ p [" var_log_mu" ])) ,
341
347
ncol = 2 )
342
- elog <- abe.GenerateData(n = n , T .cal = T .tot , T .star = 0 , params = params )$ elog
348
+ elog <- abe.GenerateData(n = n , T .cal = T .tot , T .star = 0 , params = params ,
349
+ covariates = covariates )$ elog
343
350
}
344
351
setDT(elog )
345
352
elog $ cust <- paste0(elog $ cust , " _" , i )
@@ -389,6 +396,8 @@ mcmc.ExpectedCumulativeTransactions <- function(draws, T.cal, T.tot, n.periods.f
389
396
# ' @param ymax Upper boundary for y axis.
390
397
# ' @param sample_size Sample size for estimating the probability distribution.
391
398
# ' See \code{\link{mcmc.ExpectedCumulativeTransactions}}.
399
+ # ' @param covariates (optional) Matrix of covariates, for Pareto/NBD (Abe)
400
+ # ' model, passed to \code{\link{abe.GenerateData}} for simulating data.
392
401
# ' @return Matrix containing actual and expected cumulative repeat transactions.
393
402
# ' @export
394
403
# ' @seealso \code{\link{mcmc.PlotTrackingInc}}
@@ -407,10 +416,11 @@ mcmc.ExpectedCumulativeTransactions <- function(draws, T.cal, T.tot, n.periods.f
407
416
mcmc.PlotTrackingCum <- function (draws , T .cal , T .tot , actual.cu.tracking.data ,
408
417
xlab = " Week" , ylab = " Cumulative Transactions" ,
409
418
xticklab = NULL , title = " Tracking Cumulative Transactions" ,
410
- ymax = NULL , sample_size = 10000 ) {
419
+ ymax = NULL , sample_size = 10000 , covariates = NULL ) {
411
420
412
421
actual <- actual.cu.tracking.data
413
- expected <- mcmc.ExpectedCumulativeTransactions(draws , T .cal , T .tot , length(actual ), sample_size = sample_size )
422
+ expected <- mcmc.ExpectedCumulativeTransactions(draws , T .cal , T .tot , length(actual ),
423
+ sample_size = sample_size , covariates = covariates )
414
424
415
425
dc.PlotTracking(actual = actual , expected = expected , T .cal = T .cal ,
416
426
xlab = xlab , ylab = ylab , title = title ,
@@ -445,6 +455,8 @@ mcmc.PlotTrackingCum <- function(draws, T.cal, T.tot, actual.cu.tracking.data,
445
455
# ' @param ymax Upper boundary for y axis.
446
456
# ' @param sample_size Sample size for estimating the probability distribution.
447
457
# ' See \code{\link{mcmc.ExpectedCumulativeTransactions}}.
458
+ # ' @param covariates (optional) Matrix of covariates, for Pareto/NBD (Abe)
459
+ # ' model, passed to \code{\link{abe.GenerateData}} for simulating data.
448
460
# ' @return Matrix containing actual and expected incremental repeat
449
461
# ' transactions.
450
462
# ' @export
@@ -464,10 +476,11 @@ mcmc.PlotTrackingCum <- function(draws, T.cal, T.tot, actual.cu.tracking.data,
464
476
mcmc.PlotTrackingInc <- function (draws , T .cal , T .tot , actual.inc.tracking.data ,
465
477
xlab = " Week" , ylab = " Transactions" ,
466
478
xticklab = NULL , title = " Tracking Weekly Transactions" ,
467
- ymax = NULL , sample_size = 10000 ) {
479
+ ymax = NULL , sample_size = 10000 , covariates = NULL ) {
468
480
469
481
actual <- actual.inc.tracking.data
470
- expected_cum <- mcmc.ExpectedCumulativeTransactions(draws , T .cal , T .tot , length(actual ), sample_size = sample_size )
482
+ expected_cum <- mcmc.ExpectedCumulativeTransactions(draws , T .cal , T .tot , length(actual ),
483
+ sample_size = sample_size , covariates = covariates )
471
484
expected <- BTYD :: dc.CumulativeToIncremental(expected_cum )
472
485
473
486
dc.PlotTracking(actual = actual , expected = expected , T .cal = T .cal ,
0 commit comments