kernelshap: Kernel SHAP

Michael Mayer, David WatsonOpenalex Unknown 2022 · DOI · OpenAlex

Abstract

Efficient implementation of Kernel SHAP (Lundberg and Lee, 2017), permutation SHAP, and additive SHAP for model interpretability. For Kernel SHAP and permutation SHAP, if the number of features is too large for exact calculations, the algorithms iterate until the SHAP values are sufficiently precise in terms of their standard errors. The package integrates smoothly with meta-learning packages such as 'tidymodels', 'caret' or 'mlr3'. It supports multi-output models, case weights, and parallel computations. Visualizations can be done using the R package 'shapviz'.

Introduction

Calculating Shapley values—the gold standard for model-agnostic feature attribution—is computationally expensive because the number of possible feature coalitions grows exponentially with the number of features ($2^p$). While Kernel SHAP approximates these values using linear regression, existing tools often struggle with balancing speed and precision, especially for models with many features or multiple outputs. The kernelshap package addresses this by providing an efficient R implementation that integrates with modern machine learning workflows and offers statistical guarantees on the precision of the estimated SHAP values.

Core Algorithms

The package implements three primary algorithms to facilitate model interpretability across various use cases:

  1. Kernel SHAP (Hybrid Approach): For models with 8 or fewer features, the package calculates exact SHAP values. For larger feature sets, it employs a "hybrid degree" strategy. It first calculates exact contributions for low-order coalitions (e.g., single features or pairs) and then uses iterative paired sampling for the remaining "mass" of the Shapley distribution.
  2. Permutation SHAP: This uses an antithetic sampling scheme that evaluates Shapley’s formula twice per feature using a single permutation, significantly reducing the variance of the estimates compared to simple random sampling.
  3. Additive SHAP: For purely additive models (like GLMs or GAMs), the package provides an "exact" shortcut by extracting terms directly from the model's prediction logic, assuming feature independence.
The kernelshap function parameters allow for fine-grained control over the hybrid sampling strategy and background data.

Precision and Performance

A critical innovation in this implementation is the precision-based stopping criterion. Instead of running for a fixed number of iterations, the algorithms iterate until the standard error of the SHAP values falls below a specified tolerance (tol), ensuring the results are statistically reliable. This allows users to trade off computation time for precision in a principled way.

Furthermore, the package is designed for high-performance environments. It supports parallel processing via the foreach package and works out-of-the-box with ranger random forests and various meta-learning frameworks like tidymodels and mlr3. Unlike many implementations that handle only single-label regression, kernelshap natively supports multi-output models, such as multi-class classification.

The package uses a tolerance-based stopping criterion (tol) and supports parallelization to handle complex models efficiently.

Key Insights

  • The Power of "Partly Exact": By combining exact calculations for small coalitions with sampling for the rest, the hybrid algorithm achieves faster convergence than pure sampling methods.
  • Standard Errors as a Compass: Monitoring standard errors during iteration provides a robust metric for convergence, moving beyond arbitrary iteration counts.
  • Seamless Integration: The package is a "plug-and-play" solution for the R ecosystem, supporting custom prediction functions and case weights.

Limitations

While powerful, the package has certain constraints. The additive_shap function does not currently support models with interactions (e.g., x1:x2). Additionally, calculating SHAP values for datasets with hundreds of features remains computationally demanding, and results remain sensitive to the choice of "background data" used to represent the "off" state of features. The package defaults to a sample of 200 rows, which users must evaluate based on their specific data distribution.

Summary updated

Abstract

Efficient implementation of Kernel SHAP (Lundberg and Lee, 2017, &lt;<a href="https://doi.org/10.48550%2FarXiv.1705.07874" target="_top">doi:10.48550/arXiv.1705.07874</a>&gt;) permutation SHAP, and additive SHAP for model interpretability. For Kernel SHAP and permutation SHAP, if the number of features is too large for exact calculations, the algorithms iterate until the SHAP values are sufficiently precise in terms of their standard errors. The package integrates smoothly with meta-learning packages such as 'tidymodels', 'caret' or 'mlr3'. It supports multi-output models, case weights, and parallel computations. Visualizations can be done using the R package 'shapviz'.
BibTeX
@inproceedings{mayer2022kernelshap,
  author = {Michael Mayer and David Watson},
  title = {{kernelshap: Kernel SHAP}},
  booktitle = {Openalex Unknown},
  year = {2022},
  doi = {10.32614/cran.package.kernelshap},
}
Figure