vip: Variable importance plots

Description Usage Arguments Examples

View source: R/vip.R

Description

Plot variable importance scores for the predictors in a model.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
vip(object, ...)

## Default S3 method:
vip(
  object,
  num_features = 10L,
  geom = c("col", "point", "boxplot", "violin"),
  mapping = NULL,
  aesthetics = list(),
  horizontal = TRUE,
  all_permutations = FALSE,
  jitter = FALSE,
  include_type = FALSE,
  ...
)

## S3 method for class 'model_fit'
vip(object, ...)

Arguments

object

A fitted model object (e.g., a "randomForest" object) or an object that inherits from class "vi".

...

Additional optional arguments to be passed on to vi.

num_features

Integer specifying the number of variable importance scores to plot. Default is 10.

geom

Character string specifying which type of plot to construct. The currently available options are described below.

  • geom = "col" uses geom_col to construct a bar chart of the variable importance scores.

  • geom = "point" uses geom_point to construct a Cleveland dot plot of the variable importance scores.

  • geom = "boxplot" uses geom_boxplot to construct a boxplot plot of the variable importance scores. This option can only for the permutation-based importance method with nsim > 1 and keep = TRUE; see vi_permute for details.

  • geom = "violin" uses geom_violin to construct a violin plot of the variable importance scores. This option can only for the permutation-based importance method with nsim > 1 and keep = TRUE; see vi_permute for details.

mapping

Set of aesthetic mappings created by aes or aes_. See example usage below.

aesthetics

List specifying additional arguments passed on to layer. These are often aesthetics, used to set an aesthetic to a fixed value, like colour = "red" or size = 3. See example usage below.

horizontal

Logical indicating whether or not to plot the importance scores on the x-axis (TRUE). Default is TRUE.

all_permutations

Logical indicating whether or not to plot all permutation scores along with the average. Default is FALSE. (Only used for permutation scores when nsim > 1.)

jitter

Logical indicating whether or not to jitter the raw permutation scores. Default is FALSE. (Only used when all_permutations = TRUE.)

include_type

Logical indicating whether or not to include the type of variable importance computed in the axis label. Default is FALSE.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#
# A projection pursuit regression example
#

# Load the sample data
data(mtcars)

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

# Construct variable importance plot
vip(model, method = "firm")

# Better yet, store the variable importance scores and then plot
vi_scores <- vi(model, method = "firm")
vip(vi_scores, geom = "point", horiz = FALSE)
vip(vi_scores, geom = "point", horiz = FALSE, aesthetics = list(size = 3))

# The `%T>\%` operator is imported for convenience; see ?magrittr::`%T>%`
# for details
vi_scores <- model %>%
  vi(method = "firm") %T>%
  {print(vip(.))}
vi_scores

# Permutation scores (barplot w/ raw values and jittering)
pfun <- function(object, newdata) predict(object, newdata = newdata)
vip(model, method = "permute", train = mtcars, target = "mpg", nsim = 10,
    metric = "rmse", pred_wrapper = pfun,
    aesthetics = list(color = "grey50", fill = "grey50"),
    all_permutations = TRUE, jitter = TRUE)

# Permutation scores (boxplot)
vip(model, method = "permute", train = mtcars, target = "mpg", nsim = 10,
    metric = "rmse", pred_wrapper = pfun, geom = "boxplot")

# Permutation scores (boxplot colored by feature)
library(ggplot2)  # for `aes_string()` function
vip(model, method = "permute", train = mtcars, target = "mpg", nsim = 10,
    metric = "rmse", pred_wrapper = pfun, geom = "boxplot",
    all_permutations = TRUE, mapping = aes_string(fill = "Variable"),
    aesthetics = list(color = "grey35", size = 0.8))

Example output

Attaching package:vipThe following object is masked frompackage:utils:

    vi

# A tibble: 10 x 2
   Variable Importance
   <chr>         <dbl>
 1 wt           3.41  
 2 hp           2.54  
 3 gear         1.82  
 4 qsec         1.52  
 5 cyl          0.725 
 6 am           0.663 
 7 vs           0.433 
 8 drat         0.236 
 9 carb         0.0844
10 disp         0.0242

vip documentation built on Dec. 17, 2020, 5:08 p.m.