# NOT RUN {
# We train a random forest on the Boston dataset:
if(require("randomForest")){
data("Boston", package = "MASS")
mod = randomForest(medv ~ ., data = Boston, ntree = 50)
# Compute the individual conditional expectations for the first feature
ice.obj = ice(mod, Boston, feature = 1)
# Plot the results directly
plot(ice.obj)
# You can center the ICE plot
ice.obj$center.at = 0
plot(ice.obj)
# ICE plots can be centered at initialization
ice.obj = ice(mod, Boston, feature = 1, center=75)
plot(ice.obj)
# Centering can also be removed
ice.obj$center.at = NULL
plot(ice.obj)
# Since the result is a ggplot object, you can extend it:
library("ggplot2")
plot(ice.obj) + theme_bw()
# If you want to do your own thing, just extract the data:
ice.dat = ice.obj$data()
head(ice.dat)
ggplot(ice.dat) +
geom_line(aes(x = crim, y = y.hat, group = ..individual, color = factor(..individual))) +
scale_color_discrete(guide = "none")
# You can reuse the ice object for other features:
ice.obj$feature = 2
plot(ice.obj)
# ICE 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(ice(mod, iris, feature = 1, predict.args = list(type = 'prob')))
# For multiclass classification models, you can choose to only show one class:
plot(ice(mod, iris, feature = 1, class = 1, predict.args = list(type = 'prob')))
# ICE plots can be centered:
plot(ice(mod, iris, feature = 1, center = 1, predict.args = list(type = 'prob')))
}
# }
Run the code above in your browser using DataLab