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 ale()

...

not used. Inserted to require explicit naming of subsequent arguments.

model_call_string

character string. If NULL, model_bootstrap() tries to automatically detect and construct the call for bootstrapped datasets. If it cannot, the function will fail early. In that case, a character string of the full call for the model must be provided that includes boot_data as the data argument for the call. See examples.

model_call_string_vars

character. Character vector of names of variables included in model_call_string that are not columns in data. If any such variables exist, they must be specified here or else parallel processing will produce an error. If parallelization is disabled with parallel = 0, then this is not a concern.

parallel

See documentation for ale()

model_packages

See documentation for ale()

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 data with no bootstrapping.

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 ale()

output

character vector. Which types of bootstraps to calculate and return:

  • 'ale': Calculate and return bootstrapped ALE data and plot.

  • 'model_stats': Calculate and return bootstrapped overall model statistics.

  • 'model_coefs': Calculate and return bootstrapped model coefficients.

  • 'boot_data': Return full data for all bootstrap iterations. This data will always be calculated because it is needed for the bootstrap averages. By default, it is not returned except if included in this output argument.

ale_options, tidy_options, glance_options

list of named arguments. Arguments to pass to the ale(), broom::tidy(), or broom::glance() functions, respectively, beyond (or overriding) the defaults. In particular, to obtain p-values for ALE statistics, see the details.

compact_plots

See documentation for ale()

silent

See documentation for ale()

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:

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:

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()






[Package ale version 0.3.0 Index]