Skip to content

bgreenwell/pdp

Repository files navigation

pdp

r-universe version R-CMD-check Codecov test coverage R Journal

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

Installation

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)

Quick start

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.

Documentation

Development

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.

About

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

Topics

Resources

Stars

99 stars

Watchers

5 watching

Forks

Packages

 
 
 

Contributors