Model-agnostic explanations: permutation importance, interactions and partial dependence
explanations.RdThree tools that need only predict(), so they explain any model (CAS
Monograph 16, chapter 9). permutation_importance() measures how much a
score worsens when a feature's column is shuffled, which breaks its link
to the response and leaves every other column in place; several columns
named together are shuffled with one permutation (a group).
interaction_screen() shuffles two features separately and together: with
L_A, L_B and L_AB the losses, L_A + L_B - L_AB is about zero if
neither interacts with anything and positive if they interact with each
other or with a common third feature. A deviance is not additive on the
link scale, so the pairs of an additive log-link model need not be zero:
compare pairs with one another, on a flexible model such as a boosting
model. partial_dependence() sets a feature to each value on every row
and averages the predictions; for a log-link model without interactions
their ratios are exactly the feature's relativities.
Usage
permutation_importance(
model,
data,
score,
features,
splits = NULL,
n_repeats = 1,
seed = 0
)
interaction_screen(
model,
data,
score,
features,
pairs = NULL,
splits = NULL,
n_repeats = 1,
seed = 0
)
partial_dependence(model, data, feature, values, splits = NULL)Arguments
- model
A fitted model; with
splits, afit(train)function, as forcross_validate(), fitted on each split's training rows and explained on its test rows.- data
A data frame: the rows to explain (all the data with
splits).- score
score(model, test): a loss (lower is better), such asdeviance_score(); a gain such as a pseudo-R2 goes in as its negative.- features
Column names, or a list whose elements are names shuffled together (labelled by the list's names, or the names joined by
+). Never the response, exposure or weights.- splits
Optional splits from
k_fold(),group_k_fold()ortime_ordered(); the results average over them.- n_repeats
Shuffles per split, averaged.
- seed
Seed of the shuffles.
- pairs
For
interaction_screen(), a list of two-label vectors; every pair offeaturesby default.- feature
For
partial_dependence(), one column ofdata.- values
The values to set it to.
Value
permutation_importance(): a data frame with feature,
importance (the mean increase in the score over the splits),
std_error (over the splits, NA for one) and relative (percent of
the largest), with attributes by_split (a splits by features matrix)
and base (the unshuffled score per split). interaction_screen(): a
data frame with a, b, strength, std_error and the mean losses
loss_a, loss_b, loss_ab, with attribute by_split.
partial_dependence(): a data frame with value and mean (the mean
prediction), with attribute by_split.
Details
Shuffles come from permutation(): repeat r of split i (both from 0)
uses permutation(n, seed, i * n_repeats + r), so Python's
prospicio.models functions of the same names give the same values on the
same rows.
Examples
d <- data.frame(x = 1:40 / 10, noise = ((7 * 1:40) %% 11) / 11)
d$y <- 1 + 2 * d$x + sin(1:40)
fit <- glm_fit(y ~ x, d, family = "gaussian")
permutation_importance(fit, d, deviance_score("gaussian", "y"), c("x", "noise"))
#> feature importance std_error relative
#> 1 x 11.82542 NA 100
#> 2 noise 0.00000 NA 0
partial_dependence(fit, d, "x", c(1, 2))
#> value mean
#> 1 1 3.045060
#> 2 2 5.047341