Skip to Content
Course content

210: Model Interpretability with DALEX

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Imagine you've just applied for a mortgage. The bank's automated system spits out a "Denied" notice in seconds. You call the loan officer, and they tell you, "Sorry, the algorithm said no." That's an infuriating experience because the model is a black box. You don't know if it's because your credit score is five points too low, your debt-to-income ratio is slightly off, or if the model has a weird quirk regarding your employment history. You don't need the result—you already have that—you need the justification.

In R, we often build complex models (like Random Forests or XGBoost) that are great at prediction but terrible at explaining themselves. This is where DALEX comes in. It acts as the auditor who forces the loan officer to show their work. Instead of just trusting the output, we use DALEX to decompose a prediction into the actual contributions of each input feature.

Building the Auditor's Toolkit

To get started, you don't just "run" DALEX on a model. You first create an explainer object. Think of this as creating a standardized interface. DALEX doesn't care if you used randomForest, glm, or a Keras neural network; as long as you wrap it in an explainer, the interpretability tools will work the same way. I've found this "model-agnostic" approach saves a ton of time when you're iterating between different model types.

library(DALEX)
library(randomForest)

# Let's use the built-in iris dataset for a quick example, 
# predicting Species as a factor
model <- randomForest(Species ~ ., data = iris)

# Create the explainer
# We tell DALEX: what's the model, what's the data, and what's the target?
explainer_rf <- explain(
  model = model, 
  data = iris[, -5], 
  y = iris$Species, 
  label = "Random Forest"
)

Deconstructing a Single Decision

Returning to our mortgage analogy, this is where we ask, "Why was this specific person denied?" In DALEX, we use the predict_parts function to create a breakdown plot. It starts at the average prediction for the whole dataset and then adds or subtracts the influence of each variable until it reaches the final prediction for that specific observation.

# Let's look at the first observation in the dataset
individual_explanation <- predict_parts(
  explainer = explainer_rf, 
  new_observation = iris[1, -5]
)

plot(individual_explanation)

When you look at this plot, you'll see the "contribution" of each feature. If Sepal.Length pushes the prediction further toward "Setosa" and Petal.Width pulls it away, you can see exactly which variable "won" the argument. It turns a mathematical probability into a narrative.

Finding the General Trend

While breakdown plots are great for individuals, you sometimes need to know the general rule: "In general, as Petal Length increases, how does the probability of being a Virginica change?" This is where Partial Dependence Plots (PDP) come in. I like to think of PDPs as "ceteris paribus" (all other things being equal) experiments.

DALEX simulates this by taking a variable, varying its value across a range, and averaging the model's predictions while keeping all other variables constant. It's a way of isolating the effect of one feature from the noise of all the others.

# Calculate the partial dependence for Petal.Length
pdp_petal <- model_profile(explainer = explainer_rf, variables = "Petal.Length")

plot(pdp_petal)

If the resulting line is steep, that variable is a heavy hitter. If it's flat, your model is basically ignoring that feature, regardless of what the importance() function of the original model might have suggested. Be careful here, though: PDPs can be misleading if your features are highly correlated, as they might create "synthetic" data points that would never actually exist in the real world.




📋 Practical Task

Diagnosing a High-Risk Loan Prediction

You have been handed a pre-trained Random Forest model that predicts whether a loan applicant will default (1 for Default, 0 for Safe). A high-value client was just flagged as "Default," and they are demanding an explanation. Your task is to use DALEX to find the exact reason for this specific prediction.

Setup Code:

library(DALEX)
library(randomForest)

# Synthetic dataset: Credit Score, Income, Debt
set.seed(123)
loan_data <- data.frame(
  credit_score = rnorm(100, 600, 100),
  income = rnorm(100, 50000, 15000),
  debt = rnorm(100, 10000, 5000),
  default = sample(0:1, 100, replace = TRUE)
)

rf_loan_model <- randomForest(as.factor(default) ~ ., data = loan_data)

# The problematic client
client_x <- data.frame(credit_score = 450, income = 30000, debt = 25000)


Your Requirements:

  • Create a DALEX explainer object for rf_loan_model.
  • Generate a breakdown plot (using predict_parts) specifically for client_x.
  • Identify which of the three variables (credit_score, income, or debt) contributed most heavily to the "Default" prediction.
  • Create a Partial Dependence Plot for the variable you identified as the biggest driver to see if the relationship is linear or contains a threshold.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.