explain | R Documentation |
Computes dependence-aware Shapley values for observations in x_explain
from the specified
model
by using the method specified in approach
to estimate the conditional expectation.
explain(
model,
x_explain,
x_train,
approach,
prediction_zero,
n_combinations = NULL,
group = NULL,
n_samples = 1000,
n_batches = NULL,
seed = 1,
keep_samp_for_vS = FALSE,
predict_model = NULL,
get_model_specs = NULL,
MSEv_uniform_comb_weights = TRUE,
timing = TRUE,
verbose = 0,
...
)
model |
The model whose predictions we want to explain.
Run |
x_explain |
A matrix or data.frame/data.table. Contains the the features, whose predictions ought to be explained. |
x_train |
Matrix or data.frame/data.table. Contains the data used to estimate the (conditional) distributions for the features needed to properly estimate the conditional expectations in the Shapley formula. |
approach |
Character vector of length |
prediction_zero |
Numeric. The prediction value for unseen data, i.e. an estimate of the expected prediction without conditioning on any features. Typically we set this value equal to the mean of the response variable in our training data, but other choices such as the mean of the predictions in the training data are also reasonable. |
n_combinations |
Integer.
If |
group |
List.
If |
n_samples |
Positive integer. Indicating the maximum number of samples to use in the Monte Carlo integration for every conditional expectation. See also details. |
n_batches |
Positive integer (or NULL).
Specifies how many batches the total number of feature combinations should be split into when calculating the
contribution function for each test observation.
The default value is NULL which uses a reasonable trade-off between RAM allocation and computation speed,
which depends on |
seed |
Positive integer.
Specifies the seed before any randomness based code is being run.
If |
keep_samp_for_vS |
Logical.
Indicates whether the samples used in the Monte Carlo estimation of v_S should be returned
(in |
predict_model |
Function.
The prediction function used when |
get_model_specs |
Function.
An optional function for checking model/data consistency when
If |
MSEv_uniform_comb_weights |
Logical. If |
timing |
Logical.
Whether the timing of the different parts of the |
verbose |
An integer specifying the level of verbosity. If |
... |
Arguments passed on to
|
The most important thing to notice is that shapr
has implemented six different
approaches for estimating the conditional distributions of the data, namely "empirical"
,
"gaussian"
, "copula"
, "ctree"
, "vaeac"
, "categorical"
, "timeseries"
, and "independence"
.
In addition, the user also has the option of combining the different approaches.
E.g., if you're in a situation where you have trained a model that consists of 10 features,
and you'd like to use the "gaussian"
approach when you condition on a single feature,
the "empirical"
approach if you condition on 2-5 features, and "copula"
version
if you condition on more than 5 features this can be done by simply passing
approach = c("gaussian", rep("empirical", 4), rep("copula", 4))
. If
"approach[i]" = "gaussian"
means that you'd like to use the "gaussian"
approach
when conditioning on i
features. Conditioning on all features needs no approach as that is given
by the complete prediction itself, and should thus not be part of the vector.
For approach="ctree"
, n_samples
corresponds to the number of samples
from the leaf node (see an exception related to the sample
argument).
For approach="empirical"
, n_samples
is the K
parameter in equations (14-15) of
Aas et al. (2021), i.e. the maximum number of observations (with largest weights) that is used, see also the
empirical.eta
argument.
Object of class c("shapr", "list")
. Contains the following items:
data.table with the estimated Shapley values
List with the different parameters, data and functions used internally
Numeric vector with the predictions for the explained observations
List with the values of the MSEv evaluation criterion for the approach.
shapley_values
is a data.table where the number of rows equals
the number of observations you'd like to explain, and the number of columns equals m +1
,
where m
equals the total number of features in your model.
If shapley_values[i, j + 1] > 0
it indicates that the j-th feature increased the prediction for
the i-th observation. Likewise, if shapley_values[i, j + 1] < 0
it indicates that the j-th feature
decreased the prediction for the i-th observation.
The magnitude of the value is also important to notice. E.g. if shapley_values[i, k + 1]
and
shapley_values[i, j + 1]
are greater than 0
, where j != k
, and
shapley_values[i, k + 1]
> shapley_values[i, j + 1]
this indicates that feature
j
and k
both increased the value of the prediction, but that the effect of the k-th
feature was larger than the j-th feature.
The first column in dt
, called none
, is the prediction value not assigned to any of the features
(\phi
0).
It's equal for all observations and set by the user through the argument prediction_zero
.
The difference between the prediction and none
is distributed among the other features.
In theory this value should be the expected prediction without conditioning on any features.
Typically we set this value equal to the mean of the response variable in our training data, but other choices
such as the mean of the predictions in the training data are also reasonable.
Martin Jullum
Aas, K., Jullum, M., & L<U+00F8>land, A. (2021). Explaining individual predictions when features are dependent: More accurate approximations to Shapley values. Artificial Intelligence, 298, 103502.
# Load example data
data("airquality")
airquality <- airquality[complete.cases(airquality), ]
x_var <- c("Solar.R", "Wind", "Temp", "Month")
y_var <- "Ozone"
# Split data into test- and training data
data_train <- head(airquality, -3)
data_explain <- tail(airquality, 3)
x_train <- data_train[, x_var]
x_explain <- data_explain[, x_var]
# Fit a linear model
lm_formula <- as.formula(paste0(y_var, " ~ ", paste0(x_var, collapse = " + ")))
model <- lm(lm_formula, data = data_train)
# Explain predictions
p <- mean(data_train[, y_var])
# Empirical approach
explain1 <- explain(
model = model,
x_explain = x_explain,
x_train = x_train,
approach = "empirical",
prediction_zero = p,
n_samples = 1e2
)
# Gaussian approach
explain2 <- explain(
model = model,
x_explain = x_explain,
x_train = x_train,
approach = "gaussian",
prediction_zero = p,
n_samples = 1e2
)
# Gaussian copula approach
explain3 <- explain(
model = model,
x_explain = x_explain,
x_train = x_train,
approach = "copula",
prediction_zero = p,
n_samples = 1e2
)
# ctree approach
explain4 <- explain(
model = model,
x_explain = x_explain,
x_train = x_train,
approach = "ctree",
prediction_zero = p,
n_samples = 1e2
)
# Combined approach
approach <- c("gaussian", "gaussian", "empirical")
explain5 <- explain(
model = model,
x_explain = x_explain,
x_train = x_train,
approach = approach,
prediction_zero = p,
n_samples = 1e2
)
# Print the Shapley values
print(explain1$shapley_values)
# Plot the results
if (requireNamespace("ggplot2", quietly = TRUE)) {
plot(explain1)
plot(explain1, plot_type = "waterfall")
}
# Group-wise explanations
group_list <- list(A = c("Temp", "Month"), B = c("Wind", "Solar.R"))
explain_groups <- explain(
model = model,
x_explain = x_explain,
x_train = x_train,
group = group_list,
approach = "empirical",
prediction_zero = p,
n_samples = 1e2
)
print(explain_groups$shapley_values)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.