rcaretshap

SHAP (Shapley Additive Explanations) with caret in R


I have used the caret R package to train a neural network, and a random forest. Can I find the SHAP values for feature importance in any way?


Solution

  • Slightly modified from kernelshap's README: https://github.com/ModelOriented/kernelshap

    library(caret)
    library(kernelshap)
    library(shapviz)
    
    fit <- train(
      Sepal.Length ~ . + Species * Sepal.Width, 
      data = iris, 
      method = "lm", 
      tuneGrid = data.frame(intercept = TRUE),
      trControl = trainControl(method = "none")
    )
    
    xvars <- colnames(iris[-1])
    s <- kernelshap(fit, iris, predict, bg_X = iris, feature_names = xvars)
    sv <- shapviz(s)
    sv_importance(sv)
    sv_dependence(sv, xvars)
    

    enter image description here enter image description here

    Remarks

    1. Replace the linear model by anything else
    2. If the dataset is larger than 500 obs, replace bg_X by a subsample of about 200--500 rows.
    3. Probabilistic classification will work similarly