Learn R Programming

fastshap (version 0.0.7)

autoplot.explain: Plotting Shapley values

Description

Construct Shapley-based importance plots or Shap-based dependence plots.

Usage

# S3 method for explain
autoplot(
  object,
  type = c("importance", "dependence", "contribution"),
  feature = NULL,
  num_features = NULL,
  X = NULL,
  feature_values = NULL,
  color_by = NULL,
  smooth = FALSE,
  smooth_color = "red",
  smooth_linetype = "solid",
  smooth_size = 1,
  smooth_alpha = 1,
  row_num = NULL,
  ...
)

Value

A "ggplot" object; see ggplot2-package for details.

Arguments

object

An object of class "explain".

type

Character string specifying which type of plot to construct. Current options are "importance" (for Shapley-based variable importance plots), "dependence" (for Shapley-based dependence plots), and "contribution" (for visualizing the feature contributions to an individual prediction).

feature

Character string specifying which feature to use when type = "dependence". If NULL (default) the first feature will be used to construct the plot.

num_features

Integer specifying the number of variables to plot. Default is NULL which will cause all variables to be displayed.

X

A matrix-like R object (e.g., a data frame or matrix) containing ONLY the feature columns from the training data.

feature_values

A matrix-like R object (e.g., a data frame or matrix) containing the feature values correposnding to the instance being explained. Only used when type = "dependence". NOTE: Must contain the same column structure (e.g., column names, order, etc.) as X.

color_by

Character string specifying an optional feature column in X to use for coloring whenever type = "dependence".

smooth

Logical indicating whether or not to add a smoother to the scatterplot whenever type = "dependence". Default is TRUE.

smooth_color

The color to use for the smoother whenever smooth = TRUE. The default is "black"; see geom_smooth for details.

smooth_linetype

The type of line to use for the smoother whenever smooth = TRUE. The default is "solid"; see geom_smooth for details.

smooth_size

The size to use for the smoother whenever smooth = TRUE. The default is 1; see geom_smooth for details.

smooth_alpha

The transparency to use for the smoother whenever smooth = TRUE. The default is 1; see geom_smooth for details.

row_num

Integer specifying a single row/instance in object to plot the explanation when type = "contribution". If NULL (the default) the explanation for the first row/instance will be used.

...

Additional optional arguments to be passed on to geom_col (if type = "importance") or geom_point (if type = "dependence").

Examples

Run this code
#
# A projection pursuit regression (PPR) example
#

# Load the sample data; see ?datasets::mtcars for details
data(mtcars)

# Fit a projection pursuit regression model
mtcars.ppr <- ppr(mpg ~ ., data = mtcars, nterms = 1)

# Compute approximate Shapley values using 10 Monte Carlo simulations
set.seed(101)  # for reproducibility
shap <- explain(mtcars.ppr, X = subset(mtcars, select = -mpg), nsim = 10, 
                pred_wrapper = predict)
shap

# Shapley-based plots
library(ggplot2)
autoplot(shap)  # Shapley-based importance plot
autoplot(shap, type = "dependence", feature = "wt", X = mtcars)
autoplot(shap, type = "contribution", row_num = 1)  # explain first row of X

Run the code above in your browser using DataLab