glmnet_with_cv {SVEMnet} | R Documentation |
Fit a glmnet Model with Cross-Validation
Description
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.
Usage
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 |
standardize |
Logical flag passed to |
nfolds |
Number of cross-validation folds (default is |
... |
Additional arguments passed to |
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
.
Value
A list containing:
-
parms
: Coefficients from the selectedcv.glmnet
model atlambda.min
. -
debias_fit
: A linear model of the formy ~ y_pred
used for debiasing (if applicable). -
glmnet_alpha
: The vector ofalpha
values considered. -
best_alpha
: The selectedalpha
value that gave the best cross-validation result. -
best_lambda
: Thelambda
value chosen by cross-validation at the selectedalpha
. -
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 ifdebias_fit
is available. -
formula
: The formula used for model fitting. -
terms
: The terms object extracted from the model frame.
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. doi:10.18637/jss.v033.i01
See Also
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)