# NOT RUN {
# We train a random forest on the Boston dataset:
if (require("randomForest")) {
data("Boston", package = "MASS")
rf = randomForest(medv ~ ., data = Boston, ntree = 50)
mod = Predictor$new(rf, data = Boston)
# Compute the partial dependence for the first feature
pdp.obj = Partial$new(mod, feature = "crim")
# Plot the results directly
plot(pdp.obj)
# Since the result is a ggplot object, you can extend it:
if (require("ggplot2")) {
plot(pdp.obj) + theme_bw()
}
# If you want to do your own thing, just extract the data:
pdp.dat = pdp.obj$results
head(pdp.dat)
# You can reuse the pdp object for other features:
pdp.obj$set.feature("lstat")
plot(pdp.obj)
# Only plotting the aggregated partial dependence:
pdp.obj = Partial$new(mod, feature = "crim", ice = FALSE)
pdp.obj$plot()
# Only plotting the individual conditional expectation:
pdp.obj = Partial$new(mod, feature = "crim", aggregation = "none")
pdp.obj$plot()
# Partial dependence plots support up to two features:
pdp.obj = Partial$new(mod, feature = c("crim", "lstat"))
plot(pdp.obj)
# Partial dependence plots also works with multiclass classification
rf = randomForest(Species ~ ., data = iris, ntree=50)
mod = Predictor$new(rf, data = iris, type = "prob")
# For some models we have to specify additional arguments for the predict function
plot(Partial$new(mod, feature = "Petal.Width"))
# Partial dependence plots support up to two features:
pdp.obj = Partial$new(mod, feature = c("Sepal.Length", "Petal.Length"))
pdp.obj$plot()
# For multiclass classification models, you can choose to only show one class:
mod = Predictor$new(rf, data = iris, type = "prob", class = 1)
plot(Partial$new(mod, feature = "Sepal.Length"))
}
# }
Run the code above in your browser using DataLab