Learn R Programming

rtemis (version 1.4.3)

shap_case: One case's contributions, ordered by how much they moved the prediction

Description

The three parts of a per-case explanation: the baseline, each feature's contribution, and the prediction.

Usage

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

Value

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

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.

Author

EDG

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 values.

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".

Examples

Run this code
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)
contributions <- explain(
  mod,
  x[, c("age", "bmi")],
  background = x[, c("age", "bmi")],
  verbosity = 0L
)
shap_case(contributions, x[, c("age", "bmi")], case = 1L)

Run the code above in your browser using DataLab