View source: R/additive_shap.R
additive_shap | R Documentation |
Exact additive SHAP assuming feature independence. The implementation works for models fitted via
lm()
,
glm()
,
mgcv::gam()
,
mgcv::bam()
,
gam::gam()
,
survival::coxph()
, and
survival::survreg()
.
additive_shap(object, X, verbose = TRUE, ...)
object |
Fitted model object. |
X |
Dataframe with rows to be explained. Will be used like
|
verbose |
Set to |
... |
Currently unused. |
The SHAP values are extracted via predict(object, newdata = X, type = "terms")
,
a logic heavily inspired by fastshap:::explain.lm(..., exact = TRUE)
.
Models with interactions (specified via :
or *
), or with terms of
multiple features like log(x1/x2)
are not supported.
Note that the SHAP values obtained by additive_shap()
are expected to
match those of permshap()
and kernelshap()
as long as their background
data equals the full training data (which is typically not feasible).
An object of class "kernelshap" with the following components:
S
: (n \times p)
matrix with SHAP values.
X
: Same as input argument X
.
baseline
: The baseline.
exact
: TRUE
.
txt
: Summary text.
predictions
: Vector with predictions of X
on the scale of "terms".
algorithm
: "additive_shap".
# MODEL ONE: Linear regression
fit <- lm(Sepal.Length ~ ., data = iris)
s <- additive_shap(fit, head(iris))
s
# MODEL TWO: More complicated (but not very clever) formula
fit <- lm(
Sepal.Length ~ poly(Sepal.Width, 2) + log(Petal.Length) + log(Sepal.Width),
data = iris
)
s_add <- additive_shap(fit, head(iris))
s_add
# Equals kernelshap()/permshap() when background data is full training data
s_kernel <- kernelshap(
fit, head(iris[c("Sepal.Width", "Petal.Length")]), bg_X = iris
)
all.equal(s_add$S, s_kernel$S)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.