Computes the stacking weights for an ensemble of base learners using cross-validated out-of-sample predictions.
Usage
ensemble_weights(
y,
X,
type = "average",
learners = NULL,
cv_folds = 5,
cv_subsamples = NULL,
cv_results = NULL,
custom_weights = NULL,
silent = FALSE
)Arguments
- y
The outcome variable.
- X
The feature matrix.
- type
A character string or vector indicating the type(s) of ensemble weights to compute. Default is
"average".- learners
Optional list of base learners. Required when
cv_resultsis not supplied (learners are needed to run cross-validation). Whencv_resultsis supplied,learnersmay be omitted; the number of learners is inferred from the cross-validation residuals. Seeddml-introfor the full specification.- cv_folds
Number of cross-validation folds.
- cv_subsamples
Optional list of subsamples for cross-validation.
- cv_results
Optional pre-computed cross-validation results.
- custom_weights
Optional custom weights matrix.
- silent
A boolean indicating whether to suppress progress messages.
Value
A list containing:
weightsA matrix of computed ensemble weights.
cv_resultsCross-validation results used for computing weights.
See also
Other utilities:
crosspred(),
crossval(),
ddml(),
diagnostics(),
ensemble(),
shortstacking()
Examples
# \donttest{
y = AE98[, "worked"]
X = AE98[, c("age","agefst","black","hisp","othrace")]
# Compute stacking weights via NNLS
ew = ensemble_weights(y, X,
type = "nnls",
learners = list(list(what = ols),
list(what = mdl_glmnet)),
cv_folds = 5,
silent = TRUE)
ew$weights
#> nnls
#> [1,] 0.000000
#> [2,] 0.999721
# }