R/f_interface_bootstrappedapply.R
clv.bootstrapped.apply.Rd
Given a fitted model, sample new data from the clv.data
stored in it and re-fit the model on it.
Which customers are selected into the new data is determined by fn.sample
.
The model is fit on the new data with the same options with which it was originally fit,
including optimx.args
, verbose
and start parameters. If required,
any option can be changed by passing it as ...
.
After the model is fit, fn.boot.apply
is applied to it and
the value it returns is collected in a list which is eventually returned.
The estimation and holdout periods are preserved exactly as in the original data.
This is regardless of how the actually sampled transactions would define these periods.
This way, each customer's model summary data (cbs
) generated from the
sampled data remains the same as on the original data.
This makes sampling from the clv.data
object equivalent to sampling
directly from the model summary data.
Note that the Id of customers which are sampled more than once gains a suffix "_BOOTSTRAP_ID_<number>".
clv.bootstrapped.apply(object, num.boots, fn.boot.apply, fn.sample = NULL, ...)
Fitted model
number of times to sample data and re-fit the model
Method to apply on each model estimated on the sampled data. See examples.
Method sampling customer ids for creating the bootstrapped data. Receives and returns
a vector of ids (string). If NULL
, ids are sampled with replacement until reaching original length. See examples.
Passed to the model estimation method. See examples.
Returns a list containing the results of fn.boot.apply
# \donttest{
data("cdnow")
clv.cdnow <- clvdata(data.transactions = cdnow, date.format="ymd",
time.unit = "weeks", estimation.split=37)
pnbd.cdnow <- pnbd(clv.cdnow)
#> Starting estimation...
#> Estimation finished!
# bootstrapped model coefs while sampling 50 percent
# of customers without replacement
clv.bootstrapped.apply(pnbd.cdnow, num.boots=5, fn.boot.apply=coef,
fn.sample=function(x){
sample(x, size = as.integer(0.5*length(x)), replace = FALSE)})
#> Starting estimation...
#> Estimation finished!
#> Starting estimation...
#> Estimation finished!
#> Starting estimation...
#> Estimation finished!
#> Starting estimation...
#> Estimation finished!
#> Starting estimation...
#> Estimation finished!
#> [[1]]
#> r alpha s beta
#> 0.4957446 8.7161131 0.8870832 19.8228384
#>
#> [[2]]
#> r alpha s beta
#> 0.5226944 9.8447846 0.9381996 19.1965151
#>
#> [[3]]
#> r alpha s beta
#> 0.5596494 10.4023475 0.6238191 11.4740045
#>
#> [[4]]
#> r alpha s beta
#> 0.5466761 11.2869693 0.6170729 12.4091227
#>
#> [[5]]
#> r alpha s beta
#> 0.6379314 12.2352752 0.7108075 11.5893545
#>
# sample customers with built-in standard logic and
# return predictions until end of holdout period in original
# data.
# prediction.end is not required because the bootstrapped
# data contains the same estimation and holdout periods
# as the original data, even if the transactions of the sampled
# customers .
clv.bootstrapped.apply(pnbd.cdnow, num.boots=5, fn.sample=NULL,
fn.boot.apply=function(x){predict(x)})
#> Starting estimation...
#> Estimation finished!
#> Predicting from 1997-09-18 until (incl.) 1998-06-30 (40.86 Weeks).
#> Estimating gg model to predict spending...
#> Starting estimation...
#> Estimation finished!
#> Starting estimation...
#> Estimation finished!
#> Predicting from 1997-09-18 until (incl.) 1998-06-30 (40.86 Weeks).
#> Estimating gg model to predict spending...
#> Starting estimation...
#> Estimation finished!
#> Starting estimation...
#> Estimation finished!
#> Predicting from 1997-09-18 until (incl.) 1998-06-30 (40.86 Weeks).
#> Estimating gg model to predict spending...
#> Starting estimation...
#> Estimation finished!
#> Starting estimation...
#> Estimation finished!
#> Predicting from 1997-09-18 until (incl.) 1998-06-30 (40.86 Weeks).
#> Estimating gg model to predict spending...
#> Starting estimation...
#> Estimation finished!
#> Starting estimation...
#> Estimation finished!
#> Predicting from 1997-09-18 until (incl.) 1998-06-30 (40.86 Weeks).
#> Estimating gg model to predict spending...
#> Starting estimation...
#> Estimation finished!
#> [[1]]
#> Key: <Id>
#> Id period.first period.last period.length actual.x
#> <char> <Date> <Date> <num> <int>
#> 1: 1 1997-09-18 1998-06-30 40.85714 1
#> 2: 10 1997-09-18 1998-06-30 40.85714 0
#> 3: 100 1997-09-18 1998-06-30 40.85714 0
#> 4: 1000 1997-09-18 1998-06-30 40.85714 3
#> 5: 1001 1997-09-18 1998-06-30 40.85714 0
#> ---
#> 2353: 993_BOOTSTRAP_ID_2 1997-09-18 1998-06-30 40.85714 0
#> 2354: 997 1997-09-18 1998-06-30 40.85714 0
#> 2355: 997_BOOTSTRAP_ID_2 1997-09-18 1998-06-30 40.85714 0
#> 2356: 998 1997-09-18 1998-06-30 40.85714 0
#> 2357: 999 1997-09-18 1998-06-30 40.85714 0
#> actual.total.spending PAlive CET DERT
#> <num> <num> <num> <num>
#> 1: 26.48 0.9019063 1.6714626 0.46842422
#> 2: 0.00 0.2864883 0.1142056 0.03200590
#> 3: 0.00 0.7967051 0.9065016 0.25425372
#> 4: 81.95 0.8429948 3.0868965 0.87225145
#> 5: 0.00 0.3208890 0.1416068 0.04001323
#> ---
#> 2353: 0.00 0.3208890 0.1416068 0.04001323
#> 2354: 0.00 0.3208890 0.1416068 0.04001323
#> 2355: 0.00 0.3208890 0.1416068 0.04001323
#> 2356: 0.00 0.3208890 0.1416068 0.04001323
#> 2357: 0.00 0.2019389 0.2517020 0.07112238
#> predicted.mean.spending predicted.total.spending predicted.CLV
#> <num> <num> <num>
#> 1: 24.51763 40.980311 11.484654
#> 2: 35.22984 4.023445 1.127563
#> 3: 28.82005 26.125421 7.327605
#> 4: 18.00660 55.584502 15.706281
#> 5: 35.22984 4.988784 1.409660
#> ---
#> 2353: 35.22984 4.988784 1.409660
#> 2354: 35.22984 4.988784 1.409660
#> 2355: 35.22984 4.988784 1.409660
#> 2356: 35.22984 4.988784 1.409660
#> 2357: 17.83571 4.489283 1.268518
#>
#> [[2]]
#> Key: <Id>
#> Id period.first period.last period.length actual.x
#> <char> <Date> <Date> <num> <int>
#> 1: 10 1997-09-18 1998-06-30 40.85714 0
#> 2: 100 1997-09-18 1998-06-30 40.85714 0
#> 3: 1000 1997-09-18 1998-06-30 40.85714 3
#> 4: 1000_BOOTSTRAP_ID_2 1997-09-18 1998-06-30 40.85714 3
#> 5: 1000_BOOTSTRAP_ID_3 1997-09-18 1998-06-30 40.85714 3
#> ---
#> 2353: 992 1997-09-18 1998-06-30 40.85714 0
#> 2354: 993 1997-09-18 1998-06-30 40.85714 0
#> 2355: 993_BOOTSTRAP_ID_2 1997-09-18 1998-06-30 40.85714 0
#> 2356: 99_BOOTSTRAP_ID_2 1997-09-18 1998-06-30 40.85714 4
#> 2357: 9_BOOTSTRAP_ID_2 1997-09-18 1998-06-30 40.85714 2
#> actual.total.spending PAlive CET DERT
#> <num> <num> <num> <num>
#> 1: 0.00 0.3194660 0.1236958 0.03487670
#> 2: 0.00 0.7928033 0.8720294 0.24607336
#> 3: 81.95 0.8416514 2.9678274 0.84360913
#> 4: 81.95 0.8416514 2.9678274 0.84360913
#> 5: 81.95 0.8416514 2.9678274 0.84360913
#> ---
#> 2353: 0.00 0.3577129 0.1529400 0.04347341
#> 2354: 0.00 0.3577129 0.1529400 0.04347341
#> 2355: 0.00 0.3577129 0.1529400 0.04347341
#> 2356: 130.01 0.9887136 2.4890312 0.70236656
#> 2357: 57.47 0.9832008 1.7602109 0.49630117
#> predicted.mean.spending predicted.total.spending predicted.CLV
#> <num> <num> <num>
#> 1: 36.42148 4.505183 1.270261
#> 2: 30.02371 26.181556 7.388035
#> 3: 18.86891 55.999675 15.917987
#> 4: 18.86891 55.999675 15.917987
#> 5: 18.86891 55.999675 15.917987
#> ---
#> 2353: 36.42148 5.570300 1.583366
#> 2354: 36.42148 5.570300 1.583366
#> 2355: 36.42148 5.570300 1.583366
#> 2356: 24.17666 60.176473 16.980881
#> 2357: 28.04119 49.358417 13.916878
#>
#> [[3]]
#> Key: <Id>
#> Id period.first period.last period.length actual.x
#> <char> <Date> <Date> <num> <int>
#> 1: 1 1997-09-18 1998-06-30 40.85714 1
#> 2: 10 1997-09-18 1998-06-30 40.85714 0
#> 3: 100 1997-09-18 1998-06-30 40.85714 0
#> 4: 1001 1997-09-18 1998-06-30 40.85714 0
#> 5: 1002 1997-09-18 1998-06-30 40.85714 0
#> ---
#> 2353: 995 1997-09-18 1998-06-30 40.85714 0
#> 2354: 997 1997-09-18 1998-06-30 40.85714 0
#> 2355: 998 1997-09-18 1998-06-30 40.85714 0
#> 2356: 998_BOOTSTRAP_ID_2 1997-09-18 1998-06-30 40.85714 0
#> 2357: 999 1997-09-18 1998-06-30 40.85714 0
#> actual.total.spending PAlive CET DERT
#> <num> <num> <num> <num>
#> 1: 26.48 0.8926002 1.5814475 0.44825818
#> 2: 0.00 0.2969806 0.1059317 0.03002612
#> 3: 0.00 0.7813099 0.8401179 0.23833738
#> 4: 0.00 0.3335302 0.1315155 0.03760389
#> 5: 0.00 0.3335302 0.1315155 0.03760389
#> ---
#> 2353: 0.00 0.3335302 0.1315155 0.03760389
#> 2354: 0.00 0.3335302 0.1315155 0.03760389
#> 2355: 0.00 0.3335302 0.1315155 0.03760389
#> 2356: 0.00 0.3335302 0.1315155 0.03760389
#> 2357: 0.00 0.2111519 0.2484093 0.07102702
#> predicted.mean.spending predicted.total.spending predicted.CLV
#> <num> <num> <num>
#> 1: 24.67334 39.019587 11.060025
#> 2: 36.07451 3.821432 1.083177
#> 3: 29.07775 24.428734 6.930314
#> 4: 36.07451 4.744358 1.356542
#> 5: 36.07451 4.744358 1.356542
#> ---
#> 2353: 36.07451 4.744358 1.356542
#> 2354: 36.07451 4.744358 1.356542
#> 2355: 36.07451 4.744358 1.356542
#> 2356: 36.07451 4.744358 1.356542
#> 2357: 18.11525 4.499997 1.286672
#>
#> [[4]]
#> Key: <Id>
#> Id period.first period.last period.length actual.x
#> <char> <Date> <Date> <num> <int>
#> 1: 1 1997-09-18 1998-06-30 40.85714 1
#> 2: 1000 1997-09-18 1998-06-30 40.85714 3
#> 3: 1001 1997-09-18 1998-06-30 40.85714 0
#> 4: 1001_BOOTSTRAP_ID_2 1997-09-18 1998-06-30 40.85714 0
#> 5: 1003 1997-09-18 1998-06-30 40.85714 0
#> ---
#> 2353: 994 1997-09-18 1998-06-30 40.85714 1
#> 2354: 995 1997-09-18 1998-06-30 40.85714 0
#> 2355: 995_BOOTSTRAP_ID_2 1997-09-18 1998-06-30 40.85714 0
#> 2356: 995_BOOTSTRAP_ID_3 1997-09-18 1998-06-30 40.85714 0
#> 2357: 997 1997-09-18 1998-06-30 40.85714 0
#> actual.total.spending PAlive CET DERT
#> <num> <num> <num> <num>
#> 1: 26.48 0.9076955 1.7146275 0.47746326
#> 2: 81.95 0.8520109 3.1253974 0.87719732
#> 3: 0.00 0.3101175 0.1557945 0.04372645
#> 4: 0.00 0.3101175 0.1557945 0.04372645
#> 5: 0.00 0.9274587 1.1999874 0.33679739
#> ---
#> 2353: 62.96 0.4179170 0.8714892 0.24459866
#> 2354: 0.00 0.3101175 0.1557945 0.04372645
#> 2355: 0.00 0.3101175 0.1557945 0.04372645
#> 2356: 0.00 0.3101175 0.1557945 0.04372645
#> 2357: 0.00 0.3101175 0.1557945 0.04372645
#> predicted.mean.spending predicted.total.spending predicted.CLV
#> <num> <num> <num>
#> 1: 24.17783 41.455964 11.54402
#> 2: 17.65682 55.184574 15.48851
#> 3: 36.61718 5.704757 1.60114
#> 4: 36.61718 5.704757 1.60114
#> 5: 61.09642 73.314932 20.57711
#> ---
#> 2353: 41.72710 36.364719 10.20639
#> 2354: 36.61718 5.704757 1.60114
#> 2355: 36.61718 5.704757 1.60114
#> 2356: 36.61718 5.704757 1.60114
#> 2357: 36.61718 5.704757 1.60114
#>
#> [[5]]
#> Key: <Id>
#> Id period.first period.last period.length actual.x
#> <char> <Date> <Date> <num> <int>
#> 1: 1 1997-09-18 1998-06-30 40.85714 1
#> 2: 10 1997-09-18 1998-06-30 40.85714 0
#> 3: 100 1997-09-18 1998-06-30 40.85714 0
#> 4: 1000 1997-09-18 1998-06-30 40.85714 3
#> 5: 1000_BOOTSTRAP_ID_2 1997-09-18 1998-06-30 40.85714 3
#> ---
#> 2353: 994_BOOTSTRAP_ID_2 1997-09-18 1998-06-30 40.85714 1
#> 2354: 996 1997-09-18 1998-06-30 40.85714 0
#> 2355: 998 1997-09-18 1998-06-30 40.85714 0
#> 2356: 9_BOOTSTRAP_ID_2 1997-09-18 1998-06-30 40.85714 2
#> 2357: 9_BOOTSTRAP_ID_3 1997-09-18 1998-06-30 40.85714 2
#> actual.total.spending PAlive CET DERT
#> <num> <num> <num> <num>
#> 1: 26.48 0.8692773 1.4570189 0.42578306
#> 2: 0.00 0.2891799 0.1014649 0.02965097
#> 3: 0.00 0.7426171 0.7603566 0.22240640
#> 4: 81.95 0.8026706 2.6604293 0.78465805
#> 5: 81.95 0.8026706 2.6604293 0.78465805
#> ---
#> 2353: 62.96 0.3635772 0.6729713 0.19848389
#> 2354: 0.00 0.3313393 0.1283848 0.03786538
#> 2355: 0.00 0.3313393 0.1283848 0.03786538
#> 2356: 57.47 0.9777397 1.6388156 0.47890931
#> 2357: 57.47 0.9777397 1.6388156 0.47890931
#> predicted.mean.spending predicted.total.spending predicted.CLV
#> <num> <num> <num>
#> 1: 23.80207 34.680062 10.134517
#> 2: 35.53266 3.605317 1.053578
#> 3: 28.07311 21.345576 6.243640
#> 4: 17.38695 46.256743 13.642808
#> 5: 17.38695 46.256743 13.642808
#> ---
#> 2353: 41.71241 28.071254 8.279241
#> 2354: 35.53266 4.561853 1.345458
#> 2355: 35.53266 4.561853 1.345458
#> 2356: 26.65296 43.679279 12.764348
#> 2357: 26.65296 43.679279 12.764348
#>
# return the fitted models
# forward additional arguments to the model fitting method
clv.bootstrapped.apply(pnbd.cdnow, num.boots=5, fn.sample=NULL,
fn.boot.apply=return,
# args for ..., forwarded to pnbd()
verbose=FALSE, optimx.args=list(method="Nelder-Mead"),
start.params.model=coef(pnbd.cdnow))
#> [[1]]
#> Pareto/NBD Standard Model
#>
#> Call:
#> clv.fitted.estimate.same.specification.on.new.data(clv.fitted = object,
#> newdata = clv.data.boot, verbose = FALSE, optimx.args = ..2,
#> start.params.model = ..3)
#>
#> Coefficients:
#> r alpha s beta
#> 0.5949 11.0468 0.5823 10.2491
#> KKT1: TRUE
#> KKT2: TRUE
#>
#> Used Options:
#> Correlation: FALSE
#>
#> [[2]]
#> Pareto/NBD Standard Model
#>
#> Call:
#> clv.fitted.estimate.same.specification.on.new.data(clv.fitted = object,
#> newdata = clv.data.boot, verbose = FALSE, optimx.args = ..2,
#> start.params.model = ..3)
#>
#> Coefficients:
#> r alpha s beta
#> 0.472 9.129 1.007 29.232
#> KKT1: TRUE
#> KKT2: TRUE
#>
#> Used Options:
#> Correlation: FALSE
#>
#> [[3]]
#> Pareto/NBD Standard Model
#>
#> Call:
#> clv.fitted.estimate.same.specification.on.new.data(clv.fitted = object,
#> newdata = clv.data.boot, verbose = FALSE, optimx.args = ..2,
#> start.params.model = ..3)
#>
#> Coefficients:
#> r alpha s beta
#> 0.5507 9.8285 0.7867 15.1654
#> KKT1: TRUE
#> KKT2: TRUE
#>
#> Used Options:
#> Correlation: FALSE
#>
#> [[4]]
#> Pareto/NBD Standard Model
#>
#> Call:
#> clv.fitted.estimate.same.specification.on.new.data(clv.fitted = object,
#> newdata = clv.data.boot, verbose = FALSE, optimx.args = ..2,
#> start.params.model = ..3)
#>
#> Coefficients:
#> r alpha s beta
#> 0.5518 10.1416 0.6039 10.6094
#> KKT1: TRUE
#> KKT2: TRUE
#>
#> Used Options:
#> Correlation: FALSE
#>
#> [[5]]
#> Pareto/NBD Standard Model
#>
#> Call:
#> clv.fitted.estimate.same.specification.on.new.data(clv.fitted = object,
#> newdata = clv.data.boot, verbose = FALSE, optimx.args = ..2,
#> start.params.model = ..3)
#>
#> Coefficients:
#> r alpha s beta
#> 0.6378 11.9780 0.9405 18.6957
#> KKT1: TRUE
#> KKT2: TRUE
#>
#> Used Options:
#> Correlation: FALSE
#>
# }