Calculates different types of profiles across covariable values. By default, partial dependence profiles are calculated (see Friedman). Other options are profiles of ALE (accumulated local effects, see Apley), response, predicted values ("M plots" or "marginal plots", see Apley), residuals, and shap. The results are aggregated either by (weighted) means or by (weighted) quartiles. Note that ALE profiles are calibrated by (weighted) average predictions. In contrast to the suggestions in Apley, we calculate ALE profiles of factors in the same order as the factor levels. They are not being reordered based on similiarity of other variables.
light_profile(x, ...)# S3 method for default
light_profile(x, ...)
# S3 method for flashlight
light_profile(
x,
v = NULL,
data = NULL,
by = x$by,
type = c("partial dependence", "ale", "predicted", "response", "residual", "shap"),
stats = c("mean", "quartiles"),
breaks = NULL,
n_bins = 11,
cut_type = c("equal", "quantile"),
use_linkinv = TRUE,
value_name = "value",
q1_name = "q1",
q3_name = "q3",
label_name = "label",
type_name = "type",
counts_name = "counts",
counts = TRUE,
counts_weighted = FALSE,
v_labels = TRUE,
pred = NULL,
pd_evaluate_at = NULL,
pd_grid = NULL,
pd_indices = NULL,
pd_n_max = 1000,
pd_seed = NULL,
pd_center = c("no", "first", "middle", "last", "mean", "0"),
ale_two_sided = FALSE,
...
)
# S3 method for multiflashlight
light_profile(
x,
v = NULL,
data = NULL,
breaks = NULL,
n_bins = 11,
cut_type = c("equal", "quantile"),
pd_evaluate_at = NULL,
pd_grid = NULL,
...
)
An object of class flashlight
or multiflashlight
.
Further arguments passed to cut3
resp. formatC
in forming the cut breaks of the v
variable. Not relevant for partial dependence and ALE profiles.
The variable to be profiled.
An optional data.frame
. Not used for type = "shap"
.
An optional vector of column names used to additionally group the results.
Type of the profile: Either "partial dependence", "ale", "predicted", "response", "residual", or "shap".
Statistic to calculate: "mean" or "quartiles". For ALE profiles, only "mean" makes sense.
Cut breaks for a numeric v
.
Maxmium number of unique values to evaluate for numeric v
. Only used if neither grid
nor pd_evaluate_at
is specified.
For the default "equal", bins of equal width are created for v
by pretty
. Choose "quantile" to create quantile bins.
Should retransformation function be applied? Default is TRUE. Not used for type "shap".
Column name in resulting data
containing the profile value. Defaults to "value".
Name of the resulting column with first quartile values. Only relevant for stats
"quartiles".
Name of the resulting column with third quartile values. Only relevant for stats
"quartiles".
Column name in resulting data
containing the label of the flashlight. Defaults to "label".
Column name in the resulting data
with the plot type
.
Name of the column containing counts if counts
is TRUE.
Should counts be added?
If counts
is TRUE: Should counts be weighted by the case weights? If TRUE, the sum of w
is returned by group.
If FALSE, return group centers of v
instead of labels. Only relevant for types "response", "predicted" or "residual" and if v
is being binned. In that case useful if e.g. different flashlights use different data sets and bin labels would not match.
Optional vector with predictions (after application of inverse link). Can be used to avoid recalculation of predictions over and over if the functions is to be repeatedly called for different v
and predictions are computationally expensive to make. Only relevant for type = "predicted"
and type = "ale"
. Not implemented for multiflashlight.
Vector with values of v
used to evaluate the profile. Only relevant for type = "partial dependence" and "ale".
A data.frame
with grid values, e.g. generated by expand.grid
. Only used for type = "partial dependence".
A vector of row numbers to consider in calculating partial dependence profiles. Only used for type = "partial dependence" and "ale".
Maximum number of ICE profiles to calculate (will be randomly picked from data
). Only used for type = "partial dependence" and "ale".
Integer random seed used to select ICE profiles. Only used for type = "partial dependence" and "ale".
How should ICE curves be centered? Default is "no". Choose "first", "middle", or "last" to 0-center at specific evaluation points. Choose "mean" to center all profiles at the within-group means. Choose "0" to mean-center curves at 0. Only relevant for partial dependence.
If TRUE
, v
is continuous and breaks
are passed or being calculated, then two-sided derivatives are calculated for ALE instead of left derivatives. More specifically: Usually, local effects at value x are calculated using points between x-e and x. Set ale_two_sided = TRUE
to use points between x-e/2 and x+e/2.
An object of classes light_profile
, light
(and a list) with the following elements.
data
A tibble containing results. Can be used to build fully customized visualizations. Its column names are specified by all other items in this list.
by
Names of group by variable.
v
The variable(s) evaluated.
type
Same as input type
. For information only.
stats
Same as input stats
.
value_name
Same as input value_name
.
q1_name
Same as input q1_name
.
q3_name
Same as input q3_name
.
label_name
Same as input label_name
.
type_name
Same as input type_name
.
counts_name
Same as input counts_name
.
default
: Default method not implemented yet.
flashlight
: Profiles for flashlight.
multiflashlight
: Profiles for multiflashlight.
For numeric covariables v
with more than n_bins
disjoint values, its values are binned. Alternatively, breaks
can be provided to specify the binning. For partial dependence profiles (and partly also ALE profiles), this behaviour can be overritten either by providing a vector of evaluation points (pd_evaluate_at
) or an evaluation pd_grid
. By the latter we mean a data frame with column name(s) with a (multi-)variate evaluation grid. For partial dependence, ALE, and prediction profiles, "model", "predict_function", linkinv" and "data" are required. For response profiles its "y", "linkinv" and "data" and for shap profiles it is just "shap". "data" can be passed on the fly.
Friedman J. H. (2001). Greedy function approximation: A gradient boosting machine. The Annals of Statistics, 29:1189<U+2013>1232.
Apley D. W. (2016). Visualizing the effects of predictor variables in black box supervised learning models.
# NOT RUN {
fit <- lm(Sepal.Length ~ ., data = iris)
fl <- flashlight(model = fit, label = "iris", data = iris, y = "Sepal.Length")
light_profile(fl, v = "Species")
light_profile(fl, v = "Petal.Width", type = "residual")
# }
Run the code above in your browser using DataLab