Convert your randomForest model into a standardized representation. The returned representation is easy to be interpreted by the user and ready to be used as an argument in treeshap() function.

randomForest.unify(rf_model, data)

Arguments

rf_model

An object of randomForest class. At the moment, models built on data with categorical features are not supported - please encode them before training.

data

Reference dataset. A data.frame or matrix with the same columns as in the training set of the model. Usually dataset used to train model.

Value

a unified model representation - a model_unified.object object

Details

Binary classification models with a target variable that is a factor with two levels, 0 and 1, are supported

Examples

if (requireNamespace("randomForest", quietly = TRUE)) {
  library(randomForest)
  data_fifa <- fifa20$data[
    !colnames(fifa20$data) %in%
      c(
        'work_rate',
        'value_eur',
        'gk_diving',
        'gk_handling',
        'gk_kicking',
        'gk_reflexes',
        'gk_speed',
        'gk_positioning'
      )
  ]
  data <- na.omit(cbind(data_fifa, target = fifa20$target))

  rf <- randomForest::randomForest(
    target ~ .,
    data = data,
    maxnodes = 10,
    ntree = 10
  )
  unified_model <- randomForest.unify(rf, data)
  shaps <- treeshap(unified_model, data[1:2, ])
   # plot_contribution(shaps, obs = 1)
}
#> randomForest 4.7-1.2
#> Type rfNews() to see new features/changes/bug fixes.
#> 
|0%----|------|20%---|------|40%---|------|60%---|------|80%---|------|100%
#> =---------------------------------------------------------------------- (0%)

====================================----------------------------------- (50%)

======================================================================= (100%)