Fit a GLM with elastic net regularization for a single value of lambda
glmnet.fit.RdFit a generalized linear model via penalized maximum likelihood for a single value of lambda. Can deal with any GLM family.
Usage
glmnet.fit(
x,
y,
weights,
lambda,
alpha = 1,
offset = rep(0, nobs),
family = gaussian(),
intercept = TRUE,
penalty.factor = rep(1, nvars),
exclude = c(),
lower.limits = -Inf,
upper.limits = Inf,
warm = NULL,
from.glmnet.path = FALSE,
save.fit = FALSE,
control = glmnet.control()
)Arguments
- x
Input matrix, of dimension
nobs x nvars; each row is an observation vector. If it is a sparse matrix, it is assumed to be unstandardized. It should have attributesxmandxs, wherexm(j)andxs(j)are the centering and scaling factors for variable j respsectively. If it is not a sparse matrix, it is assumed that any standardization needed has already been done.- y
Quantitative response variable.
- weights
Observation weights.
glmnet.fitdoes NOT standardize these weights.- lambda
A single value for the
lambdahyperparameter.- 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.)- 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.- warm
Either a
glmnetfitobject or a list (with namesbetaanda0containing coefficients and intercept respectively) which can be used as a warm start. Default isNULL, indicating no warm start. For internal use only.- from.glmnet.path
Was
glmnet.fit()called fromglmnet.path()? Default is FALSE.This has implications for computation of the penalty factors.- save.fit
Return the warm start object? Default is FALSE.
- control
A fully resolved 17-key control list of the form returned by
glmnet.control(). Default isglmnet.control()– current session state. This function does not resolve or validate the list; keys (thresh,maxit,trace.it,epsnr,mxitnr,big, etc.) are read directly. See?glmnet.pathfor the same contract.
Value
An object with class "glmnetfit" and "glmnet". The list returned contains more keys than that of a "glmnet" object.
- a0
Intercept value.
- beta
A
nvars x 1matrix of coefficients, stored in sparse matrix format.- df
The number of nonzero coefficients.
- dim
Dimension of coefficient matrix.
- lambda
Lambda value used.
- 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.
- 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.
- nobs
Number of observations.
- warm_fit
If
save.fit=TRUE, output of C++ routine, used for warm starts. For internal use only.- family
Family used for the model.
- converged
A logical variable: was the algorithm judged to have converged?
- boundary
A logical variable: is the fitted value on the boundary of the attainable values?
- obj_function
Objective function value at the solution.
Details
WARNING: Users should not call glmnet.fit directly. Higher-level functions
in this package call glmnet.fit as a subroutine. If a warm start object
is provided, some of the other arguments in the function may be overriden.
glmnet.fit solves the elastic net problem for a single, user-specified
value of lambda. glmnet.fit works for any GLM family. It solves the
problem using iteratively reweighted least squares (IRLS). For each IRLS
iteration, glmnet.fit makes a quadratic (Newton) approximation of the
log-likelihood, then calls elnet.fit to minimize the resulting
approximation.
In terms of standardization: glmnet.fit does not standardize x
and weights. penalty.factor is standardized so that they sum up
to nvars.