bgreenwell / pdp

A general framework for constructing partial dependence (i.e., marginal effect) plots from various types machine learning models in R.
http://bgreenwell.github.io/pdp
93 stars 12 forks source link

Prioritize pdp specific classes over data.frame #102

Closed atusy closed 5 years ago

atusy commented 5 years ago

Currently, results from the pdp package has class like c("data.frame", "partial"). This is not robust to the conflict of S3 methods because a method for data.frame has priority to methods for pdp specific classes. For example, if I define autoplot.data.frame, it is used instead of autoplot.partial by partial(plot = TRUE, plot.engine = "ggplot2"). This PR fixes the problem by prioritizing pdp specific classes over data.frame (e.g., c("partial", "data.frame"))

library(pdp)
library(randomForest)
#> randomForest 4.6-14
#> Type rfNews() to see new features/changes/bug fixes.
data (boston)  # load the boston housing data
set.seed(101)  # for reproducibility
boston.rf <- randomForest(cmedv ~ ., data = boston)

partial(boston.rf, pred.var = "lstat", plot = TRUE, plot.engine = "ggplot2", rug = TRUE)


autoplot.data.frame <- function(object, ...) stop()
partial(boston.rf, pred.var = "lstat", plot = TRUE, plot.engine = "ggplot2", rug = TRUE)
#> Error in autoplot.data.frame(object = pd.df, smooth = smooth, rug = rug, :

Created on 2019-11-04 by the reprex package (v0.3.0)

bgreenwell commented 5 years ago

Good call, thx @atusy!