Learn R Programming

⚠️There's a newer version (0.9.7) of this package.Take me there.

{shapviz}

Overview

{shapviz} provides typical SHAP plots:

  • sv_importance(): Importance plot (bar/beeswarm).
  • sv_dependence() and sv_dependence2D(): Dependence plots to study feature effects and interactions.
  • sv_interaction(): Interaction plot (beeswarm).
  • sv_waterfall(): Waterfall plot to study single or average predictions.
  • sv_force(): Force plot as alternative to waterfall plot.

SHAP and feature values are stored in a "shapviz" object that is built from:

  1. Models that know how to calculate SHAP values: XGBoost, LightGBM, H2O (boosted trees).
  2. SHAP crunchers like {fastshap}, {kernelshap}, {treeshap}, {fastr}, and {DALEX}.
  3. SHAP matrix and corresponding feature values.

We use {patchwork} to glue together multiple plots with (potentially) inconsistent x and/or color scale.

Installation

# From CRAN
install.packages("shapviz")

# Or the newest version from GitHub:
# install.packages("devtools")
devtools::install_github("ModelOriented/shapviz")

Usage

Shiny diamonds... let's use XGBoost to model their prices by the four "C" variables:

library(shapviz)
library(ggplot2)
library(xgboost)

set.seed(1)

xvars <- c("log_carat", "cut", "color", "clarity")
X <- diamonds |> 
  transform(log_carat = log(carat)) |> 
  subset(select = xvars)

# Fit (untuned) model
fit <- xgb.train(
  params = list(learning_rate = 0.1), 
  data = xgb.DMatrix(data.matrix(X), label = log(diamonds$price)),
  nrounds = 65
)

# SHAP analysis: X can even contain factors
X_explain <- X[sample(nrow(X), 2000), ]
shp <- shapviz(fit, X_pred = data.matrix(X_explain), X = X_explain)

sv_importance(shp, show_numbers = TRUE)
sv_importance(shp, kind = "bee")
sv_dependence(shp, v = xvars)  # multiple plots -> patchwork

Decompositions of individual predictions can be visualized as waterfall or force plot:

sv_waterfall(shp, row_id = 2) +
  ggtitle("Waterfall plot for second prediction")
  
sv_force(shp, row_id = 2) +
  ggtitle("Force plot for second prediction")

More to Discover

Check-out the vignettes for topics like:

  • Basic use (includes working with other packages and SHAP interactions).
  • Multiple models, multi-output models, and subgroup analyses.
  • Plotting geographic effects.
  • Working with Tidymodels.

References

[1] Scott M. Lundberg and Su-In Lee. A Unified Approach to Interpreting Model Predictions. Advances in Neural Information Processing Systems 30 (2017).

Copy Link

Version

Install

install.packages('shapviz')

Monthly Downloads

3,562

Version

0.9.5

License

GPL (>= 2)

Issues

Pull Requests

Stars

Forks

Maintainer

Michael Mayer

Last Published

September 14th, 2024

Functions in shapviz (0.9.5)

potential_interactions

Interaction Strength
summary.shapviz

Summarizes "shapviz" Object
sv_waterfall

SHAP Waterfall Plot
print.mshapviz

Prints "mshapviz" Object
sv_force

SHAP Force Plot
sv_dependence2D

2D SHAP Dependence Plot
sv_dependence

SHAP Dependence Plot
sv_importance

SHAP Importance Plots
sv_interaction

SHAP Interaction Plot
miami

Miami-Dade County House Prices
c.shapviz

Concatenates "shapviz" Objects
dimnames<-.shapviz

Dimnames (Replacement Method) of "shapviz" Object
extractors

Extractor Functions
is.shapviz

Check for shapviz
is.mshapviz

Check for mshapviz
format_max

Number Formatter
collapse_shap

Collapse SHAP values
dimnames.shapviz

Dimnames of "shapviz" Object
dim.shapviz

Dimensions of "shapviz" Object
shapviz-package

shapviz: SHAP Visualizations
split.shapviz

Splits "shapviz" Object
print.shapviz

Prints "shapviz" Object
shapviz

Initialize "shapviz" Object
mshapviz

Combines compatible "shapviz" Objects
rbind.shapviz

Rowbinds Multiple "shapviz" or "mshapviz" Objects
+.shapviz

Rowbinds two "shapviz" Objects
[.shapviz

Subsets "shapviz" Object