R/f_interface_bootstrappedapply.R
clv.bootstrapped.apply.RdGiven 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.4626189 8.3874747 0.8599152 19.4851593
#>
#> [[2]]
#> r alpha s beta
#> 0.5185512 9.4333639 0.7019989 12.4426672
#>
#> [[3]]
#> r alpha s beta
#> 0.5348799 9.7840170 0.8295822 16.0219689
#>
#> [[4]]
#> r alpha s beta
#> 0.5550662 10.6577692 0.6021649 11.1597964
#>
#> [[5]]
#> r alpha s beta
#> 0.5945242 11.8422582 0.8227568 15.5448863
#>
# 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.period.spending PAlive CET DERT
#> <num> <num> <num> <num>
#> 1: 26.48 0.9025921 1.6746121 0.46899995
#> 2: 0.00 0.2891751 0.1145936 0.03209363
#> 3: 0.00 0.7980614 0.9079615 0.25449523
#> 4: 81.95 0.8439529 3.0975448 0.87463903
#> 5: 0.00 0.3235729 0.1419929 0.04009386
#> ---
#> 2353: 0.00 0.3235729 0.1419929 0.04009386
#> 2354: 0.00 0.3235729 0.1419929 0.04009386
#> 2355: 0.00 0.3235729 0.1419929 0.04009386
#> 2356: 0.00 0.3235729 0.1419929 0.04009386
#> 2357: 0.00 0.2034528 0.2536429 0.07161995
#> predicted.mean.spending predicted.period.spending predicted.CLV
#> <num> <num> <num>
#> 1: 24.61126 41.214320 11.542681
#> 2: 35.31601 4.046988 1.133419
#> 3: 28.92573 26.263448 7.361460
#> 4: 18.08406 56.016178 15.817023
#> 5: 35.31601 5.014622 1.415955
#> ---
#> 2353: 35.31601 5.014622 1.415955
#> 2354: 35.31601 5.014622 1.415955
#> 2355: 35.31601 5.014622 1.415955
#> 2356: 35.31601 5.014622 1.415955
#> 2357: 18.07856 4.585498 1.294785
#>
#> [[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.period.spending PAlive CET DERT
#> <num> <num> <num> <num>
#> 1: 0.00 0.3162950 0.1246695 0.03511670
#> 2: 0.00 0.7937655 0.8785545 0.24767058
#> 3: 81.95 0.8424923 2.9760151 0.84509806
#> 4: 81.95 0.8424923 2.9760151 0.84509806
#> 5: 81.95 0.8424923 2.9760151 0.84509806
#> ---
#> 2353: 0.00 0.3542898 0.1541568 0.04377586
#> 2354: 0.00 0.3542898 0.1541568 0.04377586
#> 2355: 0.00 0.3542898 0.1541568 0.04377586
#> 2356: 130.01 0.9888038 2.4958019 0.70358380
#> 2357: 57.47 0.9833353 1.7670689 0.49774500
#> predicted.mean.spending predicted.period.spending predicted.CLV
#> <num> <num> <num>
#> 1: 36.26780 4.521489 1.273605
#> 2: 29.87918 26.250484 7.400193
#> 3: 18.76609 55.848165 15.859186
#> 4: 18.76609 55.848165 15.859186
#> 5: 18.76609 55.848165 15.859186
#> ---
#> 2353: 36.26780 5.590928 1.587654
#> 2354: 36.26780 5.590928 1.587654
#> 2355: 36.26780 5.590928 1.587654
#> 2356: 24.07741 60.092439 16.940474
#> 2357: 27.93606 49.364945 13.905035
#>
#> [[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.period.spending PAlive CET DERT
#> <num> <num> <num> <num>
#> 1: 26.48 0.8895684 1.5608379 0.44408136
#> 2: 0.00 0.2988330 0.1034243 0.02942575
#> 3: 0.00 0.7763286 0.8238972 0.23461848
#> 4: 0.00 0.3360385 0.1285911 0.03691055
#> 5: 0.00 0.3360385 0.1285911 0.03691055
#> ---
#> 2353: 0.00 0.3360385 0.1285911 0.03691055
#> 2354: 0.00 0.3360385 0.1285911 0.03691055
#> 2355: 0.00 0.3360385 0.1285911 0.03691055
#> 2356: 0.00 0.3360385 0.1285911 0.03691055
#> 2357: 0.00 0.2119292 0.2461226 0.07064658
#> predicted.mean.spending predicted.period.spending predicted.CLV
#> <num> <num> <num>
#> 1: 24.63803 38.455978 10.941292
#> 2: 36.31092 3.755433 1.068476
#> 3: 29.06629 23.947633 6.819488
#> 4: 36.31092 4.669261 1.340256
#> 5: 36.31092 4.669261 1.340256
#> ---
#> 2353: 36.31092 4.669261 1.340256
#> 2354: 36.31092 4.669261 1.340256
#> 2355: 36.31092 4.669261 1.340256
#> 2356: 36.31092 4.669261 1.340256
#> 2357: 17.98136 4.425619 1.270322
#>
#> [[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_BOOTSTRAP_ID_2 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.period.spending PAlive CET DERT
#> <num> <num> <num> <num>
#> 1: 26.48 0.9106031 1.7332676 0.48121251
#> 2: 81.95 0.8563033 3.1577353 0.88344354
#> 3: 0.00 0.3111230 0.1601151 0.04479561
#> 4: 0.00 0.3111230 0.1601151 0.04479561
#> 5: 0.00 0.9296575 1.2158862 0.34016999
#> ---
#> 2353: 62.96 0.4256017 0.8942471 0.25018464
#> 2354: 0.00 0.3111230 0.1601151 0.04479561
#> 2355: 0.00 0.3111230 0.1601151 0.04479561
#> 2356: 0.00 0.3111230 0.1601151 0.04479561
#> 2357: 0.00 0.3111230 0.1601151 0.04479561
#> predicted.mean.spending predicted.period.spending predicted.CLV
#> <num> <num> <num>
#> 1: 24.23266 42.001684 11.661059
#> 2: 17.70384 55.904035 15.640342
#> 3: 36.55664 5.853271 1.637577
#> 4: 36.55664 5.853271 1.637577
#> 5: 60.86377 74.003421 20.704029
#> ---
#> 2353: 41.69323 37.284053 10.431006
#> 2354: 36.55664 5.853271 1.637577
#> 2355: 36.55664 5.853271 1.637577
#> 2356: 36.55664 5.853271 1.637577
#> 2357: 36.55664 5.853271 1.637577
#>
#> [[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: 996 1997-09-18 1998-06-30 40.85714 0
#> 2354: 998 1997-09-18 1998-06-30 40.85714 0
#> 2355: 99_BOOTSTRAP_ID_2 1997-09-18 1998-06-30 40.85714 4
#> 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.period.spending PAlive CET DERT
#> <num> <num> <num> <num>
#> 1: 26.48 0.8659139 1.43713313 0.42191059
#> 2: 0.00 0.2873552 0.09904166 0.02907645
#> 3: 0.00 0.7372533 0.74643591 0.21934372
#> 4: 81.95 0.7985184 2.62358791 0.77739420
#> 5: 81.95 0.7985184 2.62358791 0.77739420
#> ---
#> 2353: 0.00 0.3300279 0.12563780 0.03722768
#> 2354: 0.00 0.3300279 0.12563780 0.03722768
#> 2355: 130.01 0.9846119 2.30493478 0.67731599
#> 2356: 57.47 0.9770863 1.62164278 0.47607856
#> 2357: 57.47 0.9770863 1.62164278 0.47607856
#> predicted.mean.spending predicted.period.spending predicted.CLV
#> <num> <num> <num>
#> 1: 23.79897 34.202288 10.041037
#> 2: 35.37092 3.503195 1.028461
#> 3: 28.05774 20.943307 6.154290
#> 4: 17.38964 45.623245 13.518604
#> 5: 17.38964 45.623245 13.518604
#> ---
#> 2353: 35.37092 4.443925 1.316777
#> 2354: 35.37092 4.443925 1.316777
#> 2355: 22.80276 52.558874 15.444674
#> 2356: 26.64622 43.210656 12.685696
#> 2357: 26.64622 43.210656 12.685696
#>
# 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.5947 11.0512 0.5653 9.9591
#> 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.4712 9.1052 1.0972 31.9163
#> 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.5476 9.7393 0.7519 14.2960
#> 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.5502 10.1761 0.6580 12.0356
#> 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.6333 11.9508 0.9327 18.8618
#> KKT1: TRUE
#> KKT2: TRUE
#>
#> Used Options:
#> Correlation: FALSE
#>
# }