Cross-validate an elastic net
elastic_net_cv.Rdglmnet's cv.glmnet() on the Rust core: on each fold, fits the whole
lambda path to the other folds (warm starts) and scores the family's
mean deviance on the held-out fold; folds run in parallel. The score per
lambda is the folds' mean weighted by fold weight, with its standard
error. Matches cv.glmnet() on the same folds.
Usage
elastic_net_cv(
formula,
data,
family = "poisson",
link = NULL,
alpha = 1,
lambda = NULL,
nlambda = 100,
lambda_min_ratio = 1e-04,
standardize = TRUE,
penalty_factor = NULL,
offset = NULL,
weights = NULL,
folds = 10,
seed = 1,
foldid = NULL,
theta = NULL,
power = NULL,
link_power = NULL,
lower_limits = NULL,
upper_limits = NULL,
relax = FALSE,
gamma = c(0, 0.25, 0.5, 0.75, 1)
)Arguments
- formula
A model formula;
offset(...)terms are honoured.- data
A data frame.
- family
"gaussian","poisson","gamma","inverse_gaussian","binomial"(response a proportion,weightsthe trials),"negative_binomial"(needstheta) or"tweedie"(needspower).- link
"identity","log","logit","probit","cloglog","inverse","inverse_squared"or"power"(needslink_power);NULLfor the log link for the gamma, inverse Gaussian and Tweedie and the canonical link otherwise. R's ownglm()defaults to the canonical link (Gamma()is the inverse link), so givelinkwhen porting a model from it.- alpha
Mixing between ridge (0) and the lasso (1).
- lambda
Penalty strengths;
NULLfor a path ofnlambdavalues log-spaced from the smallestlambdathat zeroes every coefficient down tolambda_min_ratiotimes it.- nlambda, lambda_min_ratio
The default path.
- standardize
Penalize standardized coefficients.
- penalty_factor
Optional penalty factors: a vector with one per design column, or a named vector for some columns (the rest get 1); 0 leaves a column unpenalized.
- offset
Optional offset added to any
offset()terms.- weights
Optional prior weights.
- folds
Number of folds, used when
foldidisNULL.- seed
Seed of the fold assignment.
- foldid
Optional fold number per row.
- theta
Negative binomial
theta(variancemu + mu^2 / theta).- power
Tweedie power in
(1, 2).- link_power
Exponent of the power link.
- lower_limits, upper_limits
Optional bounds on the coefficients, on the design's scale, as glmnet's
lower.limitsandupper.limits: one value for every column, one per design column, or a named vector for some (the rest unbounded). Lower bounds are at most 0, upper at least 0; the intercept is never bounded.lower_limits = 0makes every coefficient non-negative, which keeps a cumulative step basis monotone.- relax
Also refit each
lambda's non-zero columns without the penalty (glmnet'srelax = TRUE), within the same bounds; thenstats::coef()andstats::predict()takegamma, the blendgamma * penalized + (1 - gamma) * refitted.- gamma
With
relax = TRUE, the relaxations to score, ascv.glmnet(relax = TRUE)'sgamma.
Value
A list with lambda, mean, se, lambda_min (lowest mean)
and lambda_1se (the largest lambda within one standard error of
it), and fit, the elastic_net_model on all rows over lambda.
With relax = TRUE, mean and se are matrices (a row per lambda,
a column per gamma), and gamma_min and gamma_1se go with
lambda_min and lambda_1se: the lowest mean over every pair, and
the largest lambda, then gamma, within one standard error of it,
as cv.glmnet() chooses. fit is then relaxed.