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 = ""
)

Arguments

treeshap

A treeshap object produced with the treeshap function. treeshap.object.

obs

A numeric indicating which observation should be plotted. Be default it's first observation.

max_vars

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.

min_max

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.

digits

number of decimal places (round) to be used.

explain_deviation

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.

title

the plot's title, by default 'SHAP Break-Down'.

subtitle

the plot's subtitle. By default no subtitle.

Value

a ggplot2 object

See also

Examples

# \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))

# }