A general framework for constructing partial dependence (i.e., marginal effect) plots from various types of machine learning models in R.

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.
pred.fun)plot(..., lattice = TRUE)batch.size), parallel execution via
foreach, training-data
subsampling (frac), and Friedman's exact weighted tree-traversal method
for gbm modelspdp 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.
citation("pdp"))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.