# 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 individual conditional expectations for the first feature
ice = Ice$new(mod, feature = "crim")
# Plot the results directly
plot(ice)
# You can center the Ice plot
ice$center(0)
plot(ice)
# Ice plots can be centered at initialization
ice = Ice$new(mod, feature = "crim", center = 75)
plot(ice)
# Centering can also be removed
ice$center(NULL)
plot(ice)
# Since the result is a ggplot object, you can extend it:
if (require("ggplot2")) {
plot(ice) + theme_bw()
# If you want to do your own thing, just extract the data:
iceData = ice$results
head(iceData)
ggplot(iceData) +
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$set.feature("lstat")
plot(ice)
# Ice also works with multiclass classification
rf = randomForest(Species ~ ., data= iris, ntree=50)
predict.fun = function(obj, newdata) predict(obj, newdata, type = "prob")
mod = Predictor$new(rf, data = iris, predict.fun = predict.fun)
# For some models we have to specify additional arguments for the predict function
plot(Ice$new(mod, feature = "Sepal.Length"))
# For multiclass classification models, you can choose to only show one class:
mod = Predictor$new(rf, data = iris, predict.fun = predict.fun, class = "virginica")
plot(Ice$new(mod, feature = "Sepal.Length"))
# Ice plots can be centered:
plot(Ice$new(mod, feature = "Sepal.Length", center = 1))
}
# }
Run the code above in your browser using DataLab