Calculates sequential additive variable contributions (approximate SHAP) to the prediction of a single observation, see Gosiewska and Biecek (see reference) and the details below.
light_breakdown(x, ...)# S3 method for default
light_breakdown(x, ...)
# S3 method for flashlight
light_breakdown(
x,
new_obs,
data = x$data,
by = x$by,
v = NULL,
visit_strategy = c("importance", "permutation", "v"),
n_max = Inf,
n_perm = 20,
seed = NULL,
use_linkinv = FALSE,
after_name = "after",
before_name = "before",
label_name = "label",
variable_name = "variable",
step_name = "step",
description_name = "description",
description = TRUE,
digits = 2,
...
)
# S3 method for multiflashlight
light_breakdown(x, ...)
An object of class flashlight
or multiflashlight
.
Further arguments passed to prettyNum
to format numbers in description text.
One single new observation to calculate variable attribution for. Needs to be a data.frame
of same structure as data
.
An optional data.frame
.
An optional vector of column names used to filter data
for rows with equal values in "by" variables as new_obs
.
Vector of variables to assess contribution for. Defaults to all except those specified by "y", "w" and "by".
In what sequence should variables be visited? By "importance", by n_perm
"permutation" or as "v" (see Details).
Maximum number of rows in data
to consider in the reference data. Set to lower value if data
is large.
Number of permutations of random visit sequences. Only used if visit_strategy = "permutation"
.
An integer random seed used to shuffle rows if n_max
is smaller than the number of rows in data
.
Should retransformation function be applied? Default is FALSE
.
Column name in resulting data
containing prediction after the step in step_name
. Defaults to "after".
Column name in resulting data
containing prediction before the step in step_name
. Defaults to "before".
Column name in resulting data
containing the label of the flashlight. Defaults to "label".
Column name in resulting data
containing the variable names. Defaults to "variable".
Column name in resulting data
containing the step of the prediction process. Defaults to "step".
Column name in resulting data
containing the description text to be visualized. Defaults to "description".
Should descriptions be added? Default is TRUE
.
Passed to prettyNum
to format numbers in description text.
An object of class light_breakdown
, light
(and a list) with the following elements.
data
A tibble with results. Can be used to build fully customized visualizations.
by
Same as input by
.
after_name
Same as input after_name
.
before_name
Same as input before_name
.
label_name
Same as input label_name
.
variable_name
Same as input variable_name
.
step_name
Same as input step_name
.
description_name
Same as input description_name
.
default
: Default method not implemented yet.
flashlight
: Variable attribution to single observation for a flashlight.
multiflashlight
: Variable attribution to single observation for a multiflashlight.
The breakdown algorithm works as follows: First, the visit order (x_1, ..., x_m) of the variables v
is specified. Then, in the query data
, the column x_1 is set to the value of x_1 of the single observation new_obs
to be explained. The change in the (weighted) average prediction on data
measures the contribution of x_1 on the prediction of new_obs
. This procedure is iterated over all x_i until eventually, all rows in data
are identical to new_obs
.
A complication with this approach is that the visit order is relevant, at least for non-additive models. Ideally, the algorithm could be repeated for all possible permutations of v
and its results averaged per variable. This is basically what SHAP values do, see the reference below for an explanation. Unfortunately, there is no efficient way to do this in a model agnostic way. We offer two visit strategies to approximate SHAP. The first one uses
the short-cut described in the reference below: The variables are sorted by the size of their contribution in the same way as the breakdown algorithm but without iteration, i.e. starting from the original query data for each variable $x_i$. We call this visit strategy "importance". The second strategy "permutation" averages contributions from a small number of random permutations of v.
Note that the minimum required elements in the (multi-) flashlight are a "predict_function", "model", and "data". The latter can also directly be passed to light_breakdown
. Note that by default, no retransformation function is applied.
A. Gosiewska and P. Biecek (2019). IBREAKDOWN: Uncertainty of model explanations for non-additive predictive models. ArXiv.
# NOT RUN {
fit <- lm(Sepal.Length ~ . + Petal.Length:Species, data = iris)
fl <- flashlight(model = fit, label = "lm", data = iris, y = "Sepal.Length")
light_breakdown(fl, new_obs = iris[1, ])
# }
Run the code above in your browser using DataLab