plot_PDP: plot_PDP

Description Usage Arguments Value See Also Examples

View source: R/plot_PDP.R

Description

Plot a 1D or 2D Partial Dependence Plot (PDP)

Usage

1
2
3
4
5
6
7
8
plot_PDP(
  data,
  model,
  explain_col,
  weight = NULL,
  n_bins = 10,
  use_plotly = TRUE
)

Arguments

data

dataframe - Data to use for the predictions. Should have same columns as training data (can be training data)

model

model object - Model to produce PDP from

explain_col

string - Vector of length 1 for 1D plot or length 2 for 2D plot. This is the factor(s) to explain in the model. The columns must be in data

weight

numeric - Vector of length nrow(data) contains weightings, if NULL even weighting is used

n_bins

numeric - Vector of length 1 for 1D plot and 1 or 2 for 2D plots. This is the number of points to calculate the PDP for

use_plotly

Optional: boolean - If TRUE plotly object is returned else ggplot2 object

Value

plotly/ggplot object of PDP plot

See Also

plot_ALE

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
data <- data.frame(x1=runif(100, 0, 25), x2=runif(100, 0, 25)) %>%
  dplyr::mutate(target=x1^2 * 0.01 + x2 + rnorm(n(),sd=5))

#LM
model_lm <- glm(target ~ poly(x1, 2) + x2, data=data)

plot_PDP(data, model_lm, explain_col="x1", n_bins=5)
plot_PDP(data, model_lm, explain_col="x2", n_bins=5)
plot_PDP(data, model_lm, explain_col=c("x1","x2"), n_bins=5)

#GLM
model_glm <- glm(target ~ poly(x1, 2) + x2, data=data)

plot_PDP(data, model_glm, explain_col="x1", n_bins=5)
plot_PDP(data, model_glm, explain_col="x2", n_bins=5)
plot_PDP(data, model_glm, explain_col=c("x1","x2"), n_bins=5)

#GBM
model_gbm <- xgboost(data = as.matrix(data[,which(!(names(data)=="target"))]), label=data[["target"]], nrounds=20, verbose = 0)
plot_PDP(data[,which(!(names(data)=="target"))], model_gbm, explain_col="x1", n_bins=10)
plot_PDP(data[,which(!(names(data)=="target"))], model_gbm, explain_col="x2", n_bins=10)
plot_PDP(data[,which(!(names(data)=="target"))], model_gbm, explain_col=c("x1","x2"), n_bins=10)

gloverd2/admr documentation built on Dec. 2, 2020, 11:16 p.m.