model_bootstrap {ale} | R Documentation |
model_bootstrap.R
Description
Execute full model bootstrapping with ALE calculation on each bootstrap run
Usage
model_bootstrap(
data,
model,
...,
model_call_string = NULL,
model_call_string_vars = character(),
parallel = parallel::detectCores(logical = FALSE) - 1,
model_packages = as.character(NA),
boot_it = 100,
seed = 0,
boot_alpha = 0.05,
boot_centre = "mean",
output = c("ale", "model_stats", "model_coefs"),
ale_options = list(),
tidy_options = list(),
glance_options = list(),
compact_plots = FALSE,
silent = FALSE
)
Arguments
data |
dataframe. Dataset that will be bootstrapped. |
model |
See documentation for |
... |
not used. Inserted to require explicit naming of subsequent arguments. |
model_call_string |
character string. If NULL, |
model_call_string_vars |
character. Character vector of names of variables
included in |
parallel |
See documentation for |
model_packages |
See documentation for |
boot_it |
integer from 0 to Inf. Number of bootstrap iterations.
If boot_it = 0, then the model is run as normal once on the full |
seed |
integer. Random seed. Supply this between runs to assure identical bootstrap samples are generated each time on the same data. |
boot_alpha |
numeric. The confidence level for the bootstrap confidence intervals is 1 - boot_alpha. For example, the default 0.05 will give a 95% confidence interval, that is, from the 2.5% to the 97.5% percentile. |
boot_centre |
See See documentation for |
output |
character vector. Which types of bootstraps to calculate and return:
|
ale_options , tidy_options , glance_options |
list of named arguments.
Arguments to pass to the |
compact_plots |
See documentation for |
silent |
See documentation for |
Details
No modelling results, with or without ALE, should be considered reliable without
being bootstrapped. For large datasets, normally the model provided to ale()
is the final deployment model that has been validated and evaluated on
training and testing on subsets; that is why ale()
is calculated on the full
dataset. However, when a dataset is too small to be subdivided into training
and test sets for a standard machine learning process, then the entire model
should be bootstrapped. That is, multiple models should be trained, one on
each bootstrap sample. The reliable results are the average results of all
the bootstrap models, however many there are. For details, see the vignette
on small datasets or the details and examples below.
model_bootstrap()
automatically carries out full-model bootstrapping suitable
for small datasets. Specifically, it:
Creates multiple bootstrap samples (default 100; the user can specify any number);
Creates a model on each bootstrap sample;
Calculates model overall statistics, variable coefficients, and ALE values for each model on each bootstrap sample;
Calculates the mean, median, and lower and upper confidence intervals for each of those values across all bootstrap samples.
P-values
The broom::tidy()
summary statistics will provide p-values as normal, but the
situation is somewhat complicated with p-values for ALE statistics. The challenge
is that the procedure for obtaining their p-values is very slow: it involves
retraining the model 1000 times. Thus, it is not efficient to calculate p-values
on every execution of model_bootstrap()
. Although the ale()
function provides
an 'auto' option for creating p-values,
that option is disabled in model_bootstrap()
because it would be far too slow:
it would involve retraining the model 1000 times the number of bootstrap iterations.
Rather, you must first create a p-values function object using the procedure
described in help(create_p_funs)
. If the name of your p-values object is
p_funs
, you can then request p-values each time you run model_bootstrap()
by passing it the argument ale_options = list(p_values = p_funs)
.
Value
list with tibbles of the following elements (depending on values requested in
the output
argument:
model_stats: bootstrapped results from
broom::glance()
model_coefs: bootstrapped results from
broom::tidy()
ale: bootstrapped ALE results
data: ALE data (see
ale()
for details about the format)stats: ALE statistics. The same data is duplicated with different views that might be variously useful. The column
by_term: statistic, estimate, conf.low, median, mean, conf.high. ("term" means variable name.) The column names are compatible with the
broom
package. The confidence intervals are based on theale()
function defaults; they can be changed with theale_options
argument. The estimate is the median or the mean, depending on theboot_centre
argument.by_statistic: term, estimate, conf.low, median, mean, conf.high.
estimate: term, then one column per statistic Provided with the default estimate. This view does not present confidence intervals.
plots: ALE plots (see
ale()
for details about the format)
boot_data: full bootstrap data (not returned by default)
other values: the
boot_it
,seed
,boot_alpha
, andboot_centre
arguments that were originally passed are returned for reference.
References
Okoli, Chitu. 2023. “Statistical Inference Using Machine Learning and Classical Techniques Based on Accumulated Local Effects (ALE).” arXiv. https://arxiv.org/abs/2310.09877.
Examples
# attitude dataset
attitude
## ALE for general additive models (GAM)
## GAM is tweaked to work on the small dataset.
gam_attitude <- mgcv::gam(rating ~ complaints + privileges + s(learning) +
raises + s(critical) + advance,
data = attitude)
summary(gam_attitude)
# Full model bootstrapping
# Only 4 bootstrap iterations for a rapid example; default is 100
# Increase value of boot_it for more realistic results
mb_gam <- model_bootstrap(
attitude,
gam_attitude,
boot_it = 4,
parallel = 2 # CRAN limit (delete this line on your own computer)
)
# If the model is not standard, supply model_call_string with
# 'data = boot_data' in the string (not as a direct argument to [model_bootstrap()])
mb_gam <- model_bootstrap(
attitude,
gam_attitude,
model_call_string = 'mgcv::gam(
rating ~ complaints + privileges + s(learning) +
raises + s(critical) + advance,
data = boot_data
)',
boot_it = 4,
parallel = 2 # CRAN limit (delete this line on your own computer)
)
# Model statistics and coefficients
mb_gam$model_stats
mb_gam$model_coefs
# Plot ALE
mb_gam$ale$plots |>
patchwork::wrap_plots()