glmnet_with_cv function

Fit a glmnet Model with Cross-Validation

Fit a glmnet Model with Cross-Validation

A wrapper function for cv.glmnet that takes input arguments in a manner similar to SVEMnet. This function searches over multiple alpha values by running cv.glmnet() for each provided alpha, and then selects the combination of alpha and lambda with the best cross-validation performance.

glmnet_with_cv( formula, data, glmnet_alpha = c(0, 0.5, 1), standardize = TRUE, nfolds = 10, ... )

Arguments

  • formula: A formula specifying the model to be fitted.
  • data: A data frame containing the variables in the model.
  • glmnet_alpha: Elastic Net mixing parameter(s) (default is c(0, 0.5, 1)). If multiple values are provided, cv.glmnet is run for each alpha, and the model with the lowest cross-validation error is selected.
  • standardize: Logical flag passed to glmnet. If TRUE (default), each variable is standardized before model fitting.
  • nfolds: Number of cross-validation folds (default is 10).
  • ...: Additional arguments passed to cv.glmnet.

Returns

A list containing:

  • parms: Coefficients from the selected cv.glmnet model at lambda.min.
  • debias_fit: A linear model of the form y ~ y_pred used for debiasing (if applicable).
  • glmnet_alpha: The vector of alpha values considered.
  • best_alpha: The selected alpha value that gave the best cross-validation result.
  • best_lambda: The lambda value chosen by cross-validation at the selected alpha.
  • actual_y: The response vector used in the model.
  • training_X: The predictor matrix used in the model.
  • y_pred: The fitted values from the final model (no debiasing).
  • y_pred_debiased: Debiased fitted values if debias_fit is available.
  • formula: The formula used for model fitting.
  • terms: The terms object extracted from the model frame.

Details

This function uses cv.glmnet to fit a generalized linear model with elastic net regularization, performing k-fold cross-validation to select the regularization parameter lambda. If multiple alpha values are provided, it selects the best-performing alpha-lambda pair based on the minimal cross-validation error.

After fitting, the function calculates a debiasing linear model (if possible). This is done by regressing the actual responses on the fitted values obtained from the selected model. The resulting linear model is stored in debias_fit.

Examples

set.seed(0) n <- 50 X1 <- runif(n) X2 <- runif(n) y <- 1 + 2*X1 + 3*X2 + rnorm(n) data <- data.frame(y, X1, X2) model_cv <- glmnet_with_cv(y ~ X1 + X2, data = data, glmnet_alpha = c(0,0.5,1)) predictions <- predict_cv(model_cv, data)

References

Friedman, J., Hastie, T., & Tibshirani, R. (2010). Regularization Paths for Generalized Linear Models via Coordinate Descent. Journal of Statistical Software, 33(1), 1-22. tools:::Rd_expr_doi("10.18637/jss.v033.i01")

See Also

glmnet, cv.glmnet, SVEMnet

  • Maintainer: Andrew T. Karl
  • License: GPL-2 | GPL-3
  • Last published: 2024-12-21

Useful links