permshap | R Documentation |
Exact permutation SHAP algorithm with respect to a background dataset, see Strumbelj and Kononenko. The function works for up to 14 features.
permshap(object, ...)
## Default S3 method:
permshap(
object,
X,
bg_X,
pred_fun = stats::predict,
feature_names = colnames(X),
bg_w = NULL,
parallel = FALSE,
parallel_args = NULL,
verbose = TRUE,
...
)
## S3 method for class 'ranger'
permshap(
object,
X,
bg_X,
pred_fun = function(m, X, ...) stats::predict(m, X, ...)$predictions,
feature_names = colnames(X),
bg_w = NULL,
parallel = FALSE,
parallel_args = NULL,
verbose = TRUE,
...
)
object |
Fitted model object. |
... |
Additional arguments passed to |
X |
|
bg_X |
Background data used to integrate out "switched off" features,
often a subset of the training data (typically 50 to 500 rows)
It should contain the same columns as |
pred_fun |
Prediction function of the form |
feature_names |
Optional vector of column names in |
bg_w |
Optional vector of case weights for each row of |
parallel |
If |
parallel_args |
Named list of arguments passed to |
verbose |
Set to |
An object of class "kernelshap" with the following components:
S
: (n \times p)
matrix with SHAP values or, if the model output has
dimension K > 1
, a list of K
such matrices.
X
: Same as input argument X
.
baseline
: Vector of length K representing the average prediction on the
background data.
m_exact
: Integer providing the effective number of exact on-off vectors used.
exact
: Logical flag indicating whether calculations are exact or not
(currently TRUE
).
txt
: Summary text.
predictions
: (n \times K)
matrix with predictions of X
.
algorithm
: "permshap".
permshap(default)
: Default permutation SHAP method.
permshap(ranger)
: Permutation SHAP method for "ranger" models, see Readme for an example.
Erik Strumbelj and Igor Kononenko. Explaining prediction models and individual predictions with feature contributions. Knowledge and Information Systems 41, 2014.
# MODEL ONE: Linear regression
fit <- lm(Sepal.Length ~ ., data = iris)
# Select rows to explain (only feature columns)
X_explain <- iris[1:2, -1]
# Select small background dataset (could use all rows here because iris is small)
set.seed(1)
bg_X <- iris[sample(nrow(iris), 100), ]
# Calculate SHAP values
s <- permshap(fit, X_explain, bg_X = bg_X)
s
# MODEL TWO: Multi-response linear regression
fit <- lm(as.matrix(iris[, 1:2]) ~ Petal.Length + Petal.Width + Species, data = iris)
s <- permshap(fit, iris[1:4, 3:5], bg_X = bg_X)
s
# Non-feature columns can be dropped via 'feature_names'
s <- permshap(
fit,
iris[1:4, ],
bg_X = bg_X,
feature_names = c("Petal.Length", "Petal.Width", "Species")
)
s
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.