Skip to contents

Convert a long-format SHAP table with observation IDs, feature names, and SHAP values to a wide format with one row per observation and one column per feature. Baseline rows can be optionally dropped based on common baseline labels.

Usage

.dalex_shap_to_wide(
  shap,
  id_col = NULL,
  variable_col = NULL,
  value_col = NULL,
  drop_baseline = TRUE,
  baseline_labels = c("_baseline_", "baseline", "intercept")
)

Arguments

shap

A long-format SHAP table containing observation IDs, feature names, and SHAP values.

id_col

Optional column name identifying observations. If NULL, common ID column names will be searched for.

variable_col

Optional column name containing feature names. If NULL, common variable column names will be searched for.

value_col

Optional column name containing SHAP values. If NULL, common value column names will be searched for.

drop_baseline

Logical scalar. If TRUE, rows with feature names matching common baseline labels will be dropped before reshaping.

baseline_labels

Character vector of feature names to treat as baseline rows when drop_baseline = TRUE. Matching is case-sensitive.

Value

A wide-format data.table with one row per observation and one numeric column per SHAP feature.

Details

The function attempts to identify the ID, variable, and value columns based on common naming conventions. If drop_baseline = TRUE, rows with feature names in baseline_labels are removed before reshaping. The resulting wide table contains only numeric SHAP feature columns. Aliases for the identified columns are as follows:

  • ID column candidates: "_id_", "id", "row_id", "observation_id", "observation", "case_id"

  • Variable column candidates: "_vname_", "variable", "variable_name", "feature"

  • Value column candidates: "_attribution_", "contribution", "phi", "shap", "value"