library(xgboost) library(ggplot2) library(patchwork) library(shapviz) library(kernelshap) options(shapviz.viridis_args = list(option = "D", begin = 0.1, end = 0.9)) set.seed(1) # https://github.com/stedy/Machine-Learning-with-R-datasets df <- read.csv("https://raw.githubusercontent.com/stedy/Machine-Learning-with-R-datasets/refs/heads/master/insurance.csv") # Gamma GLM with interactions fit_glm <- glm(charges ~ . * smoker, data = df, family = Gamma(link = "log")) # Use SHAP to explain xvars <- c("age", "sex", "bmi", "children", "smoker", "region") X_explain <- head(df[xvars], 500) # The new sampling permutation algo (forced with exact = FALSE) shap_glm <- permshap(fit_glm, X_explain, exact = FALSE, seed = 1) |> shapviz() sv_importance(shap_glm, kind = "bee") sv_dependence( shap_glm, v = xvars, share_y = TRUE, color_var = "smoker" )
