SHAP Summary Plot Visualisation for Random Forest (Ranger)

Hi all,

I've been using the 'Ranger' random forest package alongside packages such as 'treeshap' to get Shapley values.

Yet, one thing I've noticed is that I am unable obtain the SHAP summary plot, typically known as the 'beeswarm' plot by using this package (or any random forest Shapley packages I could find online).

I wondered therefore if there was an equivalent package for random forest modelling to something like 'SHAPforXGboost' in which the beeswarm/summary plot was obtainable?

Any equivalent code equivalent to XGboost that would be suitable for Ranger would be greatly appreciated.

SHAP Visualization in R (first post) | Welcome to my blog

library(SHAPforxgboost)
library(xgboost)
library(data.table)
library(ggplot2)
})

X1 = as.matrix(iris[,-5])
mod1 = xgboost::xgboost(
  data = X1, label = iris$Species, gamma = 0, eta = 1, 
  lambda = 0,nrounds = 1, verbose = F)

# shap.values(model, X_dataset) returns the SHAP
# data matrix and ranked features by mean|SHAP|
shap_values <- shap.values(xgb_model = mod1, X_train = X1)
shap_values$mean_shap_score
shap_values_iris <- shap_values$shap_score

# shap.prep() returns the long-format SHAP data from either model or
shap_long_iris <- shap.prep(xgb_model = mod1, X_train = X1)
# is the same as: using given shap_contrib
shap_long_iris <- shap.prep(shap_contrib = shap_values_iris, X_train = X1)

shap.plot.summary(shap_long_iris)

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.