-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathREADME.Rmd
195 lines (146 loc) · 7.84 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
---
output: github_document
bibliography: ./inst/REFERENCES.bib
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%",
tidy = "styler"
)
```
# shapr <img src="man/figures/nrlogo_new.jpg" align="right" height="139"/>
<!-- badges: start -->
[](https://cran.r-project.org/package=shapr)
[](https://cran.r-project.org/package=shapr)
[](https://github.com/NorskRegnesentral/shapr/actions?query=workflow%3AR-CMD-check)
[](https://lifecycle.r-lib.org/articles/stages.html)
[](https://opensource.org/license/mit)
[](https://doi.org/10.21105/joss.02027)
See the pkgdown site at [norskregnesentral.github.io/shapr/](https://norskregnesentral.github.io/shapr/)
for a complete introduction with examples and documentation of the package.
<!-- badges: end -->
## NEWS
With `shapr` version 1.0.0 (GitHub only, Nov 2024) and version 1.0.1 (CRAN, Jan 2025),
the package was subject to a major update, providing a full restructuring of the code based, and
a full suit of new functionality, including:
* A long list of approaches for estimating the contribution/value function $v(S)$, including Variational Autoencoders,
and regression-based methods
* Iterative Shapley value estimation with convergence detection
* Parallelized computations with progress updates
* Reweighted Kernel SHAP for faster convergence
* New function `explain_forecast()` for explaining forecasts
* Several other methodological, computational and user-experience improvements
* Python wrapper making the core functionality of `shapr` available in Python
See the [NEWS](https://github.com/NorskRegnesentral/shapr/blob/master/NEWS.md) for a complete list.
### Coming from shapr < 1.0.0?
`shapr` version >= 1.0.0 comes with a number of breaking changes.
Most notably, we moved from using two function (`shapr()` and `explain()`) to
a single function (`explain()`).
In addition, custom models are now explained by passing the prediction function directly to `explain()`,
quite a few input arguments got new names, and a few functions for edge cases was removed to simplify the code base.
Click [here](https://github.com/NorskRegnesentral/shapr/blob/cranversion_0.2.2/README.md) to view a version of this
README with old syntax (v0.2.2).
### Python wrapper
We provide an (experimental) Python wrapper (`shaprpy`) which allows explaining Python models with the methodology
implemented in `shapr`, directly from Python.
The wrapper calls `R` internally, and therefore requires an installation of `R`.
See [here](https://github.com/NorskRegnesentral/shapr/shaprpy.html) for installation instructions and examples.
## The package
The `shapr` R package implements an enhanced version of the Kernel SHAP method, for approximating Shapley values,
with a strong focus on conditional Shapley values.
The core idea is to remain completely model-agnostic while offering a variety of methods for estimating contribution
functions, enabling accurate computation of conditional Shapley values across different feature types, dependencies,
and distributions.
The package also includes evaluation metrics to compare various approaches.
With features like parallelized computations, convergence detection, progress updates, and extensive plotting options,
shapr is as a highly efficient and user-friendly tool, delivering precise estimates of conditional Shapley values,
which are critical for understanding how features truly contribute to predictions.
A basic example is provided below.
Otherwise we refer to the [pkgdown website](https://norskregnesentral.github.io/shapr/) and the different vignettes
there for details and further examples.
## Installation
`shapr` is available on [CRAN](https://cran.r-project.org/package=shapr) and can be installed in R as:
```{r, eval = FALSE}
install.packages("shapr")
```
To install the development version of `shapr`, available on GitHub, use
```{r, eval = FALSE}
remotes::install_github("NorskRegnesentral/shapr")
```
To also install all dependencies, use
```{r, eval = FALSE}
remotes::install_github("NorskRegnesentral/shapr", dependencies = TRUE)
```
## Example
`shapr` supports computation of Shapley values with any predictive model which takes a set of numeric features and
produces a numeric outcome.
The following example shows how a simple `xgboost` model is trained using the *airquality* dataset, and how `shapr`
explains the individual predictions.
We first enable parallel computation and progress updates with the following code chunk.
These are optional, but recommended for improved performance and user friendliness,
particularly for problems with many features.
```{r init_no_eval,eval = FALSE}
# Enable parallel computation
# Requires the future and future_lapply packages
future::plan("multisession", workers = 2) # Increase the number of workers for increased performance with many features
# Enable progress updates of the v(S)-computations
# Requires the progressr package
progressr::handlers(global = TRUE)
progressr::handlers("cli") # Using the cli package as backend (recommended for the estimates of the remaining time)
```
Here comes the actual example
```{r basic_example, warning = FALSE}
library(xgboost)
library(shapr)
data("airquality")
data <- data.table::as.data.table(airquality)
data <- data[complete.cases(data), ]
x_var <- c("Solar.R", "Wind", "Temp", "Month")
y_var <- "Ozone"
ind_x_explain <- 1:6
x_train <- data[-ind_x_explain, ..x_var]
y_train <- data[-ind_x_explain, get(y_var)]
x_explain <- data[ind_x_explain, ..x_var]
# Looking at the dependence between the features
cor(x_train)
# Fitting a basic xgboost model to the training data
model <- xgboost(
data = as.matrix(x_train),
label = y_train,
nround = 20,
verbose = FALSE
)
# Specifying the phi_0, i.e. the expected prediction without any features
p0 <- mean(y_train)
# Computing the Shapley values with kernelSHAP accounting for feature dependence using
# the empirical (conditional) distribution approach with bandwidth parameter sigma = 0.1 (default)
explanation <- explain(
model = model,
x_explain = x_explain,
x_train = x_train,
approach = "empirical",
phi0 = p0
)
# Printing the Shapley values for the test data.
# For more information about the interpretation of the values in the table, see ?shapr::explain.
print(explanation$shapley_values_est)
# Finally we plot the resulting explanations
plot(explanation)
```
See the [general usage vignette](https://norskregnesentral.github.io/shapr/articles/general_usage.html) for further
basic usage examples and brief introductions to the methodology.
For more thorough information about the underlying methodology, see
@aas2019explaining, @redelmeier2020explaining, @jullum2021efficient, @olsen2022using, @olsen2024comparative .
See also @sellereite2019shapr for a brief paper about the previous (< 1.0.0) version of the package.
## Contribution
All feedback and suggestions are very welcome. Details on how to contribute can be found
[here](https://norskregnesentral.github.io/shapr/CONTRIBUTING.html). If you have any questions or comments, feel
free to open an issue [here](https://github.com/NorskRegnesentral/shapr/issues).
Please note that the 'shapr' project is released with a
[Contributor Code of Conduct](https://norskregnesentral.github.io/shapr/CODE_OF_CONDUCT.html).
By contributing to this project, you agree to abide by its terms.
## References