Skip to content

24-7 Today

Menu
  • Home
  • Ads Guide
  • Blogging
  • Sec Tips
  • SEO Strategies
Menu

Key improvements in shapviz and kernelshap

Posted on September 20, 2025 by 24-7
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"
)

Related

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

©2025 24-7 Today | Design: WordPress | Design: Facts