fastshap

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.

Installation

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)

Quick start

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)

Documentation

Development

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.