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

View source: R/explain.R

shap_caseR Documentation

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

Description

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.

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

Value

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

Author(s)

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

rtemis documentation built on Oct. 20, 2026, 1:06 a.m.