Fit a GLM with elastic net regularization for a path of lambda values
glmnet.path.RdFit a generalized linear model via penalized maximum likelihood for a path of lambda values. Can deal with any GLM family.
Usage
glmnet.path(
x,
y,
weights = NULL,
lambda = NULL,
nlambda = 100,
lambda.min.ratio = ifelse(nobs < nvars, 0.01, 1e-04),
alpha = 1,
offset = NULL,
family = gaussian(),
standardize = TRUE,
intercept = TRUE,
penalty.factor = rep(1, nvars),
exclude = integer(0),
lower.limits = -Inf,
upper.limits = Inf,
control = glmnet.control()
)Arguments
- x
Input matrix, of dimension
nobs x nvars; each row is an observation vector. Can be a sparse matrix.- y
Quantitative response variable.
- weights
Observation weights. Default is 1 for each observation.
- lambda
A user supplied lambda sequence. Typical usage is to have the program compute its own lambda sequence based on
nlambdaandlambda.min.ratio. Supplying a value of lambda overrides this.- nlambda
The number of lambda values, default is 100.
- lambda.min.ratio
Smallest value for lambda as a fraction of lambda.max, the (data derived) entry value (i.e. the smallest value for which all coefficients are zero). The default depends on the sample size
nobsrelative to the number of variablesnvars. Ifnobs >= nvars, the default is 0.0001, close to zero. Ifnobs < nvars, the default is 0.01. A very small value oflambda.min.ratiowill lead to a saturated fit in thenobs < nvarscase. This is undefined for some families of models, and the function will exit gracefully when the percentage deviance explained is almost 1.- alpha
The elasticnet mixing parameter, with \(0 \le \alpha \le 1\). The penalty is defined as $$(1-\alpha)/2||\beta||_2^2+\alpha||\beta||_1.$$
alpha=1is the lasso penalty, andalpha=0the ridge penalty.- offset
A vector of length
nobsthat is included in the linear predictor. Useful for the "poisson" family (e.g. log of exposure time), or for refining a model by starting at a current fit. Default is NULL. If supplied, then values must also be supplied to thepredictfunction.- family
A description of the error distribution and link function to be used in the model. This is the result of a call to a family function. Default is
gaussian(). (Seefamilyfor details on family functions.)- standardize
Logical flag for x variable standardization, prior to fitting the model sequence. The coefficients are always returned on the original scale. Default is
standardize=TRUE. If variables are in the same units already, you might not wish to standardize.- intercept
Should intercept be fitted (default=TRUE) or set to zero (FALSE)?
- penalty.factor
Separate penalty factors can be applied to each coefficient. This is a number that multiplies
lambdato allow differential shrinkage. Can be 0 for some variables, which implies no shrinkage, and that variable is always included in the model. Default is 1 for all variables (and implicitly infinity for variables listed in exclude). Note: the penalty factors are internally rescaled to sum tonvars.- exclude
Indices of variables to be excluded from the model. Default is none. Equivalent to an infinite penalty factor.
- lower.limits
Vector of lower limits for each coefficient; default
-Inf. Each of these must be non-positive. Can be presented as a single value (which will then be replicated), else a vector of lengthnvars.- upper.limits
Vector of upper limits for each coefficient; default
Inf. Seelower.limits.- control
A fully resolved 17-key control list of the form returned by
glmnet.control(). Default isglmnet.control()– i.e., the current session state. This function does not resolve, validate, or layer the list; it reads keys (thresh,maxit,trace.it,fdev,eps,epsnr,mxitnr, etc.) from it directly. When called fromglmnet(), the argument is populated by.resolve_control()and already reflects any per-call overrides; seeglmnet.controlfor the parameter taxonomy. When calling this function directly (e.g., from test code), either pass nothing (use session state) or build a full list viamodifyList(glmnet.control(), ...).
Value
An object with class "glmnetfit" and "glmnet".
- a0
Intercept sequence of length
length(lambda).- beta
A
nvars x length(lambda)matrix of coefficients, stored in sparse matrix format.- df
The number of nonzero coefficients for each value of lambda.
- dim
Dimension of coefficient matrix.
- lambda
The actual sequence of lambda values used. When alpha=0, the largest lambda reported does not quite give the zero coefficients reported (lambda=inf would in principle). Instead, the largest lambda for alpha=0.001 is used, and the sequence of lambda values is derived from this.
- dev.ratio
The fraction of (null) deviance explained. The deviance calculations incorporate weights if present in the model. The deviance is defined to be 2*(loglike_sat - loglike), where loglike_sat is the log-likelihood for the saturated model (a model with a free parameter per observation). Hence dev.ratio=1-dev/nulldev.
- nulldev
Null deviance (per observation). This is defined to be 2*(loglike_sat -loglike(Null)). The null model refers to the intercept model.
- npasses
Total passes over the data summed over all lambda values.
- jerr
Error flag, for warnings and errors (largely for internal debugging).
- offset
A logical variable indicating whether an offset was included in the model.
- call
The call that produced this object.
- family
Family used for the model.
- nobs
Number of observations.
Details
glmnet.path solves the elastic net problem for a path of lambda values.
It generalizes glmnet::glmnet in that it works for any GLM family.
Sometimes the sequence is truncated before nlambda values of lambda
have been used. This happens when glmnet.path detects that the decrease
in deviance is marginal (i.e. we are near a saturated fit).