treeshap {treeshap}R Documentation

Calculate SHAP values of a tree ensemble model.

Description

Calculate SHAP values and optionally SHAP Interaction values.

Usage

treeshap(unified_model, x, interactions = FALSE, verbose = TRUE)

Arguments

unified_model

Unified data.frame representation of the model created with a (model).unify function. A model_unified.object object.

x

Observations to be explained. A data.frame or matrix object with the same columns as in the training set of the model. Keep in mind that objects different than data.frame or plain matrix will cause an error or unpredictable behavior.

interactions

Whether to calculate SHAP interaction values. By default is FALSE. Basic SHAP values are always calculated.

verbose

Whether to print progress bar to the console. Should be logical. Progress bar will not be displayed on Windows.

Value

A treeshap.object object (for single-output models) or treeshap_multioutput.object, which is a list of treeshap.object objects (for multi-output models). SHAP values can be accessed from treeshap.object with $shaps, and interaction values can be accessed with $interactions.

See Also

xgboost.unify for XGBoost models lightgbm.unify for LightGBM models gbm.unify for GBM models randomForest.unify for randomForest models ranger.unify for ranger models ranger_surv.unify for ranger survival models

Examples


library(xgboost)
data <- fifa20$data[colnames(fifa20$data) != 'work_rate']
target <- fifa20$target

# calculating simple SHAP values
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))
treeshap1 <- treeshap(unified_model, head(data, 3))
plot_contribution(treeshap1, obs = 1)
treeshap1$shaps

# It's possible to calcualte explanation over different part of the data set

unified_model_rec <- set_reference_dataset(unified_model, data[1:1000, ])
treeshap_rec <- treeshap(unified_model, head(data, 3))
plot_contribution(treeshap_rec, obs = 1)

# calculating SHAP interaction values
param2 <- list(objective = "reg:squarederror", max_depth = 7)
xgb_model2 <- xgboost::xgboost(as.matrix(data), params = param2, label = target, nrounds = 10)
unified_model2 <- xgboost.unify(xgb_model2, as.matrix(data))
treeshap2 <- treeshap(unified_model2, head(data, 3), interactions = TRUE)
treeshap2$interactions


[Package treeshap version 0.3.1 Index]