# NOT RUN {
#
# Regression example (requires randomForest package to run)
#
# Load required packages
library(ggplot2) # for autoplot() generic
library(gridExtra) # for `grid.arrange()`
library(magrittr) # for forward pipe operator `%>%`
library(randomForest)
# Fit a random forest to the Boston housing data
data (boston) # load the boston housing data
set.seed(101) # for reproducibility
boston.rf <- randomForest(cmedv ~ ., data = boston)
# Partial dependence of cmedv on lstat
boston.rf %>%
partial(pred.var = "lstat") %>%
autoplot(rug = TRUE, train = boston) + theme_bw()
# Partial dependence of cmedv on lstat and rm
boston.rf %>%
partial(pred.var = c("lstat", "rm"), chull = TRUE, progress = TRUE) %>%
autoplot(contour = TRUE, legend.title = "cmedv",
option = "B", direction = -1) + theme_bw()
# ICE curves and c-ICE curves
age.ice <- partial(boston.rf, pred.var = "lstat", ice = TRUE)
grid.arrange(
autoplot(age.ice, alpha = 0.1), # ICE curves
autoplot(age.ice, center = TRUE, alpha = 0.1), # c-ICE curves
ncol = 2
)
# }
Run the code above in your browser using DataLab