This function sums up SHAP values (or SHAP interaction values) of feature groups. Typical application: SHAP values have been generated by a model with one or multiple one-hot encoded variables, but the explanations should be done using the original factor.

collapse_shap(S, collapse = NULL, ...)

Arguments

S

Either a (n x p) matrix of SHAP values or a (n x p x p) array of SHAP interaction values.

collapse

A named list of character vectors. Each vector specifies the feature names whose SHAP values need to be summed up. The names determine the resulting collapsed column/dimension names.

...

Currently unused.

Value

A matrix of SHAP values, or an array of SHAP interaction values.

Examples

S <- cbind(
  x = c(0.1, 0.1, 0.1),
  `age low` = c(0.2, -0.1, 0.1),
  `age mid` = c(0, 0.2, -0.2),
  `age high` = c(1, -1, 0)
)
collapse <- list(age = c("age low", "age mid", "age high"))
collapse_shap(S, collapse)
#>        x  age
#> [1,] 0.1  1.2
#> [2,] 0.1 -0.9
#> [3,] 0.1 -0.1

# Arrays (as with SHAP interactions)
S_inter <- array(1, dim = c(2, 4, 4), dimnames = list(NULL, letters[1:4], letters[1:4]))
collapse_shap(S_inter, collapse = list(cd = c("c", "d"), ab = c("a", "b")))
#> , , cd
#> 
#>      cd ab
#> [1,]  4  4
#> [2,]  4  4
#> 
#> , , ab
#> 
#>      cd ab
#> [1,]  4  4
#> [2,]  4  4
#>