ALEpDist | R Documentation |
ALE statistics are accompanied with two indicators of the confidence of their values. First, bootstrapping creates confidence intervals for ALE effects and ALE statistics to give a range of the possible ALE values. Second, we calculate p-values, an indicator of the probability that a given ALE statistic is random. An ALEpDist
S7 object contains the necessary distribution data for generating such p-values.
ALEpDist(
model,
data = NULL,
...,
y_col = NULL,
rand_it = NULL,
surrogate = FALSE,
parallel = "all",
model_packages = NULL,
random_model_call_string = NULL,
random_model_call_string_vars = character(),
positive = TRUE,
pred_fun = function(object, newdata, type = pred_type) {
stats::predict(object =
object, newdata = newdata, type = type)
},
pred_type = "response",
output_residuals = FALSE,
seed = 0,
silent = FALSE,
.skip_validation = FALSE
)
model |
See documentation for |
data |
See documentation for |
... |
not used. Inserted to require explicit naming of subsequent arguments. |
y_col |
See documentation for |
rand_it |
non-negative integer(1). Number of times that the model should be retrained with a new random variable. The default of |
surrogate |
logical(1). Create p-value distributions based on a surrogate linear model ( |
parallel |
See documentation for |
model_packages |
See documentation for |
random_model_call_string |
character(1). If |
random_model_call_string_vars |
See documentation for |
positive |
See documentation for |
pred_fun , pred_type |
See documentation for |
output_residuals |
logical(1). If |
seed |
See documentation for |
silent |
See documentation for |
.skip_validation |
Internal use only. logical(1). Skip non-mutating data validation checks. Changing the default |
An object of class ALEpDist
with properties rand_stats
, residual_distribution
, residuals
, and params
.
A named list of tibbles. There is normally one element whose name is the same as y_col
except if y_col
is a categorical variable; in that case, the elements are named for each category of y_col
. Each element is a tibble whose rows are each of the rand_it_ok
iterations of the random variable analysis and whose columns are the ALE statistics obtained for each random variable.
A univariateML
object with the closest estimated distribution for the residuals
as determined by univariateML::model_select()
. This is the distribution used to generate all the random variables.
If output_residuals == TRUE
, returns a matrix of the actual y_col
values from data
minus the predicted values from the model
(without random variables) on the data
. The rows correspond to each row of data
. The columns correspond to the named elements (y_col
or categories) described above for rand_stats
. NULL
if output_residuals == FALSE
(default).
Parameters used to generate p-value distributions. Most of these repeat selected arguments passed to ALEpDist()
. These are either values provided by the user or used by default if the user did not change them but the following additional or modified objects are notable:
* `model`: selected elements that describe the `model` used to generate the random distributions. * `rand_it`: the number of random iterations requested by the user either explicitly (by specifying a whole number) or implicitly with the default `NULL`: exact p distributions imply 1000 iterations and surrogate distributions imply 100 unless an explicit number of iterations is requested. * `rand_it_ok`: A whole number with the number of `rand_it` iterations that successfully generated a random variable, that is, those that did not fail for whatever reason. The `rand_it` - `rand_it_ok` failed attempts are discarded. * `exactness`: A string. For regular p-values generated from the original model, `'exact'` if `rand_it_ok >= 1000` and `'approx'` otherwise. `'surrogate'` for p-values generated from a surrogate model. `'invalid'` if `rand_it_ok < 100`.
Because ALE is non-parametric (that is, it does not assume any particular distribution of data), the {ale}
package takes a literal frequentist approach to the calculation of empirical (Monte Carlo) p-values. That is, it literally retrains the model 1000 times, each time modifying it by adding a distinct random variable to the model. (The number of iterations is customizable with the rand_it
argument.) The ALEs and ALE statistics are calculated for each random variable. The percentiles of the distribution of these random-variable ALEs are then used to determine p-values for non-random variables. Thus, p-values are interpreted as the frequency of random variable ALE statistics that exceed the value of ALE statistic of the actual variable in question. The specific steps are as follows:
The residuals of the original model trained on the training data are calculated (residuals are the actual y target value minus the predicted values).
The closest distribution of the residuals is detected with univariateML::model_select()
.
1000 new models are trained by generating a random variable each time with univariateML::rml()
and then training a new model with that random variable added.
The ALEs and ALE statistics are calculated for each random variable.
For each ALE statistic, the empirical cumulative distribution function (stats::ecdf()
) is used to create a function to determine p-values according to the distribution of the random variables' ALE statistics.
Because the ale
package is model-agnostic (that is, it works with any kind of R model), the ALEpDist()
constructor cannot always automatically manipulate the model object to create the p-values. It can only do so for models that follow the standard R statistical modelling conventions, which includes almost all base R algorithms (like stats::lm()
and stats::glm()
) and many widely used statistics packages (like mgcv
and survival
), but which excludes most machine learning algorithms (like tidymodels
and caret
). For non-standard algorithms, the user needs to do a little work to help the ALEpDist()
constructor correctly manipulate its model object:
The full model call must be passed as a character string in the argument random_model_call_string
, with two slight modifications as follows.
In the formula that specifies the model, you must add a variable named 'random_variable'. This corresponds to the random variables that the constructor will use to estimate p-values.
The dataset on which the model is trained must be named 'rand_data'. This corresponds to the modified datasets that will be used to train the random variables.
See the example below for how this is implemented.
If the model generation is unstable (because of a small dataset size or a finicky model algorithm), then one or more iterations might fail, possibly dropping the number of successful iterations to below 1000. Then the p-values are only considered approximate; they are no longer exact. If that is the case, then request rand_it at a sufficiently high number such that even if some iterations fail, at least 1000 will succeed. For example, for an ALEpDist
object named p_dist
, if p_dist@params$rand_it_ok
is 950, you could rerun ALEpDist()
with rand_it = 1100
or higher to allow for up to 100 possible failures.
The procedure we have just described requires at least 1000 random iterations for p-values to be considered "exact". Unfortunately, this procedure is rather slow–it takes at least 1000 times as long as the time it takes to train the model once.
With fewer iterations (at least 100), p-values can only be considered approximate ("approx"). Fewer than 100 such p-values are invalid. There might be fewer iterations either because the user requests them with the rand_it
argument or because some iterations fail for whatever reason. As long as at least 1000 iterations succeed, p-values will be considered exact.
Because the procedure can be very slow, a faster version of the algorithm generates "surrogate" p-values by substituting the original model
with a linear model that predicts the same y_col
outcome from all the other columns in data
. By default, these surrogate p-values use only 100 iterations and if the dataset is large, the surrogate model samples 1000 rows. Thus, the surrogate p-values can be generated much faster than for slower model algorithms on larger datasets. Although they are suitable for model development and analysis because they are faster to generate, they are less reliable than approximate p-values based on the original model. In any case, definitive conclusions (e.g., for publication) always require exact p-values with at least 1000 iterations on the original model. Note that surrogate p-values are always marked as "surrogate"; even if they are generated based on over 1000 iterations, they can never be considered exact because they are not based on the original model
.
Okoli, Chitu. 2023. "Statistical Inference Using Machine Learning and Classical Techniques Based on Accumulated Local Effects (ALE)." arXiv. doi:10.48550/arXiv.2310.09877.
# Sample 1000 rows from the ggplot2::diamonds dataset (for a simple example)
set.seed(0)
diamonds_sample <- ggplot2::diamonds[sample(nrow(ggplot2::diamonds), 1000), ]
# Create a GAM with flexible curves to predict diamond price
# Smooth all numeric variables and include all other variables
gam_diamonds <- mgcv::gam(
price ~ s(carat) + s(depth) + s(table) + s(x) + s(y) + s(z) +
cut + color + clarity +
ti(carat, by = clarity), # a 2D interaction
data = diamonds_sample
)
summary(gam_diamonds)
# Create p_value distribution
pd_diamonds <- ALEpDist(
gam_diamonds,
diamonds_sample,
# only 100 iterations for a quick demo; but usually should remain at 1000
rand_it = 100
)
# Examine the structure of the returned object
print(pd_diamonds)
# In RStudio: View(pd_diamonds)
# Calculate ALEs with p-values
ale_gam_diamonds <- ALE(
gam_diamonds,
p_values = pd_diamonds
)
# Plot the ALE data. The horizontal bands in the plots use the p-values.
plot(ale_gam_diamonds)
# For non-standard models that give errors with the default settings,
# you can use 'random_model_call_string' to specify a model for the estimation
# of p-values from random variables as in this example.
# See details above for an explanation.
pd_diamonds <- ALEpDist(
gam_diamonds,
diamonds_sample,
random_model_call_string = 'mgcv::gam(
price ~ s(carat) + s(depth) + s(table) + s(x) + s(y) + s(z) +
cut + color + clarity + random_variable,
data = rand_data
)',
# only 100 iterations for a quick demo; but usually should remain at 1000
rand_it = 100
)
# Examine the structure of the returned object
print(pd_diamonds)
# In RStudio: View(pd_diamonds)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.