View source: R/f_interface_bootstrappedapply.R
clv.bootstrapped.apply | R Documentation |
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, ...)
object |
Fitted model |
num.boots |
number of times to sample data and re-fit the model |
fn.boot.apply |
Method to apply on each model estimated on the sampled data. See examples. |
fn.sample |
Method sampling customer ids for creating the bootstrapped data. Receives and returns
a vector of ids (string). If |
... |
Passed to the model estimation method. See examples. |
Returns a list containing the results of fn.boot.apply
For possible inputs to ...
see pnbd, ggomnbd, bgnbd.
Internal methods clv.data.create.bootstrapping.data
to create a clv.data
object of given customer ids and clv.fitted.estimate.same.specification.on.new.data
to
estimate a model again on new data with its original specification.
data("cdnow")
clv.cdnow <- clvdata(data.transactions = cdnow, date.format="ymd",
time.unit = "weeks", estimation.split=37)
pnbd.cdnow <- pnbd(clv.cdnow)
# 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)})
# 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)})
# 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))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.