knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

This example demonstrates how to use the breakDown package for models created with the kernlab package.

library("breakDown")
library(kernlab)

wine_svm_model <- ksvm(quality~., data = wine)
wine_svm_model

# or with the e1071:::svm

library(e1071)
wine_svm_model <- svm(quality~., data = wine)
wine_svm_model

Now we are ready to call the broken() function. Since kernlab is useing S4 methods we need to pass here the hook to kernlab:::predict method.

library("breakDown")
nobs <- wine[5, , drop = FALSE]
base_prediction <- predict(wine_svm_model, nobs)
set.seed(1313)

explain_5_up <- broken(wine_svm_model, new_observation = nobs, 
                    data = wine, predict.function = predict,
                    baseline = "intercept", direction = "up")
explain_5_up

explain_5_down <- broken(wine_svm_model, new_observation = nobs, 
                    data = wine, predict.function = predict,
                    baseline = "intercept", direction = "down")
explain_5_down

And plot it.

library(ggplot2)
plot(explain_5_up) + ggtitle(paste0("Prediction for SVM model ", round(base_prediction, 3)))
plot(explain_5_down) +  ggtitle(paste0("Prediction for SVM model ", round(base_prediction, 3)))


pbiecek/breakDown documentation built on March 15, 2024, 10:46 a.m.