A wrapper for glmnet::glmnet() fit jointly over a decreasing grid of
lambda values, for use in nadir::super_learner().
Arguments
- data
A dataframe to train a learner / learners on.
- formula
A regression formula to use inside this learner.
- weights
Observation weights; see
?lm- lambda
A numeric vector of penalty values (a grid). Must be explicitly specified; see
validate_lambda_gridfor why auto-generated grids are not supported. Values are deduplicated and sorted in decreasing order.- ...
Any extra arguments that should be passed to the internal model for model fitting purposes.
Value
A nadir_multi_predictor: a named list of prediction
functions, one per lambda value, each of which accepts newdata and
returns a numeric vector of predictions.
Details
glmnet exploits warm starts along the regularization path, so
fitting an entire grid of lambda values costs little more than fitting one
– and is often faster than fitting a single small lambda from a cold
start. lnr_glmnet_grid fits the whole path once and returns a
multi-predictor: one prediction function per lambda value.
super_learner() expands these into distinct pseudo-learners (named
e.g. glmnet_grid_lambda_0.1) which each receive their own weight in
the meta-learning stage.
Compare with lnr_glmnet, which fits a single lambda value, and
lnr_cvglmnet, which internally selects one lambda by
cross-validation and exposes only that model to the meta-learner.
Examples
multi_predictor <- lnr_glmnet_grid(
mtcars, mpg ~ hp + disp + am + wt,
lambda = c(0.01, 0.1, 0.5, 1)
)
names(multi_predictor)
#> [1] "lambda_1" "lambda_0.5" "lambda_0.1" "lambda_0.01"
multi_predictor[["lambda_0.5"]](mtcars)
#> [1] 23.65957 22.88185 25.08866 20.77059 18.11857 20.17458 15.60509 22.29849
#> [9] 21.42247 19.69121 19.69121 16.04594 17.08290 16.93040 11.69102 10.85791
#> [17] 10.64521 26.27121 28.47878 27.41465 23.45115 18.63066 18.88990 14.78162
#> [25] 16.88338 27.07942 25.69812 26.94505 17.32473 21.23630 13.95753 23.20183
