| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/xgb.plot.shap.R |
| 3 |
\name{xgb.plot.shap} |
| 4 |
\alias{xgb.plot.shap} |
| 5 |
\title{SHAP dependence plots} |
| 6 |
\usage{ |
| 7 |
xgb.plot.shap( |
| 8 |
data, |
| 9 |
shap_contrib = NULL, |
| 10 |
features = NULL, |
| 11 |
top_n = 1, |
| 12 |
model = NULL, |
| 13 |
trees = NULL, |
| 14 |
target_class = NULL, |
| 15 |
approxcontrib = FALSE, |
| 16 |
subsample = NULL, |
| 17 |
n_col = 1, |
| 18 |
col = rgb(0, 0, 1, 0.2), |
| 19 |
pch = ".", |
| 20 |
discrete_n_uniq = 5, |
| 21 |
discrete_jitter = 0.01, |
| 22 |
ylab = "SHAP", |
| 23 |
plot_NA = TRUE, |
| 24 |
col_NA = rgb(0.7, 0, 1, 0.6), |
| 25 |
pch_NA = ".", |
| 26 |
pos_NA = 1.07, |
| 27 |
plot_loess = TRUE, |
| 28 |
col_loess = 2, |
| 29 |
span_loess = 0.5, |
| 30 |
which = c("1d", "2d"), |
| 31 |
plot = TRUE, |
| 32 |
... |
| 33 |
) |
| 34 |
} |
| 35 |
\arguments{ |
| 36 |
\item{data}{The data to explain as a \code{matrix}, \code{dgCMatrix}, or \code{data.frame}.} |
| 37 |
|
| 38 |
\item{shap_contrib}{Matrix of SHAP contributions of \code{data}. |
| 39 |
The default (\code{NULL}) computes it from \code{model} and \code{data}.} |
| 40 |
|
| 41 |
\item{features}{Vector of column indices or feature names to plot. When \code{NULL} |
| 42 |
(default), the \code{top_n} most important features are selected by \code{\link[=xgb.importance]{xgb.importance()}}.} |
| 43 |
|
| 44 |
\item{top_n}{How many of the most important features (<= 100) should be selected? |
| 45 |
By default 1 for SHAP dependence and 10 for SHAP summary. |
| 46 |
Only used when \code{features = NULL}.} |
| 47 |
|
| 48 |
\item{model}{An \code{xgb.Booster} model. Only required when \code{shap_contrib = NULL} or |
| 49 |
\code{features = NULL}.} |
| 50 |
|
| 51 |
\item{trees}{Passed to \code{\link[=xgb.importance]{xgb.importance()}} when \code{features = NULL}.} |
| 52 |
|
| 53 |
\item{target_class}{Only relevant for multiclass models. The default (\code{NULL}) |
| 54 |
averages the SHAP values over all classes. Pass a (0-based) class index |
| 55 |
to show only SHAP values of that class.} |
| 56 |
|
| 57 |
\item{approxcontrib}{Passed to \code{\link[=predict.xgb.Booster]{predict.xgb.Booster()}} when \code{shap_contrib = NULL}.} |
| 58 |
|
| 59 |
\item{subsample}{Fraction of data points randomly picked for plotting. |
| 60 |
The default (\code{NULL}) will use up to 100k data points.} |
| 61 |
|
| 62 |
\item{n_col}{Number of columns in a grid of plots.} |
| 63 |
|
| 64 |
\item{col}{Color of the scatterplot markers.} |
| 65 |
|
| 66 |
\item{pch}{Scatterplot marker.} |
| 67 |
|
| 68 |
\item{discrete_n_uniq}{Maximal number of unique feature values to consider the |
| 69 |
feature as discrete.} |
| 70 |
|
| 71 |
\item{discrete_jitter}{Jitter amount added to the values of discrete features.} |
| 72 |
|
| 73 |
\item{ylab}{The y-axis label in 1D plots.} |
| 74 |
|
| 75 |
\item{plot_NA}{Should contributions of cases with missing values be plotted? |
| 76 |
Default is \code{TRUE}.} |
| 77 |
|
| 78 |
\item{col_NA}{Color of marker for missing value contributions.} |
| 79 |
|
| 80 |
\item{pch_NA}{Marker type for \code{NA} values.} |
| 81 |
|
| 82 |
\item{pos_NA}{Relative position of the x-location where \code{NA} values are shown: |
| 83 |
\code{min(x) + (max(x) - min(x)) * pos_NA}.} |
| 84 |
|
| 85 |
\item{plot_loess}{Should loess-smoothed curves be plotted? (Default is \code{TRUE}). |
| 86 |
The smoothing is only done for features with more than 5 distinct values.} |
| 87 |
|
| 88 |
\item{col_loess}{Color of loess curves.} |
| 89 |
|
| 90 |
\item{span_loess}{The \code{span} parameter of \code{\link[stats:loess]{stats::loess()}}.} |
| 91 |
|
| 92 |
\item{which}{Whether to do univariate or bivariate plotting. Currently, only "1d" is implemented.} |
| 93 |
|
| 94 |
\item{plot}{Should the plot be drawn? (Default is \code{TRUE}). |
| 95 |
If \code{FALSE}, only a list of matrices is returned.} |
| 96 |
|
| 97 |
\item{...}{Other parameters passed to \code{\link[graphics:plot.default]{graphics::plot()}}.} |
| 98 |
} |
| 99 |
\value{ |
| 100 |
In addition to producing plots (when \code{plot = TRUE}), it silently returns a list of two matrices: |
| 101 |
\itemize{ |
| 102 |
\item \code{data}: Feature value matrix. |
| 103 |
\item \code{shap_contrib}: Corresponding SHAP value matrix. |
| 104 |
} |
| 105 |
} |
| 106 |
\description{ |
| 107 |
Visualizes SHAP values against feature values to gain an impression of feature effects. |
| 108 |
} |
| 109 |
\details{ |
| 110 |
These scatterplots represent how SHAP feature contributions depend of feature values. |
| 111 |
The similarity to partial dependence plots is that they also give an idea for how feature values |
| 112 |
affect predictions. However, in partial dependence plots, we see marginal dependencies |
| 113 |
of model prediction on feature value, while SHAP dependence plots display the estimated |
| 114 |
contributions of a feature to the prediction for each individual case. |
| 115 |
|
| 116 |
When \code{plot_loess = TRUE}, feature values are rounded to three significant digits and |
| 117 |
weighted LOESS is computed and plotted, where the weights are the numbers of data points |
| 118 |
at each rounded value. |
| 119 |
|
| 120 |
Note: SHAP contributions are on the scale of the model margin. |
| 121 |
E.g., for a logistic binomial objective, the margin is on log-odds scale. |
| 122 |
Also, since SHAP stands for "SHapley Additive exPlanation" (model prediction = sum of SHAP |
| 123 |
contributions for all features + bias), depending on the objective used, transforming SHAP |
| 124 |
contributions for a feature from the marginal to the prediction space is not necessarily |
| 125 |
a meaningful thing to do. |
| 126 |
} |
| 127 |
\examples{ |
| 128 |
|
| 129 |
data(agaricus.train, package = "xgboost") |
| 130 |
data(agaricus.test, package = "xgboost") |
| 131 |
|
| 132 |
## Keep the number of threads to 1 for examples |
| 133 |
nthread <- 1 |
| 134 |
data.table::setDTthreads(nthread) |
| 135 |
nrounds <- 20 |
| 136 |
|
| 137 |
model_binary <- xgboost( |
| 138 |
agaricus.train$data, factor(agaricus.train$label), |
| 139 |
nrounds = nrounds, |
| 140 |
verbosity = 0L, |
| 141 |
learning_rate = 0.1, |
| 142 |
max_depth = 3L, |
| 143 |
subsample = 0.5, |
| 144 |
nthreads = nthread |
| 145 |
) |
| 146 |
|
| 147 |
xgb.plot.shap(agaricus.test$data, model = model_binary, features = "odor=none") |
| 148 |
|
| 149 |
contr <- predict(model_binary, agaricus.test$data, type = "contrib") |
| 150 |
xgb.plot.shap(agaricus.test$data, contr, model = model_binary, top_n = 12, n_col = 3) |
| 151 |
|
| 152 |
# Summary plot |
| 153 |
xgb.ggplot.shap.summary(agaricus.test$data, contr, model = model_binary, top_n = 12) |
| 154 |
|
| 155 |
# Multiclass example - plots for each class separately: |
| 156 |
x <- as.matrix(iris[, -5]) |
| 157 |
set.seed(123) |
| 158 |
is.na(x[sample(nrow(x) * 4, 30)]) <- TRUE # introduce some missing values |
| 159 |
|
| 160 |
model_multiclass <- xgboost( |
| 161 |
x, iris$Species, |
| 162 |
nrounds = nrounds, |
| 163 |
verbosity = 0, |
| 164 |
max_depth = 2, |
| 165 |
subsample = 0.5, |
| 166 |
nthreads = nthread |
| 167 |
) |
| 168 |
nclass <- 3 |
| 169 |
trees0 <- seq(from = 1, by = nclass, length.out = nrounds) |
| 170 |
col <- rgb(0, 0, 1, 0.5) |
| 171 |
|
| 172 |
xgb.plot.shap( |
| 173 |
x, |
| 174 |
model = model_multiclass, |
| 175 |
trees = trees0, |
| 176 |
target_class = 0, |
| 177 |
top_n = 4, |
| 178 |
n_col = 2, |
| 179 |
col = col, |
| 180 |
pch = 16, |
| 181 |
pch_NA = 17 |
| 182 |
) |
| 183 |
|
| 184 |
xgb.plot.shap( |
| 185 |
x, |
| 186 |
model = model_multiclass, |
| 187 |
trees = trees0 + 1, |
| 188 |
target_class = 1, |
| 189 |
top_n = 4, |
| 190 |
n_col = 2, |
| 191 |
col = col, |
| 192 |
pch = 16, |
| 193 |
pch_NA = 17 |
| 194 |
) |
| 195 |
|
| 196 |
xgb.plot.shap( |
| 197 |
x, |
| 198 |
model = model_multiclass, |
| 199 |
trees = trees0 + 2, |
| 200 |
target_class = 2, |
| 201 |
top_n = 4, |
| 202 |
n_col = 2, |
| 203 |
col = col, |
| 204 |
pch = 16, |
| 205 |
pch_NA = 17 |
| 206 |
) |
| 207 |
|
| 208 |
# Summary plot |
| 209 |
xgb.ggplot.shap.summary(x, model = model_multiclass, target_class = 0, top_n = 4) |
| 210 |
|
| 211 |
} |
| 212 |
\references{ |
| 213 |
\enumerate{ |
| 214 |
\item Scott M. Lundberg, Su-In Lee, "A Unified Approach to Interpreting Model Predictions", |
| 215 |
NIPS Proceedings 2017, \url{https://arxiv.org/abs/1705.07874} |
| 216 |
\item Scott M. Lundberg, Su-In Lee, "Consistent feature attribution for tree ensembles", |
| 217 |
\url{https://arxiv.org/abs/1706.06060} |
| 218 |
} |
| 219 |
} |