Creates an ice object with individual conditional expectation curves
for the passed model object, X matrix, predictor, and response. See
Goldstein et al (2013) for further details.
Usage
ice(
object,
X,
y,
predictor,
predictfcn,
verbose = TRUE,
frac_to_build = 1,
indices_to_build = NULL,
num_grid_pts,
logodds = FALSE,
probit = FALSE,
num_cores = 1,
...
)Arguments
- object
The fitted model to estimate ICE curves for.
- X
The design matrix we wish to estimate ICE curves for. Rows are observations, columns are predictors. Typically this is taken to be
object's training data, but this is not strictly necessary.- y
Optional vector of the response values
objectwas trained on. It is used to compute y-axis ranges that are useful for plotting. If not passed, the range of predicted values is used and a warning is printed.- predictor
The column number or variable name in
Xof the predictor of interest, (\(x_S = X[, j]\)).- predictfcn
Optional function that accepts two arguments,
objectandnewdata, and returns anNvector ofobject's predicted response for datanewdata. If this argument is not passed, the procedure attempts to find a genericpredictfunction corresponding toclass(object).- verbose
If
TRUE, prints messages about the procedure's progress.- frac_to_build
Number between 0 and 1, with 1 as default. For large
Xmatrices or fitted models that are slow to make predictions, specifyingfrac_to_buildless than 1 will choose a subset of the observations to build curves for. The subset is chosen such that the remaining observations' values ofpredictorare evenly spaced throughout the quantiles of the fullX[,predictor]vector.- indices_to_build
Vector of indices, \(\subset \{1, \ldots, nrow(X)\}\) specifying which observations to build ICE curves for. As this is an alternative to setting
frac_to_build, both cannot be specified.- num_grid_pts
Optional number of values in the range of
predictorat which to estimate each curve. If missing, the curves are estimated at each unique value ofpredictorin theXobservations we estimate ICE curves for.- logodds
If
TRUE, for classification creates PDPs by plotting the centered log-odds implied by the fitted probabilities. We assume that the generic or passed predict function returns probabilities, and so the flag tells us to transform these to centered logits after the predictions are generated. Note:probitcannot beTRUE.- probit
If
TRUE, for classification creates PDPs by plotting the probit implied by the fitted probabilities. We assume that the generic or passed predict function returns probabilities, and so the flag tells us to transform these to probits after the predictions are generated. Note:logoddscannot beTRUE.- num_cores
Integer number of cores to use for parallel prediction. Defaults to 1.
- ...
Other arguments to be passed to
object's generic predict function.
Value
A list of class ice with the following elements:
- gridpts
Sorted values of
predictorat which each curve is estimated. Duplicates are removed – by definition, elements ofgridptsare unique.- ice_curves
Matrix of dimension
nrow(X)bylength(gridpts). Each row corresponds to an observation's ICE curve, estimated at the values ofpredictoringridpts.- xj
The actual values of
predictorobserved in the data in the order ofXice.- actual_prediction
Vector of length
nrow(X)containing the model's predictions at the actual value of the predictors in the order ofXice.- xlab
String with the predictor name corresponding to
predictor. Ifpredictoris a column number,xlabis set tocolnames(X)[, predictor].- nominal_axis
If
TRUE,length(gridpts)is 5 or fewer; otherwiseFALSE. WhenTRUEtheplotfunction treats the x-axis as if x is nominal.- range_y
If
ywas passed, the range of the response. Otherwise it defaults to bemax(ice_curves)-min(ice_curves)and a message is printed to the console.- sd_y
If
ywas passed, the standard deviation of the response. Otherwise it is defaults tosd(actual_prediction)and a message is printed to the console.- Xice
A matrix containing the subset of
Xfor which ICE curves are estimated. Observations are ordered to be increasing inpredictor. This ordering is the same one as inice_curves,xjandactual_prediction, meaning for all these objects thei-th element refers to the same observation inX.- pdp
A vector of size
length(gridpts)which is a numerical approximation to the partial dependence function (PDP) corresponding to the estimated ICE curves. See Goldstein et al (2013) for a discussion of how the PDP is a form of post-processing. See Friedman (2001) for a description of PDPs.- predictor
Same as the argument, see argument description.
- logodds
Same as the argument, see argument description.
- indices_to_build
Same as the argument, see argument description.
- frac_to_build
Same as the argument, see argument description.
- predictfcn
Same as the argument, see argument description.
References
Jerome Friedman. Greedy Function Approximation: A Gradient Boosting Machine. The Annals of Statistics, 29(5): 1189-1232, 2001.
Goldstein, A., Kapelner, A., Bleich, J., and Pitkin, E., Peeking Inside the Black Box: Visualizing Statistical Learning With Plots of Individual Conditional Expectation. (2014) Journal of Computational and Graphical Statistics, in press
Examples
if (FALSE) { # \dontrun{
require(ICEbox)
require(randomForest)
require(MASS) #has Boston Housing data, Pima
######## regression example
data(Boston) #Boston Housing data
X = Boston
y = X$medv
X$medv = NULL
## build a RF:
bhd_rf_mod = randomForest(X, y)
## Create an 'ice' object for the predictor "age":
bhd.ice = ice(object = bhd_rf_mod, X = X, y = y, predictor = "age", frac_to_build = .1)
} # }