# NOT RUN {
# We train a random forest on the Boston dataset:
library("randomForest")
data("Boston", package = "MASS")
mod = randomForest(medv ~ ., data = Boston, ntree = 50)
# Compute the partial dependence for the first feature
pdp.obj = pdp(mod, Boston, feature = 1)
# Plot the results directly
plot(pdp.obj)
# Since the result is a ggplot object, you can extend it:
library("ggplot2")
plot(pdp.obj) + theme_bw()
# If you want to do your own thing, just extract the data:
pdp.dat = pdp.obj$data()
head(pdp.dat)
# You can reuse the pdp object for other features:
pdp.obj$feature = 2
plot(pdp.obj)
# Partial dependence plots support up to two features:
pdp.obj = pdp(mod, Boston, feature = c(1,2))
# Partial dependence plots also works with multiclass classification
library("randomForest")
mod = randomForest(Species ~ ., data= iris, ntree=50)
# For some models we have to specify additional arguments for the predict function
plot(pdp(mod, iris, feature = 1, predict.args = list(type = 'prob')))
# For multiclass classification models, you can choose to only show one class:
plot(pdp(mod, iris, feature = 1, class = 1, predict.args = list(type = 'prob')))
# Partial dependence plots support up to two features:
pdp.obj = pdp(mod, iris, feature = c(1,3), predict.args = list(type = 'prob'))
pdp.obj$plot()
# }
Run the code above in your browser using DataLab