Skip to contents

ice() gives individual conditional expectation curves (Goldstein, Kapelner, Bleich and Pitkin, 2015): each row's prediction with feature set to each value and its other columns as they are. Their mean is partial_dependence(); curves that cross or fan out show an interaction the mean hides. center = TRUE subtracts each curve's first value (c-ICE).

Usage

ice(model, data, feature, values, center = FALSE)

ale(model, data, feature, bins = 40, levels = NULL)

shap_values(
  model,
  data,
  background,
  features = NULL,
  output = NULL,
  n_permutations = NULL,
  seed = 0,
  max_exact = 12
)

Arguments

model

A fitted model with a predict(model, newdata) method.

data

A data frame: the rows to explain.

feature

One column of data.

values

The values to set it to.

center

Whether ice() subtracts each curve's first value.

bins

For a numeric feature, the number of quantile intervals.

levels

For a factor or character feature, the order of its levels.

background

A data frame with the columns of data: a sample of the portfolio.

features

As for permutation_importance(): column names, or a list of names moved together; every column by default.

output

A function applied to each prediction, or NULL.

n_permutations

Feature orders to sample, or NULL for exact values.

seed

Seed of the orders.

max_exact

The most features the exact values enumerate.

Value

ice(): a list with feature, values, curves (a rows by values matrix) and mean. ale(): a data frame with value, effect and count (rows in each interval or at each level). shap_values(): a rows by features matrix with attributes baseline, outputs and method ("exact" or "permutation").

Details

ale() gives first-order accumulated local effects (Apley and Zhu, 2020), as ALEPlot::ALEPlot(): the effect of feature built from prediction differences across small intervals of it, each over only the rows in the interval, so correlated features are not pushed into combinations the data never shows. A numeric feature is cut at its minimum and its type 1 quantiles at seq(1/bins, 1, length.out = bins); the effects are centred to a mean of 0 over the rows. A factor's levels accumulate in the order levels gives or, by default, ALEPlot's order (classical scaling of the distances between the levels' distributions of the other columns, read from the end whose level sorts first).

shap_values() gives SHAP values (Lundberg and Lee, 2017): the Shapley values of the game v(S), the mean over the background rows of the output with the features in S taken from the row and the others from the background row (interventional, as SHAP's KernelExplainer). A row's values sum to its output less the baseline. Without n_permutations they are exact over every coalition (up to max_exact features); with it they are estimated from that many feature orders, order p from permutation(M, seed, p), the same values as Python's. output transforms each prediction before the mean: log explains a log-link model's linear predictor, where a GLM's values are exactly beta * (x - mean(x)) over the background.

Examples

d <- data.frame(x = rep(0:3, 10), z = rep(0:4, 8))
d$y <- 1 + d$x %% 3 + d$z
fit <- glm_fit(y ~ x + z, d, family = "poisson")
ice(fit, d, "x", c(0, 3))$mean
#> [1] 3.601330 3.901337
ale(fit, d, "x", bins = 3)
#>   value      effect count
#> 1     0 -0.12333760    NA
#> 2     1 -0.02599033    20
#> 3     2  0.07398833    10
#> 4     3  0.17666951    10
s <- shap_values(fit, d[1:2, c("x", "z")], d[, c("x", "z")], output = log)
attr(s, "baseline") + rowSums(s) - log(predict(fit, d[1:2, ]))
#> [1] 1.110223e-16 0.000000e+00