autoplot.partial: Plotting Partial Dependence Functions

View source: R/autoplot.R

autoplot.partialR Documentation

Plotting Partial Dependence Functions

Description

Plots partial dependence functions (i.e., marginal effects) using ggplot2 graphics.

Usage

## S3 method for class 'partial'
autoplot(
  object,
  center = FALSE,
  plot.pdp = TRUE,
  pdp.color = "red",
  pdp.size = 1,
  pdp.linetype = 1,
  rug = FALSE,
  smooth = FALSE,
  smooth.method = "auto",
  smooth.formula = y ~ x,
  smooth.span = 0.75,
  smooth.method.args = list(),
  contour = FALSE,
  contour.color = "white",
  train = NULL,
  xlab = NULL,
  ylab = NULL,
  main = NULL,
  legend.title = "yhat",
  ...
)

## S3 method for class 'ice'
autoplot(
  object,
  center = FALSE,
  plot.pdp = TRUE,
  pdp.color = "red",
  pdp.size = 1,
  pdp.linetype = 1,
  rug = FALSE,
  train = NULL,
  xlab = NULL,
  ylab = NULL,
  main = NULL,
  ...
)

## S3 method for class 'cice'
autoplot(
  object,
  plot.pdp = TRUE,
  pdp.color = "red",
  pdp.size = 1,
  pdp.linetype = 1,
  rug = FALSE,
  train = NULL,
  xlab = NULL,
  ylab = NULL,
  main = NULL,
  ...
)

Arguments

object

An object that inherits from the "partial" class.

center

Logical indicating whether or not to produce centered ICE curves (c-ICE curves). Only useful when object represents a set of ICE curves; see partial for details. Default is FALSE.

plot.pdp

Logical indicating whether or not to plot the partial dependence function on top of the ICE curves. Default is TRUE.

pdp.color

Character string specifying the color to use for the partial dependence function when plot.pdp = TRUE. Default is "red".

pdp.size

Positive number specifying the line width to use for the partial dependence function when plot.pdp = TRUE. Default is 1.

pdp.linetype

Positive number specifying the line type to use for the partial dependence function when plot.pdp = TRUE. Default is 1.

rug

Logical indicating whether or not to include rug marks on the predictor axes. Default is FALSE.

smooth

Logical indicating whether or not to overlay a LOESS smooth. Default is FALSE.

smooth.method

Character string specifying the smoothing method (function) to use (e.g., "auto", "lm", "glm", "gam", "loess", or "rlm"). Default is "auto". See geom_smooth for details.

smooth.formula

Formula to use in smoothing function (e.g., y ~ x, y ~ poly(x, 2), or y ~ log(x)).

smooth.span

Controls the amount of smoothing for the default loess smoother. Smaller numbers produce wigglier lines, larger numbers produce smoother lines. Default is 0.75.

smooth.method.args

List containing additional arguments to be passed on to the modeling function defined by smooth.method.

contour

Logical indicating whether or not to add contour lines to the level plot.

contour.color

Character string specifying the color to use for the contour lines when contour = TRUE. Default is "white".

train

Data frame containing the original training data. Only required if rug = TRUE or chull = TRUE.

xlab

Character string specifying the text for the x-axis label.

ylab

Character string specifying the text for the y-axis label.

main

Character string specifying the text for the main title of the plot.

legend.title

Character string specifying the text for the legend title. Default is "yhat".

...

Additional (optional) arguments to be passed onto geom_line, geom_point, or scale_fill_viridis_c.

Value

A "ggplot" object.

Examples

## Not run: 
#
# Regression example (requires randomForest package to run)
#

# Load required packages
library(ggplot2)  # for autoplot() generic
library(gridExtra)  # for `grid.arrange()`
library(magrittr)  # for forward pipe operator `%>%`
library(randomForest)

# Fit a random forest to the Boston housing data
data (boston)  # load the boston housing data
set.seed(101)  # for reproducibility
boston.rf <- randomForest(cmedv ~ ., data = boston)

# Partial dependence of cmedv on lstat
boston.rf %>%
  partial(pred.var = "lstat") %>%
  autoplot(rug = TRUE, train = boston) + theme_bw()

# Partial dependence of cmedv on lstat and rm
boston.rf %>%
  partial(pred.var = c("lstat", "rm"), chull = TRUE, progress = TRUE) %>%
  autoplot(contour = TRUE, legend.title = "cmedv",
           option = "B", direction = -1) + theme_bw()

# ICE curves and c-ICE curves
age.ice <- partial(boston.rf, pred.var = "lstat", ice = TRUE)
grid.arrange(
  autoplot(age.ice, alpha = 0.1),                 # ICE curves
  autoplot(age.ice, center = TRUE, alpha = 0.1),  # c-ICE curves
  ncol = 2
)

## End(Not run)

bgreenwell/pdp documentation built on June 2, 2022, 2:55 p.m.