library(tidymodels)
library(shapviz)
library(DALEXtra)
theme_set(theme_minimal(14))
set.seed(123)
n <- 10000
data <- tibble(type = sample(1:3, n, replace = TRUE)) |>
bind_cols(as_tibble(setNames(replicate(10, runif(n), simplify = FALSE), paste0("x", 1:10)))) |>
mutate(
target = (type == 2) * 4 * x1 * (1 - x1) +
(type == 3) * (-1 + 2 * x1) +
0.01 * x2 + 0.1 * x3 + 0.01 * x4 + 0.001 * x5
)PDP and ICE plots: how does the model use a feature?
![]()
In April 2025 I did a live session on Curso-R’s YouTube channel about partial dependence plots (PDP) and individual conditional expectation (ICE) plots. It’s in Portuguese, and it follows an earlier session about SHAP values. This post is the written version, with the code from the session cleaned up and rerun.
Why look past SHAP
SHAP is what I use day to day to see which features matter. But at work the next question is often about direction: does the model move the way we expect when a feature changes? In credit, for example, more simulated loan requests should mean a higher predicted default risk. A descriptive table may say so, while the model, perhaps overfitted, does something else. Checking that is a consistency test, and sometimes it teaches you something about the data.
SHAP dependence plots can answer that, but not always. In the previous session I showed a case where the plot was a shapeless cloud. PDP and ICE plots are another way to look at the same question, and they are simple enough to build by hand.
A simulated example
A simulation is the best place to start, because we know the true answer. x1 is the most important feature, and its effect on the target depends on a categorical feature, type:
- type 1:
x1has no effect (a flat line); - type 2: a parabola, \(4 x_1 (1 - x_1)\);
- type 3: a straight line, \(-1 + 2 x_1\).
x2 to x5 have small linear effects, and x6 to x10 are noise.
An xgboost model fits it almost perfectly:
set.seed(123)
split <- initial_split(data, prop = 0.3)
train <- training(split)
test <- testing(split)
fit_xgb <- workflow(
recipe(target ~ ., data = train),
boost_tree(mode = "regression", trees = 900, tree_depth = 8, learn_rate = 0.01) |>
set_engine("xgboost")
) |>
fit(train)
predict(fit_xgb, test) |>
bind_cols(test) |>
rmse(target, .pred)# A tibble: 1 × 3
.metric .estimator .estimate
<chr> <chr> <dbl>
1 rmse standard 0.00844
What SHAP says
SHAP gets the importance right: type and x1 first, then x3, then the rest.
set.seed(1)
X <- test |> slice_sample(n = 2000) |> select(-target)
shap <- shapviz(extract_fit_engine(fit_xgb), X_pred = as.matrix(X), X = X)
sv_importance(shap, max_display = 6)
The dependence plot for x1, coloured by type, is less faithful:
sv_dependence(shap, "x1", color_var = "type")
Here it isn’t a cloud, but it gives x1 a rising effect for type 1, where x1 does nothing. SHAP splits the effect of the interaction between x1 and type, so part of what belongs to type 2 and 3 shows up in the type 1 rows. If you read the plot as “how x1 moves the prediction for type 1”, you get the wrong answer.
Ceteris paribus, by hand
The idea behind PDP and ICE fits in a few lines. Take one row of the data, keep every feature fixed except x1, replace x1 with a grid of values, and ask the model for a prediction at each one. Economists call this ceteris paribus: all else equal.
one_row <- data |> slice(1) |> select(type, x1, x2, x3, target)
one_row# A tibble: 1 × 5
type x1 x2 x3 target
<int> <dbl> <dbl> <dbl> <dbl>
1 3 0.732 0.342 0.552 0.528
profile <- data |>
slice(1) |>
select(-x1) |>
cross_join(tibble(x1 = seq(0, 1, by = 0.1)))
profile$pred <- predict(fit_xgb, profile)$.pred
profile |> select(type, x1, pred)# A tibble: 11 × 3
type x1 pred
<int> <dbl> <dbl>
1 3 0 -0.924
2 3 0.1 -0.732
3 3 0.2 -0.529
4 3 0.3 -0.344
5 3 0.4 -0.138
6 3 0.5 0.0679
7 3 0.6 0.251
8 3 0.7 0.461
9 3 0.8 0.671
10 3 0.9 0.856
11 3 1 1.05
That row is type 3, and its predictions trace the straight line, from about -1 to 1. One row becomes 11 rows: the model’s answer to “what if x1 were this value?”.
ICE and PDP
Do the same for 40 rows and draw one line per row: that’s an ICE plot. ICE stands for individual conditional expectation (Goldstein et al., 2015). The PDP is the average of those lines.
set.seed(42)
ice <- data |>
slice_sample(n = 40) |>
mutate(.row = row_number(), group = sample(c("A", "B", "C"), n(), replace = TRUE)) |>
select(-x1) |>
cross_join(tibble(x1 = seq(0, 1, by = 0.05)))
ice$pred <- predict(fit_xgb, ice)$.pred
pdp <- ice |> summarise(pred = mean(pred), .by = x1)
ggplot(ice, aes(x1, pred)) +
geom_line(aes(group = .row), alpha = 0.3) +
geom_line(data = pdp, colour = "firebrick", linewidth = 1.5) +
labs(y = "prediction", subtitle = "ICE curves (grey) and their average, the PDP (red)")
The PDP alone, the red line, is misleading here: an average of a flat line, a parabola and a straight line, which matches none of them. The ICE curves show that there are three different shapes.
Finding the interaction
That is what I like most about ICE plots. Trees find interactions, but they don’t tell you which ones. When the ICE curves fall into groups, some feature probably changes how x1 works. To find it, colour the curves by candidate features.
Below, group is a random label I made up, and type is the real one:
library(patchwork)
ice_by <- function(var) {
ggplot(ice, aes(x1, pred, group = .row, colour = factor(.data[[var]]))) +
geom_line(alpha = 0.6) +
labs(y = "prediction", colour = var)
}
ice_by("group") + ice_by("type")
Every colour of group appears in all three shapes, so it explains nothing. type separates them perfectly. It’s a manual search, but it describes what the model learned: if it’s type 1, x1 doesn’t matter; type 2, a parabola; type 3, a straight line.
With DALEX
You don’t need to do it by hand. DALEX builds these plots for you, and DALEXtra has explain_tidymodels() for tidymodels workflows. model_profile() computes the profiles, and groups splits the PDP by a feature:
explainer <- explain_tidymodels(
fit_xgb,
data = select(data, -target),
y = data$target,
label = "type",
verbose = FALSE
)
set.seed(1)
pdp_by_type <- model_profile(explainer, variables = "x1", N = 500, groups = "type")
plot(pdp_by_type)
type_1, type_2 and type_3 are the flat line, the parabola and the straight line we simulated.
Real data: iris
Real data is rarely this neat. With a random forest on iris, each curve is the predicted probability of one species as a measurement changes:
set.seed(123)
iris_split <- initial_split(iris, prop = 0.8, strata = Species)
fit_rf <- workflow(Species ~ ., rand_forest(trees = 100, mode = "classification") |> set_engine("ranger")) |>
fit(training(iris_split))
explainer_rf <- explain_tidymodels(
fit_rf,
data = select(iris, -Species),
y = iris$Species,
label = "rf",
verbose = FALSE
)
plot(model_profile(explainer_rf))
Petal length and width move the predictions a lot: short petals mean setosa, medium ones versicolor, long ones virginica. The sepal measurements barely change anything.
How I use it
I see PDP and ICE plots as a complement, not a replacement. First I check that the model isn’t overfitted, that it’s stable over time, and that the SHAP importance makes sense. Then I use PDP and ICE to go further: is each feature moving in a sensible direction? Are there interactions I didn’t know about? Do they make sense, or do they point to a bias in how the data was collected?
They also give you a careful way to explain a model to someone else. You talk about the model, not about the world. For example: “in São Paulo, our score gets worse linearly with the credit card limit; in another state it follows a curve; in a third, the limit doesn’t change the score”. That’s a description of what the model does, with no causal claim, which is the kind of claim I avoid. It’s a good basis for a conversation with a colleague or a slide in a presentation, and it can show you a hole in the model.
To learn more, the Explanatory Model Analysis book by the DALEX authors covers ceteris paribus profiles, partial dependence and other methods.