Learn R Programming

rtemis (version 1.4.3)

shap_by_level: Contributions of a categorical feature, by the level each case has

Description

What a per-feature contribution hides once it is averaged across cases: that one level of a diagnosis drives the prediction and the others do nothing.

Usage

shap_by_level(x, newdata, features = NULL)

Value

data.table with columns class, feature, level, n, mean, sd, and mean_abs.

Arguments

x

SHAP object.

newdata

tabular data: The cases x explains.

features

Optional Character: Features to summarize. NULL takes every categorical feature in newdata.

Author

EDG

Details

A case has exactly one level, so its per-feature contribution is the contribution of the level it has. Grouping cases by level reports how each level contributes.

Splitting a contribution across the columns of an encoding answers a different question. Those values include a term for every level the case does not have; they depend on the encoding, since a contrast-coded factor's reference level has no column; and for a linear model they are identical for every case at the same level. Grouping by level does not depend on the encoding and includes the reference level.

newdata must be the data the explanation was computed on, which is checked against its fingerprint, so contributions are never paired with the values of other rows.

Examples

Run this code
x <- data.frame(
  age = rnorm(100),
  dx = factor(sample(c("none", "diabetes"), 100, TRUE))
)
x[["y"]] <- x[["age"]] + 3 * (x[["dx"]] == "diabetes") + rnorm(100, sd = 0.3)
mod <- train(x, hyperparameters = setup_GLM(), verbosity = 0L)
contributions <- explain(
  mod,
  x[, c("age", "dx")],
  background = x[, c("age", "dx")],
  verbosity = 0L
)
shap_by_level(contributions, x[, c("age", "dx")])

Run the code above in your browser using DataLab