This function plots contributions of features into the prediction for a single observation.
plot_contribution(
treeshap,
obs = 1,
max_vars = 5,
min_max = NA,
digits = 3,
explain_deviation = FALSE,
title = "SHAP Break-Down",
subtitle = ""
)
A treeshap object produced with the treeshap
function. treeshap.object
.
A numeric indicating which observation should be plotted. Be default it's first observation.
maximum number of variables that shall be presented. Variables with the highest importance will be presented.
Remaining variables will be summed into one additional contribution. By default 5
.
a range of OX axis. By default NA
, therefore it will be extracted from the contributions of x
.
But it can be set to some constants, useful if these plots are to be used for comparisons.
number of decimal places (round
) to be used.
if TRUE
then instead of explaining prediction and plotting intercept bar, only deviation from mean prediction of the reference dataset will be explained. By default FALSE
.
the plot's title, by default 'SHAP Break-Down'
.
the plot's subtitle. By default no subtitle.
a ggplot2
object
treeshap
for calculation of SHAP values
plot_feature_importance
, plot_feature_dependence
, plot_interaction
# \donttest{
library(xgboost)
#>
#> Attaching package: ‘xgboost’
#> The following object is masked from ‘package:lightgbm’:
#>
#> slice
data <- fifa20$data[colnames(fifa20$data) != 'work_rate']
target <- fifa20$target
param <- list(objective = "reg:squarederror", max_depth = 3)
xgb_model <- xgboost::xgboost(as.matrix(data), params = param, label = target,
nrounds = 20, verbose = FALSE)
unified_model <- xgboost.unify(xgb_model, as.matrix(data))
x <- head(data, 1)
shap <- treeshap(unified_model, x)
#>
|0%----|------|20%---|------|40%---|------|60%---|------|80%---|------|100%
#> =---------------------------------------------------------------------- (0%)
======================================================================= (100%)
plot_contribution(shap, 1, min_max = c(0, 120000000))
# }