Preface

Classifiers are not supported by default. You need to create explainer for each class you want to explain. Result of this example is available at link.

Load data & libraries

library(arenar)
library(DALEX)
library(dplyr)
library(MASS)
library(gbm)

#data set
HR <- DALEX::HR

# Get random 10 samples to explain it
observations <- HR[sample(1:nrow(HR), size=10), ]
# Name observations
rownames(observations) <- paste0(
  toupper(substr(observations$gender, 0, 1)),
  substr(observations$gender, 2, 100),
  " ",
  round(observations$age),
  "yr",
  " Grade: ",
  observations$evaluation
)

Models

model_gbm <- gbm(status ~ ., data=HR, n.trees=100, interaction.depth = 3)
model_lda <- lda(status ~ ., data=HR)

Create Explainers & Arena

# Create new arena and add prepared observations
arena <- create_arena() %>% push_observations(observations)

# Levels of target variable
levels(HR$status)
# [1] "fired"    "ok"       "promoted"

# For each target level create explainers
for (status in levels(HR$status)) {
  # Explainer for gbm
  explainer_gbm <- explain(
    model_gbm,
    # Target variable as 0,1 for each level
    y = as.numeric(HR$status == status),
    data = HR[, -6], # Remove target variable
    label = paste0("GBM [", status, "]"),
    # In predict function we need to extract class probability
    predict_function = function(m, x) predict(m, x, n.trees=100, type="response")[,status,]
  )
  # Explainer for lda
  explainer_lda <- explain(
    model_lda,
    # Target variable as 0,1 for each level
    y = as.numeric(HR$status == status),
    data = HR[, -6], # Remove target variable
    label = paste0("LDA [", status, "]"),
    # In predict function we need to extract class probability
    predict_function = function(m, x) predict(m, x)$posterior[,status]
  )
  # Add explainers
  arena <- push_model(arena, explainer_gbm)
  arena <- push_model(arena, explainer_lda)

  # Upload arena
  upload_arena(arena)
}