Learn R Programming

iml (version 0.3.0)

Ice: Individual conditional expectations (Ice)

Description

Ice fits and plots individual conditional expectation curves for prediction models.

Format

R6Class object.

Usage

ice = Ice$new(predictor, feature, grid.size = 20, center.at = NULL, run = TRUE)

plot(ice) ice$results print(ice) ice$set.feature(2) ice$center(1)

Arguments

For Ice$new():

predictor:

(Predictor) The object (created with Predictor$new()) holding the machine learning model and the data.

feature:

(`character(1)`) The feature name or index for which to compute the individual conditional expectations.

grid.size:

(`numeric(1)`) The size of the grid for evaluating the predictions

center.at:

(`numeric(1)`) The value for the centering of the plot. Numeric for numeric features, and the level name for factors.

run:

(`logical(1)`) Should the Interpretation method be run?

Fields

feature.index

The index of the features for which the partial dependence was computed.

feature.name

The names of the features for which the partial dependence was computed.

feature.type

The detected types of the features, either "categorical" or "numerical".

center.at

The value for the centering of the plot. Numeric for numeric features, and the level name for factors.

grid.size

The size of the grid.

predictor

The prediction model that was analysed.

results

data.frame with the grid of feature of interest and the predicted \(\hat{y}\).

Methods

center()

method to set the value at which the ice computations are centered. See examples.

set.feature()

method to set the feature (index) for which to compute individual conditional expectations See examples for usage.

plot()

method to plot the individual conditional expectations. See plot.Ice.

clone()

[internal] method to clone the R6 object.

initialize()

[internal] method to initialize the R6 object.

Details

The individual conditional expectation curves show how the prediction for each instance changes when we vary a single feature.

To learn more about individual conditional expectation, read the Interpretable Machine Learning book: https://christophm.github.io/interpretable-ml-book/ice.html

References

Goldstein, A., Kapelner, A., Bleich, J., and Pitkin, E. (2013). Peeking Inside the Black Box: Visualizing Statistical Learning with Plots of Individual Conditional Expectation, 1-22. https://doi.org/10.1080/10618600.2014.907095

See Also

PartialDependence for partial dependence plots (aggregated ice plots)

Examples

Run this code
# 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