Skip to contents

The three things a per-case explanation is: where the prediction would have started, what each feature did to it, and where it ended.

Usage

shap_case(x, newdata, case = 1L, class = NULL)

Arguments

x

SHAP object.

newdata

tabular data: The cases x explains.

case

Integer or Character: Which case, by position or row name.

class

Optional Character: Which class's contributions. NULL takes the first, which is the only one for a regression or binary outcome.

Value

List with steps (a data.table of feature, value and contribution), baseline, predicted, case and class.

Details

Shaped here, following session_timeline(): the table is the shared input for rtemis.draw's chart and for rtemislive, so both show the same thing, and it is useful on its own without either.

Features are ordered by the magnitude of their contribution, which is the order a waterfall reads in, and each carries the value it took for this case so the row reads "age = 62, +0.4".

Author

EDG

Examples

x <- data.frame(age = rnorm(100), bmi = rnorm(100))
x[["y"]] <- x[["age"]] * 2 + rnorm(100, sd = 0.3)
mod <- train(x, hyperparameters = setup_GLM(), verbosity = 0L)
#> Error in UseMethod("train"): no applicable method for 'train' applied to an object of class "data.frame"
contributions <- explain(
  mod,
  x[, c("age", "bmi")],
  background = x[, c("age", "bmi")],
  verbosity = 0L
)
#> Error: object 'mod' not found
shap_case(contributions, x[, c("age", "bmi")], case = 1L)
#> Error: object 'contributions' not found