Partial dependence plots (PDPs) and individual conditional expectation (ICE) curves for R: visualize how a fitted model's predictions depend on a subset of its features.
- Model-agnostic — works with dozens of model classes out of the box
(randomForest, ranger, gbm, xgboost, caret, e1071, …) and with any model
via a user-supplied prediction function (
pred.fun) - PDPs and ICE/c-ICE curves for regression and classification
- Lightweight plotting via tinyplot
(base R graphics), with lattice
displays (3-D surfaces, paneled three-predictor plots) behind
plot(..., lattice = TRUE) - Fast: batched predictions (
batch.size), parallel execution via foreach, training-data subsampling (frac), and Friedman's exact weighted tree-traversal method for gbm models - Minimal dependencies: imports only base R packages plus lattice and tinyplot
pdp is no longer available on CRAN due to CRAN's stringent and ever-changing policies. It is now hosted on r-universe, which provides a reliable alternative for distributing R packages.
# Latest stable release (recommended)
install.packages("pdp", repos = c("https://bgreenwell.r-universe.dev", "https://cloud.r-project.org"))
# Or with pak
pak::pak("bgreenwell/pdp@main") # latest stable release
pak::pak("bgreenwell/pdp") # development version (devel branch)library(pdp)
library(randomForest)
data(boston) # ships with pdp
set.seed(101)
rfo <- randomForest(cmedv ~ ., data = boston)
# Partial dependence of cmedv on lstat
pd <- partial(rfo, pred.var = "lstat", train = boston)
plot(pd, rug = TRUE, train = boston)
# Two predictors (false color level plot), restricted to the convex hull
partial(rfo, pred.var = c("lstat", "rm"), chull = TRUE, train = boston,
plot = TRUE)
# ICE curves colored by another feature
ice <- partial(rfo, pred.var = "rm", ice = TRUE, train = boston)
plot(ice, alpha = 0.1, color.by = "lstat", train = boston)partial() returns a plain data frame, so results are easy to post-process or
plot with any graphics package.
- Package website — function reference and vignettes (getting started, ICE curves, performance tips)
- Greenwell, B. M. (2017). "pdp: An R Package for Constructing Partial
Dependence Plots." The R Journal, 9(1), 421–436.
doi:10.32614/RJ-2017-016
(
citation("pdp")) - For variable importance (which pairs naturally with feature effects), see vip
Development happens on the devel
branch (the repository default); main holds stable releases, which is what
r-universe builds and the website documents. Please open pull requests against
devel and report bugs via the
issue tracker.
