Skip to content

Commit 55f107c

Browse files
committed
Docstrings
1 parent 78da770 commit 55f107c

File tree

5 files changed

+43
-45
lines changed

5 files changed

+43
-45
lines changed

R/kernelshap.R

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
#' @description
44
#' Efficient implementation of Kernel SHAP, see Lundberg and Lee (2017), and
55
#' Covert and Lee (2021), abbreviated by CL21.
6-
#' By default, for up to p=8 features, exact Kernel SHAP values are returned
6+
#' By default, for up to p=8 features, exact SHAP values are returned
77
#' (with respect to the selected background data).
8-
#' Otherwise, an almost exact hybrid algorithm combining exact calculations and
8+
#' Otherwise, a partly exact hybrid algorithm combining exact calculations and
99
#' iterative paired sampling is used, see Details.
1010
#'
1111
#' @details
@@ -41,10 +41,9 @@
4141
#' 2. Step 2 (sampling part): The remaining weight is filled by sampling vectors z
4242
#' according to Kernel SHAP weights normalized to the values not yet covered by Step 1.
4343
#' Together with the results from Step 1 - correctly weighted - this now forms a
44-
#' complete iteration as in CL21. The difference is that most mass is covered by exact
45-
#' calculations. Afterwards, the algorithm iterates until convergence.
46-
#' The output of Step 1 is reused in every iteration, leading to an extremely
47-
#' efficient strategy.
44+
#' complete iteration as in CL21. The difference is that a significant part of the mass
45+
#' is covered by exact calculations. Afterwards, the algorithm iterates until
46+
#' convergence. The output of Step 1 is reused in every iteration.
4847
#'
4948
#' If \eqn{p} is sufficiently small, all possible \eqn{2^p-2} on-off vectors \eqn{z} can be
5049
#' evaluated. In this case, no sampling is required and the algorithm returns exact
@@ -76,7 +75,7 @@
7675
#' @param bg_w Optional vector of case weights for each row of `bg_X`.
7776
#' If `bg_X = NULL`, must be of same length as `X`. Set to `NULL` for no weights.
7877
#' @param bg_n If `bg_X = NULL`: Size of background data to be sampled from `X`.
79-
#' @param exact If `TRUE`, the algorithm will produce exact Kernel SHAP values
78+
#' @param exact If `TRUE`, the algorithm will produce exact SHAP values
8079
#' with respect to the background data.
8180
#' The default is `TRUE` for up to eight features, and `FALSE` otherwise.
8281
#' @param hybrid_degree Integer controlling the exactness of the hybrid strategy. For
@@ -89,7 +88,7 @@
8988
#' for the exact part. The remaining mass is covered by random sampling.
9089
#' - `2`: Uses all \eqn{p(p+1)} on-off vectors \eqn{z} with
9190
#' \eqn{\sum z \in \{1, 2, p-2, p-1\}}. The remaining mass is covered by sampling.
92-
#' Convergence usually happens very fast.
91+
#' Usually converges fast.
9392
#' - `k>2`: Uses all on-off vectors with
9493
#' \eqn{\sum z \in \{1, \dots, k, p-k, \dots, p-1\}}.
9594
#' @param m Even number of on-off vectors sampled during one iteration.

R/permshap.R

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,35 @@
66
#'
77
#' By default, for up to p=8 features, exact SHAP values are returned
88
#' (exact with respect to the selected background data).
9-
#'
109
#' Otherwise, the sampling process iterates until the resulting values
1110
#' are sufficiently precise, and standard errors are provided.
1211
#'
12+
#' @details
1313
#' During each iteration, the algorithm cycles twice through a random permutation:
1414
#' It starts with all feature components "turned on" (i.e., taking them
1515
#' from the observation to be explained), then gradually turning off components
16-
#' according to the permutation (i.e., marginalizing them over the background data).
16+
#' according to the permutation.
1717
#' When all components are turned off, the algorithm - one by one - turns the components
1818
#' back on, until all components are turned on again. This antithetic scheme allows to
19-
#' evaluate Shapley's formula 2p times with each permutation, using a total of
20-
#' 2p + 1 evaluations of marginal means.
19+
#' evaluate Shapley's formula twice per feature using a single permutation and a total
20+
#' of 2p disjoint evaluations of the contribution function.
2121
#'
2222
#' For models with interactions up to order two, one can show that
23-
#' even a single iteration provides exact SHAP values (with respect to the
24-
#' given background dataset).
23+
#' even a single iteration provides exact SHAP values for all features
24+
#' (with respect to the given background dataset).
2525
#'
2626
#' The Python implementation "shap" uses a similar approach, but without
27-
#' providing standard errors, and without early stopping. To mimic its behavior,
28-
#' we would need to set `max_iter = p` in R, and `max_eval = (2*p+1)*p` in Python.
27+
#' providing standard errors, and without early stopping.
2928
#'
3029
#' For faster convergence, we use balanced permutations in the sense that
3130
#' p subsequent permutations each start with a different feature.
3231
#' Furthermore, the 2p on-off vectors with sum <=1 or >=p-1 are evaluated only once,
33-
#' similar to the degree 1 hybrid in [kernelshap()] (but covering less weight).
32+
#' similar to the degree 1 hybrid in [kernelshap()].
3433
#'
35-
#' @param exact If `TRUE`, the algorithm produces exact SHAP values
36-
#' with respect to the background data.
37-
#' The default is `TRUE` for up to eight features, and `FALSE` otherwise.
38-
#' @param low_memory If `FALSE` (default up to p = 15), the algorithm evaluates p
39-
#' predictions together, reducing the number of calls to `predict()`.
34+
#' @param low_memory If `FALSE` (default up to p = 15), the algorithm does p
35+
#' iterations in one chunk, evaluating Shapley's formula 2p^2 times.
36+
#' For models with interactions up to order two, you can set this to `TRUE`
37+
#' to save time.
4038
#' @inheritParams kernelshap
4139
#' @returns
4240
#' An object of class "kernelshap" with the following components:

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
The package contains three functions to crunch SHAP values:
1717

1818
- **`permshap()`**: Permutation SHAP algorithm of [1]. Both exact and sampling versions are available.
19-
- **`kernelshap()`**: Kernel SHAP algorithm of [2] and [3]. Both exact and (pseudo-exact) sampling versions are available.
19+
- **`kernelshap()`**: Kernel SHAP algorithm of [2] and [3]. Both exact and (partly exact) sampling versions are available.
2020
- **`additive_shap()`**: For *additive models* fitted via `lm()`, `glm()`, `mgcv::gam()`, `mgcv::bam()`, `gam::gam()`, `survival::coxph()`, or `survival::survreg()`. Exponentially faster than the model-agnostic options above, and recommended if possible.
2121

2222
To explain your model, select an explanation dataset `X` (up to 1000 rows from the training data, feature columns only). Use {shapviz} to visualize the resulting SHAP values.
@@ -25,8 +25,9 @@ To explain your model, select an explanation dataset `X` (up to 1000 rows from t
2525

2626
- Both algorithms need a representative background data `bg_X` to calculate marginal means (up to 500 rows from the training data). In cases with a natural "off" value (like MNIST digits), this can also be a single row with all values set to the off value. If unspecified, 200 rows are randomly sampled from `X`.
2727
- Exact Kernel SHAP gives identical results as exact permutation SHAP. Both algorithms are fast up to 8 features.
28-
With more features, `kernelshap()` switches to an almost exact algorithm with faster convergence than the sampling version of permutation SHAP.
28+
With more features, `kernelshap()` switches to a partly exact algorithm with faster convergence than the sampling version of permutation SHAP.
2929
- For models with interactions of order up to two, the sampling versions provide the same results as the exact versions.
30+
- Sampling versions iterate until standard errors of SHAP values are sufficiently small.
3031
- For additive models, `permshap()` and `kernelshap()` give the same results as `additive_shap`
3132
as long as the full training data would be used as background data.
3233

man/kernelshap.Rd

Lines changed: 7 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/permshap.Rd

Lines changed: 14 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)