feature_effects {effectplots} | R Documentation |
This is the main function of the package. By default, it calculates the following statistics per feature X over values/bins:
"y_mean": Average observed y
values. Used to assess descriptive associations
between response and features.
"pred_mean": Average predictions. Corresponds to "M Plots" (from "marginal") in Apley (2020). Shows the combined effect of X and other (correlated) features. The difference to average observed y values shows model bias.
"resid_mean": Average residuals. Calculated when
both y
and predictions are available. Useful to study model bias.
"pd": Partial dependence (Friedman, 2001): See partial_dependence()
.
Evaluated at bin averages, not at bin midpoints.
"ale": Accumulated local effects (Apley, 2020): See ale()
. Only for numeric X.
Additionally, corresponding counts/weights are calculated, and standard deviations of observed y and residuals.
Numeric X with more than discrete_m = 5
disjoint values are binned as in
graphics::hist()
via breaks
. Before calculating bins, outliers are capped
at +-2 IQR from the quartiles.
All averages and standard deviation are weighted by optional weights w
.
If you need only one specific statistic, you can use the simplified APIs of
feature_effects(object, ...)
## Default S3 method:
feature_effects(
object,
v,
data,
y = NULL,
pred = NULL,
pred_fun = stats::predict,
trafo = NULL,
which_pred = NULL,
w = NULL,
breaks = "Sturges",
right = TRUE,
discrete_m = 5L,
outlier_iqr = 2,
calc_pred = TRUE,
pd_n = 500L,
ale_n = 50000L,
ale_bin_size = 200L,
seed = NULL,
...
)
## S3 method for class 'ranger'
feature_effects(
object,
v,
data,
y = NULL,
pred = NULL,
pred_fun = NULL,
trafo = NULL,
which_pred = NULL,
w = NULL,
breaks = "Sturges",
right = TRUE,
discrete_m = 5L,
outlier_iqr = 2,
calc_pred = TRUE,
pd_n = 500L,
ale_n = 50000L,
ale_bin_size = 200L,
...
)
## S3 method for class 'explainer'
feature_effects(
object,
v = colnames(data),
data = object$data,
y = object$y,
pred = NULL,
pred_fun = object$predict_function,
trafo = NULL,
which_pred = NULL,
w = object$weights,
breaks = "Sturges",
right = TRUE,
discrete_m = 5L,
outlier_iqr = 2,
calc_pred = TRUE,
pd_n = 500L,
ale_n = 50000L,
ale_bin_size = 200L,
...
)
object |
Fitted model. |
... |
Further arguments passed to |
v |
Vector of variable names to calculate statistics. |
data |
Matrix or data.frame. |
y |
Numeric vector with observed values of the response.
Can also be a column name in |
pred |
Numeric vector with predictions. If |
pred_fun |
Prediction function, by default |
trafo |
How should predictions be transformed?
A function or |
which_pred |
If the predictions are multivariate: which column to pick
(integer or column name). By default |
w |
Optional vector with case weights. Can also be a column name in |
breaks |
An integer, vector, string or function specifying the bins
of the numeric X variables as in |
right |
Should bins be right-closed? The default is |
discrete_m |
Numeric X variables with up to this number of unique values
should not be binned and treated as a factor (after calculating partial dependence)
The default is 5. Vectorized over |
outlier_iqr |
Outliers of a numeric X are capped via the boxplot rule, i.e.,
outside |
calc_pred |
Should predictions be calculated? Default is |
pd_n |
Size of the data used for calculating partial dependence.
The default is 500. For larger |
ale_n |
Size of the data used for calculating ALE.
The default is 50000. For larger |
ale_bin_size |
Maximal number of observations used per bin for ALE calculations.
If there are more observations in a bin, |
seed |
Optional random seed (an integer) used for:
|
A list (of class "EffectData") with a data.frame of statistics per feature. Use single bracket subsetting to select part of the output.
feature_effects(default)
: Default method.
feature_effects(ranger)
: Method for "ranger" models.
feature_effects(explainer)
: Method for DALEX "explainer".
Molnar, Christoph. 2019. Interpretable Machine Learning: A Guide for Making Black Box Models Explainable. https://christophm.github.io/interpretable-ml-book/.
Friedman, Jerome H. 2001, Greedy Function Approximation: A Gradient Boosting Machine. Annals of Statistics 29 (5): 1189-1232. doi:10.1214/aos/1013203451.3.
Apley, Daniel W., and Jingyu Zhu. 2016. Visualizing the Effects of Predictor Variables in Black Box Supervised Learning Models. Journal of the Royal Statistical Society Series B: Statistical Methodology, 82 (4): 1059–1086. doi:10.1111/rssb.12377.
plot.EffectData()
, update.EffectData()
, partial_dependence()
,
ale()
, average_observed, average_predicted()
, bias()
fit <- lm(Sepal.Length ~ ., data = iris)
xvars <- colnames(iris)[2:5]
M <- feature_effects(fit, v = xvars, data = iris, y = "Sepal.Length", breaks = 5)
M
M |> update(sort = "pd") |> plot(share_y = "all")