Search code examples
rshap

Generate SHAP dependence plots


Is there a package that allows for estimation of shap values for multiple observations for models that are not XGBoost or decision tree based? I created a neural network using Caret and NNET. I want to develop a beeswarm plot and shap dependence plots to explore the relationship between certain variables in my model and the outcome. The only success I have had is using the DALEX package to estimate SHAP values, but DALEX only does this for single instances and cannot do a global analysis using SHAP values. Any insight or help would be appreciated!

I have tried using different shap packages (fastshap, shapr) but these require decision tree based models. I tried creating an XGBoost model in caret but this did not implement well with the shap packages in r and I could not get the outcome I wanted.


Solution

  • I invested a little bit of time to push R in this regard:

    • shapviz plots SHAP values from any source, including XGBoost, LightGBM, H2O, kernelshap, and fastshap
    • kernelshap calculates Kernel SHAP values for all models with numeric output, even multivariate output. This will be your friend when it comes to models outside the TreeSHAP confort zone...

    Put differently: kernelshap + shapviz = explain any model.

    Here an example using "caret" for linear regression, but nnet works identically.

    library(caret)
    library(kernelshap)
    library(shapviz)
    
    fit <- train(
      Sepal.Length ~ ., 
      data = iris, 
      method = "lm", 
      tuneGrid = data.frame(intercept = TRUE),
      trControl = trainControl(method = "none")
    )
    
    # Explain rows in `X` based on background data `bg_X` (50-200 rows, not the full training data!)
    shap <- kernelshap(fit, X = iris[, -1], bg_X = iris)
    sv <- shapviz(shap)
    
    sv_importance(sv)
    sv_importance(sv, kind = "bee")
    sv_dependence(sv, "Species", color_var = "auto")
    
    # Single observations
    sv_waterfall(sv, 1)
    sv_force(sv, 1)
    

    SHAP importance barplot SHAP summary beeswarm plot SHAP dependence SHAP waterfall