fastshap computes fast, approximate Shapley values for any supervised learning model in R, using ideas from game theory to attribute a prediction to its input features.
pred_wrapper)lm()), and Tree SHAP for
xgboost and
lightgbm (exact = TRUE)explain_effects()) —
a deterministic, Monte-Carlo-free diagnostic that is exact for models with
at most pairwise interactionspred_wrapper() calls rather than
many small ones), an efficient C++ (Rcpp/RcppArmadillo) core, and optional
parallel execution across features via
foreach
fastshap is no longer available on CRAN due to CRAN's stringent and ever-changing policies. It is now hosted on r-universe, which provides a reliable alternative for distributing R packages.
# Latest stable release (recommended)
install.packages("fastshap", repos = c("https://bgreenwell.r-universe.dev", "https://cloud.r-project.org"))
# Or with pak
pak::pak("bgreenwell/fastshap@main") # latest stable release
pak::pak("bgreenwell/fastshap") # development version (devel branch)
library(fastshap)
# Fit a projection pursuit regression model
fit <- ppr(mpg ~ ., data = mtcars, nterms = 5)
# Prediction wrapper: takes `object` and `newdata`, returns a numeric vector
pfun <- function(object, newdata) predict(object, newdata = newdata)
# Approximate Shapley values via Monte Carlo simulation
set.seed(101)
shap <- explain(fit, X = subset(mtcars, select = -mpg), nsim = 50,
pred_wrapper = pfun)
head(shap)
# Deterministic main/total/interaction effect decomposition (no simulation)
eff <- explain_effects(fit, X = subset(mtcars, select = -mpg),
pred_wrapper = pfun)
head(eff$shapley_values)
Development happens on the devel
branch (the repository default); main holds stable releases, which is what
r-universe builds and the website documents. Please open pull requests against
devel and report bugs via the
issue tracker.