Objective: turn 1000 local explanations into a view of the whole model, with plain dplyr and ggplot2.
Script: R/02_shap_exercises.R (Part 4). The script loads shap_summary: one row per variable and per observation (obs_id, variable_name, variable_value, contribution).
1. Global importance (Part 4 a)
Compute the mean absolute SHAP value of each variable and plot it as a sorted bar chart. Same top variables as the permutation importance (capital_gain, age)?
Hint
group_by(variable_name), then summarise(mean_abs = mean(abs(contribution)))
- sort the bars:
arrange(mean_abs), then fct_inorder(variable_name); then geom_col()
2. The effect of age (Part 4 b)
Plot the SHAP value of age against the value of age, one point per observation. Compare with the PDP of age: same shape? What does the spread at a given age tell you?
Hint
filter(variable_name == "age")
variable_value is stored as text: as.numeric() it
geom_point(), with some transparency (alpha)
Bonus: a beeswarm (Part 4 c)
Complete the skeleton in the script: the four continuous variables on one plot, in three steps.
Hint
- 1:
"age", "capital_gain", "capital_loss", "hours_per_week"
- 2: rescale
value
- 3:
x = contribution, colour = value_scaled
Stuck with R, or done early? Explore other observations in the app (shiny-uros2026.pyranhia.eu, during the session only), tab SHAP summary.