# NOT RUN {
#
# Regression example (requires randomForest package to run)
#
Load required packages
library(ggplot2)
library(randomForest)
# Fit a random forest to the mtcars dataset
data(mtcars, package = "datasets")
set.seed(101)
mtcars.rf <- randomForest(mpg ~ ., data = mtcars, mtry = 5, importance = TRUE)
# Topfour predictors
top4 <- topPredictors(mtcars.rf, n = 4)
# Construct partial dependence functions for top four predictors
pd <- NULL
for (i in top4) {
tmp <- partial(mtcars.rf, pred.var = i)
names(tmp) <- c("x", "y")
pd <- rbind(pd, cbind(tmp, predictor = i))
}
# Display partial dependence functions
ggplot(pd, aes(x, y)) +
geom_line() +
facet_wrap(~ predictor, scales = "free") +
theme_bw() +
ylab("mpg")
# }
Run the code above in your browser using DataLab