This learner provides fitting procedures for elastic net models, including both lasso (L1) and ridge (L2) penalized regression, using the glmnet package. The function cv.glmnet is used to select an appropriate value of the regularization parameter lambda. For details on these regularized regression models and glmnet, consider consulting Friedman et al. (2010) ).


An R6Class object inheriting from Lrnr_base.


A learner object inheriting from Lrnr_base with methods for training and prediction. For a full list of learner functionality, see the complete documentation of Lrnr_base.


  • lambda = NULL: An optional vector of lambda values to compare.

  • type.measure = "deviance": The loss to use when selecting lambda. Options documented in cv.glmnet.

  • nfolds = 10: Number of folds to use for internal cross-validation.

  • alpha = 1: The elastic net parameter: alpha = 0 is Ridge (L2-penalized) regression, while alpha = 1 specifies Lasso (L1-penalized) regression. Values in the closed unit interval specify a weighted combination of the two penalties. For further details, consult the documentation of glmnet.

  • nlambda = 100: The number of lambda values to fit. Comparing fewer values will speed up computation, but may hurt the statistical performance. For further details, consult the documentation of cv.glmnet.

  • use_min = TRUE: If TRUE, the smallest value of the lambda regularization parameter is used for prediction (i.e., lambda = cv_fit$lambda.min); otherwise, a larger value is used (i.e., lambda = cv_fit$lambda.1se). The distinction between the two variants is clarified in the documentation of cv.glmnet.

  • stratify_cv = FALSE: Stratify internal cross-validation folds, so that a binary outcome's prevalence for training is roughly the same in the training and validation sets of the internal cross-validation folds? This argument can only be used when the outcome type for training is binomial; and either the id node in the task is not specified, or cv.glmnet's foldid argument is not specified upon initializing the learner.

  • ...: Other parameters passed to cv.glmnet and glmnet.


Friedman J, Hastie T, Tibshirani R (2010). “Regularization paths for generalized linear models via coordinate descent.” Journal of statistical software, 33(1), 1.


mtcars_task <- sl3_Task$new(
  data = mtcars,
  covariates = c(
    "cyl", "disp", "hp", "drat", "wt", "qsec", "vs", "am",
    "gear", "carb"
  outcome = "mpg"
# simple prediction with lasso penalty
lasso_lrnr <- Lrnr_glmnet$new()
lasso_fit <- lasso_lrnr$train(mtcars_task)
lasso_preds <- lasso_fit$predict()

# simple prediction with ridge penalty
ridge_lrnr <- Lrnr_glmnet$new(alpha = 0)
ridge_fit <- ridge_lrnr$train(mtcars_task)
ridge_preds <- ridge_fit$predict()