Produce cv-rmse for a super_learner specified by a closure that
accepts data and returns a super_learner prediction function.
Usage
cv_super_learner(
data,
learners,
formulas,
y_variable = NULL,
n_folds = 5,
determine_super_learner_weights = NULL,
ensemble_or_discrete = c("ensemble", "discrete"),
cv_schema = NULL,
outcome_type = c("continuous", "binary", "density", "multiclass"),
extra_learner_args = NULL,
cluster_ids = NULL,
strata_ids = NULL,
weights = NULL,
rowids = NULL,
loss_metric = NULL,
use_complete_cases = FALSE,
inner_n_folds = NULL,
inner_cv_schema = NULL
)Arguments
- data
Data to use in training a
super_learner.- learners
A list of predictor/closure-returning-functions. See Details.
- formulas
Either a single regression formula or a vector of regression formulas.
- y_variable
Typically
y_variablecan be inferred automatically from theformulas, but if needed, the y_variable can be specified explicitly.- n_folds
The number of cross-validation folds to use in constructing the
super_learner. Must be a scalar integer of value >= 2.- determine_super_learner_weights
A function/method to determine the weights for each of the candidate
learners. The default is to usedetermine_super_learner_weights_nnls.- ensemble_or_discrete
Defaults to
'ensemble', but can be set to'discrete'. Discretesuper_learner()chooses only one of the candidate learners to have weight 1 in the resulting prediction algorithm, whileensemblesuper_learner()combines predictions from 1 or more candidate learners, with respective weights adding up to 1.- cv_schema
A function that takes
data,n_foldsand returns a list containingtraining_dataandvalidation_data, each of which are lists ofn_foldsdata frames.- outcome_type
One of 'continuous', 'binary', 'multiclass', or 'density'.
outcome_typeis used to infer the correctdetermine_super_learner_weightsfunction if it is not explicitly passed.- extra_learner_args
A list of equal length to the
learnerswith additional arguments to pass to each of the specified learners.- cluster_ids
(default: null) If specified, clusters will either be entirely assigned to training or validation (not both) in each cross-validation split.
- strata_ids
(default: null) If specified, strata are balanced across training and validation splits so that strata appear in both the training and validation splits.
- weights
If specified, (per observation) weights are used to indicate that risk minimization across models (i.e., the meta-learning step) should be targeted to higher weight observations.
- rowids
(default: null) If specified, rowids are stored for use in out-of-fold prediction (
$oof_predict()) and can also be used to validate/observe the behavior of the cross-validation folds construction.- loss_metric
A loss metric function, like the mean-squared-error or negative-log-loss to be used in evaluating the learners on held-out data and minimized through convex optimization. A loss metric should take two (vector) arguments: predictions, and true outcomes, and produce a single statistic summarizing the performance of each learner. Defaults to nadir's internal mean-squared-error function.
- use_complete_cases
(default: FALSE) If the
datapassed have any NA or NaN missing data, restrict thedatatodata[complete.cases(data),].- inner_n_folds
Number of folds used by the inner
super_learner()on each outer training split to estimate ensemble weights. Defaults ton_folds, matching the historical behavior in which one fold count governed both.- inner_cv_schema
Optional
cv_schemafor the innersuper_learner()calls; defaults tocv_schemawhen one is supplied, and otherwise tosuper_learner()'s own defaults.
Value
A list containing $trained_learners and $cv_loss which
respectively include 1) the trained super learner models on each fold of
the data, their holdout predictions and, 2) the cross-validated estimate of
the risk (expected loss) on held-out data.
Details
The idea is that cv_super_learner splits the data into training/validation
splits, trains super_learner on each training split, and then
evaluates their predictions on the held-out validation data, calculating
a root-mean-squared-error on those held-out data.
This function prints a message if the loss_function argument is
not set explicitly, letting the user know that the mean-squared-error will be
used by default. Pass in a loss function explicitly to
super_learner() if you'd like to suppress this message, or use a
similar approach for the appropriate loss function depending on context.
Examples
cv_sl <- cv_super_learner(
data = mtcars,
formula = mpg ~ cyl + hp,
learners = list(lnr_mean, lnr_lm)
)
#> The loss_metric is being inferred based on the outcome_type=continuous -> using CV-MSE
print(cv_sl)
#> Cross-validated Super Learner (nadir_cv_sl)
#> outcome: mpg (continuous)
#> outer folds: 5 inner CV folds: 5
#> cross-validated loss on held-out data: 12.091
#> Methods: $cv_trained_learners, $cv_loss, $crossfit
