| WhatIfRegr {counterfactuals} | R Documentation |
WhatIf for Regression Tasks
Description
WhatIf returns the n_counterfactual most similar observations to x_interest from observations in predictor$data$X
whose prediction is in the desired_outcome interval.
Details
Only observations whose features values lie between the corresponding values in lower and upper are considered
counterfactual candidates.
Super classes
counterfactuals::CounterfactualMethod -> counterfactuals::CounterfactualMethodRegr -> WhatIfRegr
Methods
Public methods
Inherited methods
Method new()
Create a new WhatIfRegr object.
Usage
WhatIfRegr$new( predictor, n_counterfactuals = 1L, lower = NULL, upper = NULL, distance_function = "gower" )
Arguments
predictor(Predictor)
The object (created withiml::Predictor$new()) holding the machine learning model and the data.n_counterfactuals(
integerish(1))
The number of counterfactuals to return Default is1L.lower(
numeric()|NULL)
Vector of minimum values for numeric features. IfNULL(default), the element for each numeric feature inloweris taken as its minimum value inpredictor$data$X. If notNULL, it should be named with the corresponding feature names.upper(
numeric()|NULL)
Vector of maximum values for numeric features. IfNULL(default), the element for each numeric feature inupperis taken as its maximum value inpredictor$data$X. If notNULL, it should be named with the corresponding feature names.distance_function(
function()|'gower'|'gower_c')
The distance function used to compute the distances betweenx_interestand the training data points for findingx_nn. Either the name of an already implemented distance function ('gower' or 'gower_c') or a function. If set to 'gower' (default), then Gower's distance (Gower 1971) is used; if set to 'gower_c', a C-based more efficient version of Gower's distance is used. A function must have three argumentsx,y, anddataand should return adoublematrix withnrow(x)rows and maximumnrow(y)columns.
Method clone()
The objects of this class are cloneable with this method.
Usage
WhatIfRegr$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.
References
Gower, J. C. (1971), "A general coefficient of similarity and some of its properties". Biometrics, 27, 623–637.
Wexler, J., Pushkarna, M., Bolukbasi, T., Wattenberg, M., Viégas, F., & Wilson, J. (2019). The what-if tool: Interactive probing of machine learning models. IEEE transactions on visualization and computer graphics, 26(1), 56–65.
Examples
if (require("randomForest")) {
set.seed(123456)
# Train a model
rf = randomForest(mpg ~ ., data = mtcars)
# Create a predictor object
predictor = iml::Predictor$new(rf)
# Find counterfactuals for x_interest
wi_regr = WhatIfRegr$new(predictor, n_counterfactuals = 5L)
cfactuals = wi_regr$find_counterfactuals(
x_interest = mtcars[1L, ], desired_outcome = c(22, 26)
)
# Print the results
cfactuals
}