| Type: | Package |
| Title: | Lasso and Elastic-Net Regularized Generalized Linear Models |
| Version: | 4.1-10 |
| Date: | 2025-07-15 |
| Depends: | R (≥ 3.6.0), Matrix (≥ 1.0-6) |
| Imports: | methods, utils, foreach, shape, survival, Rcpp |
| Suggests: | knitr, lars, testthat, xfun, rmarkdown |
| SystemRequirements: | C++17 |
| Description: | Extremely efficient procedures for fitting the entire lasso or elastic-net regularization path for linear regression, logistic and multinomial regression models, Poisson regression, Cox model, multiple-response Gaussian, and the grouped multinomial regression; see <doi:10.18637/jss.v033.i01> and <doi:10.18637/jss.v039.i05>. There are two new and important additions. The family argument can be a GLM family object, which opens the door to any programmed family (<doi:10.18637/jss.v106.i01>). This comes with a modest computational cost, so when the built-in families suffice, they should be used instead. The other novelty is the relax option, which refits each of the active sets in the path unpenalized. The algorithm uses cyclical coordinate descent in a path-wise fashion, as described in the papers cited. |
| License: | GPL-2 |
| VignetteBuilder: | knitr |
| Encoding: | UTF-8 |
| URL: | https://glmnet.stanford.edu |
| RoxygenNote: | 7.3.2 |
| LinkingTo: | RcppEigen, Rcpp |
| NeedsCompilation: | yes |
| Packaged: | 2025-07-16 00:40:18 UTC; hastie |
| Author: | Jerome Friedman [aut], Trevor Hastie [aut, cre], Rob Tibshirani [aut], Balasubramanian Narasimhan [aut], Kenneth Tay [aut], Noah Simon [aut], Junyang Qian [ctb], James Yang [aut] |
| Maintainer: | Trevor Hastie <hastie@stanford.edu> |
| Repository: | CRAN |
| Date/Publication: | 2025-07-17 04:50:02 UTC |
Elastic net model paths for some generalized linear models
Description
This package fits lasso and elastic-net model paths for regression, logisticand multinomial regression using coordinate descent. The algorithm isextremely fast, and exploits sparsity in the input x matrix where it exists.A variety of predictions can be made from the fitted models.
Details
| Package: | glmnet |
| Type: | Package |
| Version: | 1.0 |
| Date: | 2008-05-14 |
| License: | What license is it under? |
Very simple to use. Acceptsx,y data for regression models, andproduces the regularization path over a grid of values for the tuningparameterlambda. Only 5 functions:glmnetpredict.glmnetplot.glmnetprint.glmnetcoef.glmnet
Author(s)
Jerome Friedman, Trevor Hastie and Rob Tibshirani
Maintainer:Trevor Hastiehastie@stanford.edu
References
Friedman, J., Hastie, T. and Tibshirani, R. (2008)Regularization Paths for Generalized Linear Models via CoordinateDescent (2010), Journal of Statistical Software, Vol. 33(1), 1-22,doi:10.18637/jss.v033.i01.
Simon, N., Friedman, J., Hastie, T. and Tibshirani, R. (2011)Regularization Paths for Cox's ProportionalHazards Model via Coordinate Descent, Journal of Statistical Software, Vol.39(5), 1-13,doi:10.18637/jss.v039.i05.
Tibshirani,Robert, Bien, J., Friedman, J., Hastie, T.,Simon, N.,Taylor, J. andTibshirani, Ryan. (2012)Strong Rules for Discarding Predictors inLasso-type Problems, JRSSB, Vol. 74(2), 245-266,https://arxiv.org/abs/1011.2234.
Hastie, T., Tibshirani, Robert and Tibshirani, Ryan (2020)Best Subset,Forward Stepwise or Lasso? Analysis and Recommendations Based on Extensive Comparisons,Statist. Sc. Vol. 35(4), 579-592,https://arxiv.org/abs/1707.08692.
Glmnet webpage with four vignettes:https://glmnet.stanford.edu.
See Also
Useful links:
Examples
x = matrix(rnorm(100 * 20), 100, 20)y = rnorm(100)g2 = sample(1:2, 100, replace = TRUE)g4 = sample(1:4, 100, replace = TRUE)fit1 = glmnet(x, y)predict(fit1, newx = x[1:5, ], s = c(0.01, 0.005))predict(fit1, type = "coef")plot(fit1, xvar = "lambda")fit2 = glmnet(x, g2, family = "binomial")predict(fit2, type = "response", newx = x[2:5, ])predict(fit2, type = "nonzero")fit3 = glmnet(x, g4, family = "multinomial")predict(fit3, newx = x[1:3, ], type = "response", s = 0.01)Synthetic dataset with binary response
Description
Randomly generated data for binomial regression example.
Usage
data(BinomialExample)Format
List containing the following elements:
- x
100 by 30 matrix of numeric values.
- y
Numeric vector of length 100 containing 44 zeros and 56 ones.
compute C index for a Cox model
Description
Computes Harrel's C index for predictions from a"coxnet" object.
Usage
Cindex(pred, y, weights = rep(1, nrow(y)))Arguments
pred | Predictions from a |
y | a survival response object - a matrix with two columns "time" and"status"; see documentation for "glmnet" |
weights | optional observation weights |
Details
Computes the concordance index, taking into account censoring.
Author(s)
Trevor Hastiehastie@stanford.edu
References
Harrel Jr, F. E. and Lee, K. L. and Mark, D. B. (1996)Tutorial in biostatistics: multivariable prognostic models: issues indeveloping models, evaluating assumptions and adequacy, and measuring andreducing error, Statistics in Medicine, 15, pages 361–387.
See Also
cv.glmnet
Examples
set.seed(10101)N = 1000p = 30nzc = p/3x = matrix(rnorm(N * p), N, p)beta = rnorm(nzc)fx = x[, seq(nzc)] %*% beta/3hx = exp(fx)ty = rexp(N, hx)tcens = rbinom(n = N, prob = 0.3, size = 1) # censoring indicatory = cbind(time = ty, status = 1 - tcens) # y=Surv(ty,1-tcens) with library(survival)fit = glmnet(x, y, family = "cox")pred = predict(fit, newx = x)apply(pred, 2, Cindex, y=y)cv.glmnet(x, y, family = "cox", type.measure = "C")Synthetic dataset with right-censored survival response
Description
Randomly generated data for Cox regression example.
Usage
data(CoxExample)Format
List containing the following elements:
- x
1,000 by 30 matrix of numeric values.
- y
1,000 by 2 matrix with column names "time" and "status". Thefirst column consists of positive numbers representing time to event,while the second column represents the status indicator(0=right-censored, 1=observed).
Synthetic dataset with multiple Gaussian responses
Description
Randomly generated data for multi-response Gaussian regression example.
Usage
data(MultiGaussianExample)Format
List containing the following elements:
- x
100 by 20 matrix of numeric values.
- y
100 by 4 matrix of numeric values, each column representingone response vector.
Synthetic dataset with multinomial response
Description
Randomly generated data for multinomial regression example.
Usage
data(MultinomialExample)Format
List containing the following elements:
- x
500 by 30 matrix of numeric values.
- y
Numeric vector of length 500 containing 142 ones, 174 twosand 184 threes.
Synthetic dataset with count response
Description
Randomly generated data for Poisson regression example.
Usage
data(PoissonExample)Format
List containing the following elements:
- x
500 by 20 matrix of numeric values.
- y
Numeric vector of length 500 consisting of non-negativeintegers.
Synthetic dataset with Gaussian response
Description
Randomly generated data for Gaussian regression example.
Usage
data(QuickStartExample)Format
List containing the following elements:
- x
100 by 20 matrix of numeric values.
- y
Numeric vector of length 100.
Synthetic dataset with sparse design matrix
Description
Randomly generated data for Gaussian regression example with thedesign matrix x being in sparse matrix format.
Usage
data(SparseExample)Format
List containing the following elements:
- x
100 by 20 matrix of numeric values. x is in sparse matrixformat, having class "dgCMatrix".
- y
Numeric vector of length 100.
assess performance of a 'glmnet' object using test data.
Description
Given a test set, produce summary performance measures for the glmnetmodel(s)
Usage
assess.glmnet( object, newx = NULL, newy, weights = NULL, family = c("gaussian", "binomial", "poisson", "multinomial", "cox", "mgaussian"), ...)confusion.glmnet( object, newx = NULL, newy, family = c("binomial", "multinomial"), ...)roc.glmnet(object, newx = NULL, newy, ...)Arguments
object | Fitted |
newx | If predictions are to made, these are the 'x' values. Requiredfor |
newy | required argument for all functions; the new response values |
weights | For observation weights for the test observations |
family | The family of the model, in case predictions are passed in as'object' |
... | additional arguments to |
Details
assess.glmnet produces all the different performance measuresprovided bycv.glmnet for each of the families. A single vector, or amatrix of predictions can be provided, or fitted model objects or CVobjects. In the case when the predictions are still to be made, the... arguments allow, for example, 'offsets' and other predictionparameters such as values for 'gamma' for 'relaxed' fits.roc.glmnetproduces for a single vector a two column matrix with columns TPR and FPR(true positive rate and false positive rate). This object can be plotted toproduce an ROC curve. If more than one predictions are called for, then alist of such matrices is produced.confusion.glmnet produces aconfusion matrix tabulating the classification results. Again, a singletable or a list, with a print method.
Value
assess.glmnet produces a list of vectors of measures.roc.glmnet a list of 'roc' two-column matrices, andconfusion.glmnet a list of tables. If a single prediction isprovided, or predictions are made from a CV object, the latter two drop thelist status and produce a single matrix or table.
Author(s)
Trevor Hastie and Rob Tibshirani
Maintainer: Trevor Hastiehastie@stanford.edu
See Also
cv.glmnet,glmnet.measures andvignette("relax",package="glmnet")
Examples
data(QuickStartExample)x <- QuickStartExample$x; y <- QuickStartExample$yset.seed(11)train = sample(seq(length(y)),70,replace=FALSE)fit1 = glmnet(x[train,], y[train])assess.glmnet(fit1, newx = x[-train,], newy = y[-train])preds = predict(fit1, newx = x[-train, ], s = c(1, 0.25))assess.glmnet(preds, newy = y[-train], family = "gaussian")fit1c = cv.glmnet(x, y, keep = TRUE)fit1a = assess.glmnet(fit1c$fit.preval, newy=y,family="gaussian")plot(fit1c$lambda, log="x",fit1a$mae,xlab="Log Lambda",ylab="Mean Absolute Error")abline(v=fit1c$lambda.min, lty=2, col="red")data(BinomialExample)x <- BinomialExample$x; y <- BinomialExample$yfit2 = glmnet(x[train,], y[train], family = "binomial")assess.glmnet(fit2,newx = x[-train,], newy=y[-train], s=0.1)plot(roc.glmnet(fit2, newx = x[-train,], newy=y[-train])[[10]])fit2c = cv.glmnet(x, y, family = "binomial", keep=TRUE)idmin = match(fit2c$lambda.min, fit2c$lambda)plot(roc.glmnet(fit2c$fit.preval, newy = y)[[idmin]])data(MultinomialExample)x <- MultinomialExample$x; y <- MultinomialExample$yset.seed(103)train = sample(seq(length(y)),100,replace=FALSE)fit3 = glmnet(x[train,], y[train], family = "multinomial")confusion.glmnet(fit3, newx = x[-train, ], newy = y[-train], s = 0.01)fit3c = cv.glmnet(x, y, family = "multinomial", type.measure="class", keep=TRUE)idmin = match(fit3c$lambda.min, fit3c$lambda)confusion.glmnet(fit3c$fit.preval, newy = y, family="multinomial")[[idmin]]Simulated data for the glmnet vignette
Description
Simple simulated data, used to demonstrate the features of glmnet
Format
Data objects used to demonstrate features in the glmnet vignette
Details
These datasets are artificial, and are used to test out some of thefeatures of glmnet.
Examples
data(QuickStartExample)x <- QuickStartExample$x; y <- QuickStartExample$yglmnet(x, y)fit a glm with all the options inglmnet
Description
Fit a generalized linear model as inglmnet but unpenalized. Thisallows all the features ofglmnet such as sparse x, bounds oncoefficients, offsets, and so on.
Usage
bigGlm(x, ..., path = FALSE)Arguments
x | input matrix |
... | Most other arguments to glmnet that make sense |
path | Since |
Details
This is essentially the same as fitting a "glmnet" model with a single valuelambda=0, but it avoids some edge cases. CAVEAT: If the user tries aproblem with N smaller than or close to p for some models, it is likely tofail (and maybe not gracefully!) If so, use thepath=TRUE argument.
Value
It returns an object of class "bigGlm" that inherits from class"glmnet". That means it can be predicted from, coefficients extracted viacoef. It has its own print method.
Author(s)
Trevor Hastie
Maintainer: Trevor Hastiehastie@stanford.edu
See Also
print,predict, andcoef methods.
Examples
# Gaussianx = matrix(rnorm(100 * 20), 100, 20)y = rnorm(100)fit1 = bigGlm(x, y)print(fit1)fit2=bigGlm(x,y>0,family="binomial")print(fit2)fit2p=bigGlm(x,y>0,family="binomial",path=TRUE)print(fit2p)Extract coefficients from a glmnet object
Description
Similar to other predict methods, this functions predicts fitted values,logits, coefficients and more from a fitted"glmnet" object.
Usage
## S3 method for class 'glmnet'coef(object, s = NULL, exact = FALSE, ...)## S3 method for class 'glmnet'predict( object, newx, s = NULL, type = c("link", "response", "coefficients", "nonzero", "class"), exact = FALSE, newoffset, ...)## S3 method for class 'relaxed'predict( object, newx, s = NULL, gamma = 1, type = c("link", "response", "coefficients", "nonzero", "class"), exact = FALSE, newoffset, ...)Arguments
object | Fitted |
s | Value(s) of the penalty parameter |
exact | This argument is relevant only when predictions are made atvalues of |
... | This is the mechanism for passing arguments like |
newx | Matrix of new values for |
type | Type of prediction required. Type |
newoffset | If an offset is used in the fit, then one must be suppliedfor making predictions (except for |
gamma | Single value of |
Details
The shape of the objects returned are different for"multinomial"objects. This function actually callsNextMethod(), and theappropriate predict method is invoked for each of the three model types.coef(...) is equivalent topredict(type="coefficients",...)
Value
The object returned depends on type.
Author(s)
Jerome Friedman, Trevor Hastie and Rob Tibshirani
Maintainer:Trevor Hastiehastie@stanford.edu
References
Friedman, J., Hastie, T. and Tibshirani, R. (2008)Regularization Paths for Generalized Linear Models via CoordinateDescent (2010), Journal of Statistical Software, Vol. 33(1), 1-22,doi:10.18637/jss.v033.i01.
Simon, N., Friedman, J., Hastie, T. and Tibshirani, R. (2011)Regularization Paths for Cox's ProportionalHazards Model via Coordinate Descent, Journal of Statistical Software, Vol.39(5), 1-13,doi:10.18637/jss.v039.i05.
Glmnet webpage with four vignettes,https://glmnet.stanford.edu.
See Also
glmnet, andprint, andcoef methods, andcv.glmnet.
Examples
x=matrix(rnorm(100*20),100,20)y=rnorm(100)g2=sample(1:2,100,replace=TRUE)g4=sample(1:4,100,replace=TRUE)fit1=glmnet(x,y)predict(fit1,newx=x[1:5,],s=c(0.01,0.005))predict(fit1,type="coef")fit2=glmnet(x,g2,family="binomial")predict(fit2,type="response",newx=x[2:5,])predict(fit2,type="nonzero")fit3=glmnet(x,g4,family="multinomial")predict(fit3,newx=x[1:3,],type="response",s=0.01)Fit a Cox regression model with elastic net regularization for a singlevalue of lambda
Description
Fit a Cox regression model via penalized maximum likelihood for a singlevalue of lambda. Can deal with (start, stop] data and strata, as well assparse design matrices.
Usage
cox.fit( x, y, weights, lambda, alpha = 1, offset = rep(0, nobs), thresh = 1e-10, maxit = 1e+05, penalty.factor = rep(1, nvars), exclude = c(), lower.limits = -Inf, upper.limits = Inf, warm = NULL, from.cox.path = FALSE, save.fit = FALSE, trace.it = 0)Arguments
x | Input matrix, of dimension |
y | Survival response variable, must be a Surv or stratifySurv object. |
weights | Observation weights. |
lambda | A single value for the |
alpha | See glmnet help file |
offset | See glmnet help file |
thresh | Convergence threshold for coordinate descent. Each innercoordinate-descent loop continues until the maximum change in the objectiveafter any coefficient update is less than thresh times the null deviance.Default value is |
maxit | Maximum number of passes over the data; default is |
penalty.factor | See glmnet help file |
exclude | See glmnet help file |
lower.limits | See glmnet help file |
upper.limits | See glmnet help file |
warm | Either a |
from.cox.path | Was |
save.fit | Return the warm start object? Default is FALSE. |
trace.it | Controls how much information is printed to screen. If |
Details
WARNING: Users should not callcox.fit directly. Higher-levelfunctions in this package callcox.fit as a subroutine. If awarm start object is provided, some of the other arguments in the functionmay be overriden.
cox.fit solves the elastic net problem for a single, user-specifiedvalue of lambda.cox.fit works for Cox regression models, including(start, stop] data and strata. It solves the problem using iterativelyreweighted least squares (IRLS). For each IRLS iteration,cox.fitmakes a quadratic (Newton) approximation of the log-likelihood, then callselnet.fit to minimize the resulting approximation.
In terms of standardization:cox.fit does not standardizexandweights.penalty.factor is standardized so that they sumup tonvars.
Value
An object with class "coxnet", "glmnetfit" and "glmnet". The listreturned contains more keys than that of a "glmnet" object.
a0 | Intercept value, |
beta | A |
df | The number of nonzero coefficients. |
dim | Dimension of coefficient matrix. |
lambda | Lambda value used. |
dev.ratio | The fraction of (null) deviance explained. The deviancecalculations incorporate weights if present in the model. The deviance isdefined to be 2*(loglike_sat - loglike), where loglike_sat is the log-likelihoodfor 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 be2*(loglike_sat -loglike(Null)). The null model refers to the 0 model. |
npasses | Total passes over the data. |
jerr | Error flag, for warnings and errors (largely for internaldebugging). |
offset | A logical variable indicating whether an offset was includedin the model. |
call | The call that produced this object. |
nobs | Number of observations. |
warm_fit | If |
family | Family used for the model, always "cox". |
converged | A logical variable: was the algorithm judged to haveconverged? |
boundary | A logical variable: is the fitted value on the boundary ofthe attainable values? |
obj_function | Objective function value at the solution. |
Fit a Cox regression model with elastic net regularization for a path oflambda values
Description
Fit a Cox regression model via penalized maximum likelihood for a path oflambda values. Can deal with (start, stop] data and strata, as well assparse design matrices.
Usage
cox.path( x, y, weights = NULL, offset = NULL, alpha = 1, nlambda = 100, lambda.min.ratio = ifelse(nobs < nvars, 0.01, 1e-04), lambda = NULL, standardize = TRUE, thresh = 1e-10, exclude = NULL, penalty.factor = rep(1, nvars), lower.limits = -Inf, upper.limits = Inf, maxit = 1e+05, trace.it = 0, ...)Arguments
x | See glmnet help file |
y | Survival response variable, must be a |
weights | See glmnet help file |
offset | See glmnet help file |
alpha | See glmnet help file |
nlambda | See glmnet help file |
lambda.min.ratio | See glmnet help file |
lambda | See glmnet help file |
standardize | See glmnet help file |
thresh | Convergence threshold for coordinate descent. Each innercoordinate-descent loop continues until the maximum change in the objectiveafter any coefficient update is less than thresh times the null deviance.Default value is |
exclude | See glmnet help file |
penalty.factor | See glmnet help file |
lower.limits | See glmnet help file |
upper.limits | See glmnet help file |
maxit | See glmnet help file |
trace.it | Controls how much information is printed to screen. Default is |
... | Other arguments passed from glmnet (not used right now). |
Details
Sometimes the sequence is truncated beforenlambda values of lambdahave been used. This happens whencox.path detects that thedecrease in deviance is marginal (i.e. we are near a saturated fit).
Value
An object of class "coxnet" and "glmnet".
a0 | Intercept value, |
beta | A |
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, thelargest lambda reported does not quite give the zero coefficients reported(lambda=inf would in principle). Instead, the largest lambda for alpha=0.001is used, and the sequence of lambda values is derived from this. |
dev.ratio | The fraction of (null) deviance explained. The deviancecalculations incorporate weights if present in the model. The deviance isdefined to be 2*(loglike_sat - loglike), where loglike_sat is the log-likelihoodfor 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 be2*(loglike_sat -loglike(Null)). The null model refers to the 0 model. |
npasses | Total passes over the data summed over all lambda values. |
jerr | Error flag, for warnings and errors (largely for internaldebugging). |
offset | A logical variable indicating whether an offset was includedin the model. |
call | The call that produced this object. |
nobs | Number of observations. |
Examples
set.seed(2)nobs <- 100; nvars <- 15xvec <- rnorm(nobs * nvars)xvec[sample.int(nobs * nvars, size = 0.4 * nobs * nvars)] <- 0x <- matrix(xvec, nrow = nobs)beta <- rnorm(nvars / 3)fx <- x[, seq(nvars / 3)] %*% beta / 3ty <- rexp(nobs, exp(fx))tcens <- rbinom(n = nobs, prob = 0.3, size = 1)jsurv <- survival::Surv(ty, tcens)fit1 <- glmnet:::cox.path(x, jsurv)# works with sparse x matrixx_sparse <- Matrix::Matrix(x, sparse = TRUE)fit2 <- glmnet:::cox.path(x_sparse, jsurv)# example with (start, stop] dataset.seed(2)start_time <- runif(100, min = 0, max = 5)stop_time <- start_time + runif(100, min = 0.1, max = 3)status <- rbinom(n = nobs, prob = 0.3, size = 1)jsurv_ss <- survival::Surv(start_time, stop_time, status)fit3 <- glmnet:::cox.path(x, jsurv_ss)# example with stratajsurv_ss2 <- stratifySurv(jsurv_ss, rep(1:2, each = 50))fit4 <- glmnet:::cox.path(x, jsurv_ss2)Elastic net objective function value for Cox regression model
Description
Returns the elastic net objective function value for Cox regression model.
Usage
cox_obj_function(y, pred, weights, lambda, alpha, coefficients, vp)Arguments
y | Survival response variable, must be a |
pred | Model's predictions for |
weights | Observation weights. |
lambda | A single value for the |
alpha | The elasticnet mixing parameter, with |
coefficients | The model's coefficients. |
vp | Penalty factors for each of the coefficients. |
Compute gradient for Cox model
Description
Compute the gradient of the log partial likelihood at a particular fit for Coxmodel.
Usage
coxgrad(eta, y, w, std.weights = TRUE, diag.hessian = FALSE)Arguments
eta | Fit vector (usually from glmnet at a particular lambda). |
y | Survival response variable, must be a |
w | Observation weights (default is all equal to 1). |
std.weights | If TRUE (default), observation weights are standardizedto sum to 1. |
diag.hessian | If |
Details
Compute a gradient vector at the fitted vector for the log partial likelihood.This is like a residual vector, and useful for manual screening ofpredictors forglmnet in applications wherep is very large(as in GWAS). Uses the Breslow approach to ties.
This function is essentially a wrapper: it checks whether the responseprovided is right-censored or (start, stop] survival data, and calls theappropriate internal routine.
Value
A single gradient vector the same length aseta. Ifdiag.hessian=TRUE, the diagonal of the Hessian isincluded as an attribute "diag_hessian".
See Also
coxnet.deviance
Examples
set.seed(1)eta <- rnorm(10)time <- runif(10, min = 1, max = 10)d <- ifelse(rnorm(10) > 0, 1, 0)y <- survival::Surv(time, d)coxgrad(eta, y)# return diagonal of Hessian as wellcoxgrad(eta, y, diag.hessian = TRUE)# example with (start, stop] datay2 <- survival::Surv(time, time + runif(10), d)coxgrad(eta, y2)# example with stratay2 <- stratifySurv(y, rep(1:2, length.out = 10))coxgrad(eta, y2)Compute deviance for Cox model
Description
Compute the deviance (-2 log partial likelihood) for Cox model.
Usage
coxnet.deviance( pred = NULL, y, x = NULL, offset = NULL, weights = NULL, std.weights = TRUE, beta = NULL)Arguments
pred | Fit vector or matrix (usually from glmnet at a particularlambda or a sequence of lambdas). |
y | Survival response variable, must be a |
x | Optional |
offset | Optional offset vector. |
weights | Observation weights (default is all equal to 1). |
std.weights | If TRUE (default), observation weights are standardizedto sum to 1. |
beta | Optional coefficient vector/matrix, to be supplied if |
Details
Computes the deviance for a single set of predictions, or for a matrixof predictions. The user can either supply the predictionsdirectly through thepred option, or by supplying thex matrixandbeta coefficients. Uses the Breslow approach to ties.
The function first checks ifpred is passed: if so, it is used asthe predictions. Ifpred is not passed butx andbetaare passed, then these values are used to compute the predictions. Ifneitherx norbeta are passed, then the predictions are alltaken to be 0.
coxnet.deviance() is a wrapper: it calls the appropriate internalroutine based on whether the response is right-censored data or(start, stop] survival data.
Value
A vector of deviances, one for each column of predictions.
See Also
coxgrad
Examples
set.seed(1)eta <- rnorm(10)time <- runif(10, min = 1, max = 10)d <- ifelse(rnorm(10) > 0, 1, 0)y <- survival::Surv(time, d)coxnet.deviance(pred = eta, y = y)# if pred not provided, it is set to zero vectorcoxnet.deviance(y = y)# example with x and betax <- matrix(rnorm(10 * 3), nrow = 10)beta <- matrix(1:3, ncol = 1)coxnet.deviance(y = y, x = x, beta = beta)# example with (start, stop] datay2 <- survival::Surv(time, time + runif(10), d)coxnet.deviance(pred = eta, y = y2)# example with stratay2 <- stratifySurv(y, rep(1:2, length.out = 10))coxnet.deviance(pred = eta, y = y2)Cross-validation for glmnet
Description
Does k-fold cross-validation for glmnet, produces a plot, and returns avalue forlambda (andgamma ifrelax=TRUE)
Usage
cv.glmnet( x, y, weights = NULL, offset = NULL, lambda = NULL, type.measure = c("default", "mse", "deviance", "class", "auc", "mae", "C"), nfolds = 10, foldid = NULL, alignment = c("lambda", "fraction"), grouped = TRUE, keep = FALSE, parallel = FALSE, gamma = c(0, 0.25, 0.5, 0.75, 1), relax = FALSE, trace.it = 0, ...)Arguments
x |
|
y | response |
weights | Observation weights; defaults to 1 per observation |
offset | Offset vector (matrix) as in |
lambda | Optional user-supplied lambda sequence; default is |
type.measure | loss to use for cross-validation. Currently fiveoptions, not all available for all models. The default is |
nfolds | number of folds - default is 10. Although |
foldid | an optional vector of values between 1 and |
alignment | This is an experimental argument, designed to fix theproblems users were having with CV, with possible values |
grouped | This is an experimental argument, with default |
keep | If |
parallel | If |
gamma | The values of the parameter for mixing the relaxed fit with theregularized fit, between 0 and 1; default is |
relax | If |
trace.it | If |
... | Other arguments that can be passed to glmnet, for example |
Details
The function runsglmnetnfolds+1 times; the first to get thelambda sequence, and then the remainder to compute the fit with eachof the folds omitted. The error is accumulated, and the average error andstandard deviation over the folds is computed. Note thatcv.glmnetdoes NOT search for values foralpha. A specific value should besupplied, elsealpha=1 is assumed by default. If users would like tocross-validatealpha as well, they should callcv.glmnet witha pre-computed vectorfoldid, and then use this same fold vector inseparate calls tocv.glmnet with different values ofalpha.Note also that the results ofcv.glmnet are random, since the foldsare selected at random. Users can reduce this randomness by runningcv.glmnet many times, and averaging the error curves.
Ifrelax=TRUE then the values ofgamma are used to mix thefits. If\eta is the fit for lasso/elastic net, and\eta_R isthe relaxed fit (with unpenalized coefficients), then a relaxed fit mixed by\gamma is
\eta(\gamma)=(1-\gamma)\eta_R+\gamma\eta.
There ispractically no extra cost for having a lot of values forgamma.However, 5 seems sufficient for most purposes. CV then selects bothgamma andlambda.
Value
an object of class"cv.glmnet" is returned, which is a listwith the ingredients of the cross-validation fit. If the object was createdwithrelax=TRUE then this class has a prefix class of"cv.relaxed".
lambda | the values of |
cvm | The mean cross-validated error - a vector of length |
cvsd | estimate of standard error of |
cvup | upper curve = |
cvlo | lowercurve = |
nzero | number of non-zero coefficients ateach |
name | a text string indicating type of measure(for plotting purposes). |
glmnet.fit | a fitted glmnet object for thefull data. |
lambda.min | value of |
lambda.1se | largest value of |
fit.preval | if |
foldid | if |
index | a one column matrix with the indices of |
relaxed | if |
Author(s)
Jerome Friedman, Trevor Hastie and Rob Tibshirani
Noah Simonhelped develop the 'coxnet' function.
Jeffrey Wong and B. Narasimhanhelped with the parallel option
Maintainer: Trevor Hastiehastie@stanford.edu
References
Friedman, J., Hastie, T. and Tibshirani, R. (2008)Regularization Paths for Generalized Linear Models via CoordinateDescent (2010), Journal of Statistical Software, Vol. 33(1), 1-22,doi:10.18637/jss.v033.i01.
Simon, N., Friedman, J., Hastie, T. and Tibshirani, R. (2011)Regularization Paths for Cox's ProportionalHazards Model via Coordinate Descent, Journal of Statistical Software, Vol.39(5), 1-13,doi:10.18637/jss.v039.i05.
See Also
glmnet andplot,predict, andcoefmethods for"cv.glmnet" and"cv.relaxed" objects.
Examples
set.seed(1010)n = 1000p = 100nzc = trunc(p/10)x = matrix(rnorm(n * p), n, p)beta = rnorm(nzc)fx = x[, seq(nzc)] %*% betaeps = rnorm(n) * 5y = drop(fx + eps)px = exp(fx)px = px/(1 + px)ly = rbinom(n = length(px), prob = px, size = 1)set.seed(1011)cvob1 = cv.glmnet(x, y)plot(cvob1)coef(cvob1)predict(cvob1, newx = x[1:5, ], s = "lambda.min")title("Gaussian Family", line = 2.5)set.seed(1011)cvob1a = cv.glmnet(x, y, type.measure = "mae")plot(cvob1a)title("Gaussian Family", line = 2.5)set.seed(1011)par(mfrow = c(2, 2), mar = c(4.5, 4.5, 4, 1))cvob2 = cv.glmnet(x, ly, family = "binomial")plot(cvob2)title("Binomial Family", line = 2.5)frame()set.seed(1011)cvob3 = cv.glmnet(x, ly, family = "binomial", type.measure = "class")plot(cvob3)title("Binomial Family", line = 2.5)## Not run: cvob1r = cv.glmnet(x, y, relax = TRUE)plot(cvob1r)predict(cvob1r, newx = x[, 1:5])set.seed(1011)cvob3a = cv.glmnet(x, ly, family = "binomial", type.measure = "auc")plot(cvob3a)title("Binomial Family", line = 2.5)set.seed(1011)mu = exp(fx/10)y = rpois(n, mu)cvob4 = cv.glmnet(x, y, family = "poisson")plot(cvob4)title("Poisson Family", line = 2.5)# Multinomialn = 500p = 30nzc = trunc(p/10)x = matrix(rnorm(n * p), n, p)beta3 = matrix(rnorm(30), 10, 3)beta3 = rbind(beta3, matrix(0, p - 10, 3))f3 = x %*% beta3p3 = exp(f3)p3 = p3/apply(p3, 1, sum)g3 = glmnet:::rmult(p3)set.seed(10101)cvfit = cv.glmnet(x, g3, family = "multinomial")plot(cvfit)title("Multinomial Family", line = 2.5)# Coxbeta = rnorm(nzc)fx = x[, seq(nzc)] %*% beta/3hx = exp(fx)ty = rexp(n, hx)tcens = rbinom(n = n, prob = 0.3, size = 1) # censoring indicatory = cbind(time = ty, status = 1 - tcens) # y=Surv(ty,1-tcens) with library(survival)foldid = sample(rep(seq(10), length = n))fit1_cv = cv.glmnet(x, y, family = "cox", foldid = foldid)plot(fit1_cv)title("Cox Family", line = 2.5)# Parallelrequire(doMC)registerDoMC(cores = 4)x = matrix(rnorm(1e+05 * 100), 1e+05, 100)y = rnorm(1e+05)system.time(cv.glmnet(x, y))system.time(cv.glmnet(x, y, parallel = TRUE))## End(Not run)Elastic net deviance value
Description
Returns the elastic net deviance value.
Usage
dev_function(y, mu, weights, family)Arguments
y | Quantitative response variable. |
mu | Model's predictions for |
weights | Observation weights. |
family | A description of the error distribution and link function to beused in the model. This is the result of a call to a family function. |
Extract the deviance from a glmnet object
Description
Compute the deviance sequence from the glmnet object
Usage
## S3 method for class 'glmnet'deviance(object, ...)Arguments
object | fitted glmnet object |
... | additional print arguments |
Details
A glmnet object has componentsdev.ratio andnulldev. Theformer is the fraction of (null) deviance explained. The deviancecalculations incorporate weights if present in the model. The deviance isdefined to be 2*(loglike_sat - loglike), where loglike_sat is thelog-likelihood for the saturated model (a model with a free parameter perobservation). Null deviance is defined to be 2*(loglike_sat-loglike(Null)); The NULL model refers to the intercept model, except forthe Cox, where it is the 0 model. Hence dev.ratio=1-deviance/nulldev, andthisdeviance method returns (1-dev.ratio)*nulldev.
Value
(1-dev.ratio)*nulldev
Author(s)
Jerome Friedman, Trevor Hastie and Rob Tibshirani
Maintainer:Trevor Hastiehastie@stanford.edu
References
Friedman, J., Hastie, T. and Tibshirani, R. (2008)Regularization Paths for Generalized Linear Models via CoordinateDescent
See Also
glmnet,predict,print, andcoefmethods.
Examples
x = matrix(rnorm(100 * 20), 100, 20)y = rnorm(100)fit1 = glmnet(x, y)deviance(fit1)Solve weighted least squares (WLS) problem for a single lambda value
Description
Solves the weighted least squares (WLS) problem for a single lambda value. Internalfunction that users should not call directly.
Usage
elnet.fit( x, y, weights, lambda, alpha = 1, intercept = TRUE, thresh = 1e-07, maxit = 1e+05, penalty.factor = rep(1, nvars), exclude = c(), lower.limits = -Inf, upper.limits = Inf, warm = NULL, from.glmnet.fit = FALSE, save.fit = FALSE)Arguments
x | Input matrix, of dimension |
y | Quantitative response variable. |
weights | Observation weights. |
lambda | A single value for the |
alpha | The elasticnet mixing parameter, with
|
intercept | Should intercept be fitted (default=TRUE) or set to zero (FALSE)? |
thresh | Convergence threshold for coordinate descent. Each innercoordinate-descent loop continues until the maximum change in the objectiveafter any coefficient update is less than thresh times the null deviance.Default value is |
maxit | Maximum number of passes over the data; default is |
penalty.factor | Separate penalty factors can be applied to eachcoefficient. This is a number that multiplies |
exclude | Indices of variables to be excluded from the model. Default isnone. Equivalent to an infinite penalty factor. |
lower.limits | Vector of lower limits for each coefficient; default |
upper.limits | Vector of upper limits for each coefficient; default |
warm | Either a |
from.glmnet.fit | Was |
save.fit | Return the warm start object? Default is FALSE. |
Details
WARNING: Users should not callelnet.fit directly. Higher-level functionsin this package callelnet.fit as a subroutine. If a warm start objectis provided, some of the other arguments in the function may be overriden.
elnet.fit is essentially a wrapper around a C++ subroutine whichminimizes
1/2 \sum w_i (y_i - X_i^T \beta)^2 + \sum \lambda \gamma_j[(1-\alpha)/2 \beta^2+\alpha|\beta|],
over\beta, where\gamma_j is the relative penalty factor on thejth variable. Ifintercept = TRUE, then the term in the first sum isw_i (y_i - \beta_0 - X_i^T \beta)^2, and we are minimizing over both\beta_0 and\beta.
None of the inputs are standardized except forpenalty.factor, whichis standardized so that they sum up tonvars.
Value
An object with class "glmnetfit" and "glmnet". The list returned hasthe same keys as that of aglmnet object, except that it might have anadditionalwarm_fit key.
a0 | Intercept value. |
beta | A |
df | The number of nonzero coefficients. |
dim | Dimension of coefficient matrix. |
lambda | Lambda value used. |
dev.ratio | The fraction of (null) deviance explained. The deviancecalculations incorporate weights if present in the model. The deviance isdefined to be 2*(loglike_sat - loglike), where loglike_sat is the log-likelihoodfor 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 be2*(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 internaldebugging). |
offset | Always FALSE, since offsets do not appear in the WLS problem.Included for compability with glmnet output. |
call | The call that produced this object. |
nobs | Number of observations. |
warm_fit | If |
Helper function for Cox deviance and gradient
Description
Helps to find ties in death times of data.
Usage
fid(x, index)Arguments
x | Sorted vector of death times. |
index | Vector of indices for the death times. |
Value
A list with two arguments.
index_first | A vector of indices for the first observation at eachdeath time as they appear in the sorted list. |
index_ties | If there are no ties at all, this is NULL. If not, this isa list with length equal to the number of unique times with ties. For eachtime with ties, index_ties gives the indices of the observations with adeath at that time. |
Examples
# Example with no tiesglmnet:::fid(c(1, 4, 5, 6), 1:5)# Example with tiesglmnet:::fid(c(1, 1, 1, 2, 3, 3, 4, 4, 4), 1:9)Get lambda max for Cox regression model
Description
Return the lambda max value for Cox regression model, used for computinginitial lambda values. For internal use only.
Usage
get_cox_lambda_max( x, y, alpha, weights = rep(1, nrow(x)), offset = rep(0, nrow(x)), exclude = c(), vp = rep(1, ncol(x)))Arguments
x | Input matrix, of dimension |
y | Survival response variable, must be a |
alpha | The elasticnet mixing parameter, with |
weights | Observation weights. |
offset | Offset for the model. Default is a zero vector of length |
exclude | Indices of variables to be excluded from the model. |
vp | Separate penalty factors can be applied to each coefficient. |
Details
This function is called bycox.path for the value of lambda max.
Whenx is not sparse, it is expected to already by centered and scaled.Whenx is sparse, the function will get its attributesxm andxs for its centering and scaling factors. The value oflambda_max changes depending on whetherx is centered andscaled or not, so we needxm andxs to get the correct value.
Helper function to get etas (linear predictions)
Description
Given x, coefficients and intercept, return linear predictions. Wrapper thatworks with both regular and sparse x. Only works for single set of coefficientsand intercept.
Usage
get_eta(x, beta, a0)Arguments
x | Input matrix, of dimension |
beta | Feature coefficients. |
a0 | Intercept. |
Get null deviance, starting mu and lambda max
Description
Return the null deviance, starting mu and lambda max values forinitialization. For internal use only.
Usage
get_start( x, y, weights, family, intercept, is.offset, offset, exclude, vp, alpha)Arguments
x | Input matrix, of dimension |
y | Quantitative response variable. |
weights | Observation weights. |
family | A description of the error distribution and link function to beused in the model. This is the result of a call to a family function.(See |
intercept | Does the model we are fitting have an intercept term or not? |
is.offset | Is the model being fit with an offset or not? |
offset | Offset for the model. If |
exclude | Indices of variables to be excluded from the model. |
vp | Separate penalty factors can be applied to each coefficient. |
alpha | The elasticnet mixing parameter, with |
Details
This function is called byglmnet.path for null deviance, starting muand lambda max values. It is also called byglmnet.fit when usedwithout warmstart, but they only use the null deviance and starting mu values.
Whenx is not sparse, it is expected to already by centered and scaled.Whenx is sparse, the function will get its attributesxm andxs for its centering and scaling factors.
Note that whetherx is centered & scaled or not, the values ofmuandnulldev don't change. However, the value oflambda_max doeschange, and we needxm andxs to get the correct value.
fit a GLM with lasso or elasticnet regularization
Description
Fit a generalized linear model via penalized maximum likelihood. Theregularization path is computed for the lasso or elasticnet penalty at agrid of values for the regularization parameter lambda. Can deal with allshapes of data, including very large sparse data matrices. Fits linear,logistic and multinomial, poisson, and Cox regression models.
Usage
glmnet( x, y, family = c("gaussian", "binomial", "poisson", "multinomial", "cox", "mgaussian"), weights = NULL, offset = NULL, alpha = 1, nlambda = 100, lambda.min.ratio = ifelse(nobs < nvars, 0.01, 1e-04), lambda = NULL, standardize = TRUE, intercept = TRUE, thresh = 1e-07, dfmax = nvars + 1, pmax = min(dfmax * 2 + 20, nvars), exclude = NULL, penalty.factor = rep(1, nvars), lower.limits = -Inf, upper.limits = Inf, maxit = 1e+05, type.gaussian = ifelse(nvars < 500, "covariance", "naive"), type.logistic = c("Newton", "modified.Newton"), standardize.response = FALSE, type.multinomial = c("ungrouped", "grouped"), relax = FALSE, trace.it = 0, ...)relax.glmnet(fit, x, ..., maxp = n - 3, path = FALSE, check.args = TRUE)Arguments
x | input matrix, of dimension nobs x nvars; each row is an observationvector. Can be in sparse matrix format (inherit from class |
y | response variable. Quantitative for |
family | Either a character string representingone of the built-in families, or else a |
weights | observation weights. Can be total counts if responses areproportion matrices. Default is 1 for each observation |
offset | A vector of length |
alpha | The elasticnet mixing parameter, with
|
nlambda | The number of |
lambda.min.ratio | Smallest value for |
lambda | A user supplied |
standardize | Logical flag for x variable standardization, prior tofitting the model sequence. The coefficients are always returned on theoriginal scale. Default is |
intercept | Should intercept(s) be fitted (default=TRUE) or set to zero(FALSE) |
thresh | Convergence threshold for coordinate descent. Each innercoordinate-descent loop continues until the maximum change in the objectiveafter any coefficient update is less than |
dfmax | Limit the maximum number of variables in the model. Useful forvery large |
pmax | Limit the maximum number of variables ever to be nonzero |
exclude | Indices of variables to be excluded from the model. Defaultis none. Equivalent to an infinite penalty factor for the variables excluded (next item).Users can supply instead an |
penalty.factor | Separate penalty factors can be applied to eachcoefficient. This is a number that multiplies |
lower.limits | Vector of lower limits for each coefficient; default |
upper.limits | Vector of upper limits for each coefficient; default |
maxit | Maximum number of passes over the data for all lambda values;default is 10^5. |
type.gaussian | Two algorithm types are supported for (only) |
type.logistic | If |
standardize.response | This is for the |
type.multinomial | If |
relax | If |
trace.it | If |
... | Additional argument used in |
fit | For |
maxp | a limit on how many relaxed coefficients are allowed. Default is'n-3', where 'n' is the sample size. This may not be sufficient fornon-gaussian familes, in which case users should supply a smaller value.This argument can be supplied directly to 'glmnet'. |
path | Since |
check.args | Should |
Details
The sequence of models implied bylambda is fit by coordinatedescent. Forfamily="gaussian" this is the lasso sequence ifalpha=1, else it is the elasticnet sequence.
The objective function for"gaussian" is
1/2 RSS/nobs +\lambda*penalty,
and for the other models it is
-loglik/nobs +\lambda*penalty.
Note also that for"gaussian",glmnetstandardizes y to have unit variance (using 1/n rather than 1/(n-1) formula)before computing its lambda sequence (and then unstandardizes the resultingcoefficients); if you wish to reproduce/compare results with other software,best to supply a standardized y. The coefficients for any predictorvariables with zero variance are set to zero for all values of lambda.
Details onfamily option
From version 4.0 onwards, glmnet supports both the original built-in families,as well asany family object as used bystats:glm().This opens the door to a wide variety of additional models. For examplefamily=binomial(link=cloglog) orfamily=negative.binomial(theta=1.5) (from the MASS library).Note that the code runs faster for the built-in families.
The built in families are specifed via a character string. For all families,the object produced is a lasso or elasticnet regularization path for fitting thegeneralized linear regression paths, by maximizing the appropriate penalizedlog-likelihood (partial likelihood for the "cox" model). Sometimes thesequence is truncated beforenlambda values oflambda havebeen used, because of instabilities in the inverse link functions near asaturated fit.glmnet(...,family="binomial") fits a traditionallogistic regression model for the log-odds.glmnet(...,family="multinomial") fits a symmetric multinomial model,where each class is represented by a linear model (on the log-scale). Thepenalties take care of redundancies. A two-class"multinomial" modelwill produce the same fit as the corresponding"binomial" model,except the pair of coefficient matrices will be equal in magnitude andopposite in sign, and half the"binomial" values.Two useful additional families are thefamily="mgaussian" family andthetype.multinomial="grouped" option for multinomial fitting. Theformer allows a multi-response gaussian model to be fit, using a "group-lasso" penalty on the coefficients for each variable. Tying the responsestogether like this is called "multi-task" learning in some domains. Thegrouped multinomial allows the same penalty for thefamily="multinomial" model, which is also multi-responsed. For bothof these the penalty on the coefficient vector for variable j is
(1-\alpha)/2||\beta_j||_2^2+\alpha||\beta_j||_2.
Whenalpha=1this is a group-lasso penalty, and otherwise it mixes with quadratic justlike elasticnet. A small detail in the Cox model: if death times are tiedwith censored times, we assume the censored times occurred justbefore the death times in computing the Breslow approximation; ifusers prefer the usual convention ofafter, they can add a smallnumber to all censoring times to achieve this effect.
Details on response forfamily="cox"
For Cox models, the response should preferably be aSurv object,created by theSurv() function insurvival package. Forright-censored data, this object should have type "right", and for(start, stop] data, it should have type "counting". To fit stratified Coxmodels, strata should be added to the response via thestratifySurv()function before passing the response toglmnet(). (For backwardcompatibility, right-censored data can also be passed as atwo-column matrix with columns named 'time' and 'status'. Thelatter is a binary variable, with '1' indicating death, and '0' indicatingright censored.)
Details onrelax option
Ifrelax=TRUEa duplicate sequence of models is produced, where each active set in theelastic-net path is refit without regularization. The result of this is amatching"glmnet" object which is stored on the original object in acomponent named"relaxed", and is part of the glmnet output.Generally users will not callrelax.glmnet directly, unless theoriginal 'glmnet' object took a long time to fit. But if they do, they mustsupply the fit, and all the original arguments used to create that fit. Theycan limit the length of the relaxed path via 'maxp'.
Value
An object with S3 class"glmnet","*", where"*" is"elnet","lognet","multnet","fishnet"(poisson),"coxnet" or"mrelnet" for the various types ofmodels. If the model was created withrelax=TRUE then this class hasa prefix class of"relaxed".
call | the call that produced thisobject |
a0 | Intercept sequence of length |
beta | For |
lambda | The actual sequence of |
dev.ratio | Thefraction of (null) deviance explained (for |
nulldev | Null deviance (per observation). This is defined to be2*(loglike_sat -loglike(Null)); The NULL model refers to the interceptmodel, except for the Cox, where it is the 0 model. |
df | The number ofnonzero coefficients for each value of |
dfmat | For |
dim | dimension of coefficient matrix (ices) |
nobs | number ofobservations |
npasses | total passes over the data summed over alllambda values |
offset | a logical variable indicating whether an offsetwas included in the model |
jerr | error flag, for warnings and errors(largely for internal debugging). |
relaxed | If |
Author(s)
Jerome Friedman, Trevor Hastie, Balasubramanian Narasimhan, NoahSimon, Kenneth Tay and Rob Tibshirani
Maintainer: Trevor Hastiehastie@stanford.edu
References
Friedman, J., Hastie, T. and Tibshirani, R. (2008)Regularization Paths for Generalized Linear Models via CoordinateDescent (2010), Journal of Statistical Software, Vol. 33(1), 1-22,doi:10.18637/jss.v033.i01.
Simon, N., Friedman, J., Hastie, T. and Tibshirani, R. (2011)Regularization Paths for Cox's ProportionalHazards Model via Coordinate Descent, Journal of Statistical Software, Vol.39(5), 1-13,doi:10.18637/jss.v039.i05.
Tibshirani,Robert, Bien, J., Friedman, J., Hastie, T.,Simon, N.,Taylor, J. andTibshirani, Ryan. (2012)Strong Rules for Discarding Predictors inLasso-type Problems, JRSSB, Vol. 74(2), 245-266,https://arxiv.org/abs/1011.2234.
Hastie, T., Tibshirani, Robert and Tibshirani, Ryan (2020)Best Subset,Forward Stepwise or Lasso? Analysis and Recommendations Based on Extensive Comparisons,Statist. Sc. Vol. 35(4), 579-592,https://arxiv.org/abs/1707.08692.
Glmnet webpage with four vignettes:https://glmnet.stanford.edu.
See Also
print,predict,coef andplot methods,and thecv.glmnet function.
Examples
# Gaussianx = matrix(rnorm(100 * 20), 100, 20)y = rnorm(100)fit1 = glmnet(x, y)print(fit1)coef(fit1, s = 0.01) # extract coefficients at a single value of lambdapredict(fit1, newx = x[1:10, ], s = c(0.01, 0.005)) # make predictions# Relaxedfit1r = glmnet(x, y, relax = TRUE) # can be used with any model# multivariate gaussiany = matrix(rnorm(100 * 3), 100, 3)fit1m = glmnet(x, y, family = "mgaussian")plot(fit1m, type.coef = "2norm")# binomialg2 = sample(c(0,1), 100, replace = TRUE)fit2 = glmnet(x, g2, family = "binomial")fit2n = glmnet(x, g2, family = binomial(link=cloglog))fit2r = glmnet(x,g2, family = "binomial", relax=TRUE)fit2rp = glmnet(x,g2, family = "binomial", relax=TRUE, path=TRUE)# multinomialg4 = sample(1:4, 100, replace = TRUE)fit3 = glmnet(x, g4, family = "multinomial")fit3a = glmnet(x, g4, family = "multinomial", type.multinomial = "grouped")# poissonN = 500p = 20nzc = 5x = matrix(rnorm(N * p), N, p)beta = rnorm(nzc)f = x[, seq(nzc)] %*% betamu = exp(f)y = rpois(N, mu)fit = glmnet(x, y, family = "poisson")plot(fit)pfit = predict(fit, x, s = 0.001, type = "response")plot(pfit, y)# Coxset.seed(10101)N = 1000p = 30nzc = p/3x = matrix(rnorm(N * p), N, p)beta = rnorm(nzc)fx = x[, seq(nzc)] %*% beta/3hx = exp(fx)ty = rexp(N, hx)tcens = rbinom(n = N, prob = 0.3, size = 1) # censoring indicatory = cbind(time = ty, status = 1 - tcens) # y=Surv(ty,1-tcens) with library(survival)fit = glmnet(x, y, family = "cox")plot(fit)# Cox example with (start, stop] dataset.seed(2)nobs <- 100; nvars <- 15xvec <- rnorm(nobs * nvars)xvec[sample.int(nobs * nvars, size = 0.4 * nobs * nvars)] <- 0x <- matrix(xvec, nrow = nobs)start_time <- runif(100, min = 0, max = 5)stop_time <- start_time + runif(100, min = 0.1, max = 3)status <- rbinom(n = nobs, prob = 0.3, size = 1)jsurv_ss <- survival::Surv(start_time, stop_time, status)fit <- glmnet(x, jsurv_ss, family = "cox")# Cox example with stratajsurv_ss2 <- stratifySurv(jsurv_ss, rep(1:2, each = 50))fit <- glmnet(x, jsurv_ss2, family = "cox")# Sparsen = 10000p = 200nzc = trunc(p/10)x = matrix(rnorm(n * p), n, p)iz = sample(1:(n * p), size = n * p * 0.85, replace = FALSE)x[iz] = 0sx = Matrix(x, sparse = TRUE)inherits(sx, "sparseMatrix") #confirm that it is sparsebeta = rnorm(nzc)fx = x[, seq(nzc)] %*% betaeps = rnorm(n)y = fx + epspx = exp(fx)px = px/(1 + px)ly = rbinom(n = length(px), prob = px, size = 1)system.time(fit1 <- glmnet(sx, y))system.time(fit2n <- glmnet(x, y))Internal glmnet functions
Description
These are not intended for use by users.lambda.interp does linearinterpolation of the lambdas to obtain a prediction at a new point s.glmnet_softmax does the classification for multinomial models.nonzeroCoef determines in an efficient manner which variables arenonzero in each fit.jerr prints out error messages from the C++ routines.plotCoef is called by theplot method forglmnetobjects.check_dots is used incoef andpredict withargumentexact=TRUE, to make sure user supplies original data used tofit the"glmnet" object.
Author(s)
Trevor Hastie
internal glmnet parameters
Description
View and/or change the factory default parameters in glmnet
Usage
glmnet.control( fdev = 1e-05, devmax = 0.999, eps = 1e-06, big = 9.9e+35, mnlam = 5, pmin = 1e-09, exmx = 250, prec = 1e-10, mxit = 100, itrace = 0, epsnr = 1e-06, mxitnr = 25, factory = FALSE)Arguments
fdev | minimum fractional change in deviance for stopping path; factorydefault = 1.0e-5 |
devmax | maximum fraction of explained deviance for stopping path;factory default = 0.999 |
eps | minimum value of lambda.min.ratio (see glmnet); factory default=1.0e-6 |
big | large floating point number; factory default = 9.9e35. Inf indefinition of upper.limit is set to big |
mnlam | minimum number of path points (lambda values) allowed; factorydefault = 5 |
pmin | minimum probability for any class. factory default = 1.0e-9.Note that this implies a pmax of 1-pmin. |
exmx | maximum allowed exponent. factory default = 250.0 |
prec | convergence threshold for multi response bounds adjustmentsolution. factory default = 1.0e-10 |
mxit | maximum iterations for multiresponse bounds adjustment solution.factory default = 100 |
itrace | If 1 then progress bar is displayed when running |
epsnr | convergence threshold for |
mxitnr | maximum iterations for the IRLS loop in |
factory | If |
Details
If called with no arguments,glmnet.control() returns a list with thecurrent settings of these parameters. Any arguments included in the callsets those parameters to the new values, and then silently returns. Thevalues set are persistent for the duration of the R session.
Value
A list with named elements as in the argument list
Author(s)
Jerome Friedman, Kenneth Tay, Trevor Hastie
Maintainer: Trevor Hastiehastie@stanford.edu
See Also
glmnet
Examples
glmnet.control(fdev = 0) #continue along path even though not much changesglmnet.control() # view current settingsglmnet.control(factory = TRUE) # reset all the parameters to their defaultFit a GLM with elastic net regularization for a single value of lambda
Description
Fit a generalized linear model via penalized maximum likelihood for a singlevalue 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, thresh = 1e-10, maxit = 1e+05, penalty.factor = rep(1, nvars), exclude = c(), lower.limits = -Inf, upper.limits = Inf, warm = NULL, from.glmnet.path = FALSE, save.fit = FALSE, trace.it = 0)Arguments
x | Input matrix, of dimension |
y | Quantitative response variable. |
weights | Observation weights. |
lambda | A single value for the |
alpha | The elasticnet mixing parameter, with
|
offset | A vector of length |
family | A description of the error distribution and link function to beused in the model. This is the result of a call to a family function. Defaultis |
intercept | Should intercept be fitted (default=TRUE) or set to zero (FALSE)? |
thresh | Convergence threshold for coordinate descent. Each innercoordinate-descent loop continues until the maximum change in the objectiveafter any coefficient update is less than thresh times the null deviance.Default value is |
maxit | Maximum number of passes over the data; default is |
penalty.factor | Separate penalty factors can be applied to eachcoefficient. This is a number that multiplies |
exclude | Indices of variables to be excluded from the model. Default isnone. Equivalent to an infinite penalty factor. |
lower.limits | Vector of lower limits for each coefficient; default |
upper.limits | Vector of upper limits for each coefficient; default |
warm | Either a |
from.glmnet.path | Was |
save.fit | Return the warm start object? Default is FALSE. |
trace.it | Controls how much information is printed to screen. If |
Details
WARNING: Users should not callglmnet.fit directly. Higher-level functionsin this package callglmnet.fit as a subroutine. If a warm start objectis provided, some of the other arguments in the function may be overriden.
glmnet.fit solves the elastic net problem for a single, user-specifiedvalue of lambda.glmnet.fit works for any GLM family. It solves theproblem using iteratively reweighted least squares (IRLS). For each IRLSiteration,glmnet.fit makes a quadratic (Newton) approximation of thelog-likelihood, then callselnet.fit to minimize the resultingapproximation.
In terms of standardization:glmnet.fit does not standardizexandweights.penalty.factor is standardized so that they sum uptonvars.
Value
An object with class "glmnetfit" and "glmnet". The listreturned contains more keys than that of a "glmnet" object.
a0 | Intercept value. |
beta | A |
df | The number of nonzero coefficients. |
dim | Dimension of coefficient matrix. |
lambda | Lambda value used. |
dev.ratio | The fraction of (null) deviance explained. The deviancecalculations incorporate weights if present in the model. The deviance isdefined to be 2*(loglike_sat - loglike), where loglike_sat is the log-likelihoodfor 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 be2*(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 internaldebugging). |
offset | A logical variable indicating whether an offset was includedin the model. |
call | The call that produced this object. |
nobs | Number of observations. |
warm_fit | If |
family | Family used for the model. |
converged | A logical variable: was the algorithm judged to haveconverged? |
boundary | A logical variable: is the fitted value on the boundary ofthe attainable values? |
obj_function | Objective function value at the solution. |
Display the names of the measures used in CV for different "glmnet" families
Description
Produces a list of names of measures
Usage
glmnet.measures( family = c("all", "gaussian", "binomial", "poisson", "multinomial", "cox", "mgaussian", "GLM"))Arguments
family | If a "glmnet" family is supplied, a list of the names ofmeasures available for that family are produced. Default is "all", in whichcase the names of measures for all families are produced. |
Details
Try it and see. A very simple function to provide information
Author(s)
Trevor Hastie
Maintainer: Trevor Hastiehastie@stanford.edu
See Also
cv.glmnet andassess.glmnet.
Fit a GLM with elastic net regularization for a path of lambda values
Description
Fit a generalized linear model via penalized maximum likelihood for a path oflambda 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, thresh = 1e-10, maxit = 1e+05, penalty.factor = rep(1, nvars), exclude = integer(0), lower.limits = -Inf, upper.limits = Inf, trace.it = 0)Arguments
x | Input matrix, of dimension |
y | Quantitative response variable. |
weights | Observation weights. Default is 1 for each observation. |
lambda | A user supplied lambda sequence. Typical usage is to have theprogram compute its own lambda sequence based on |
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 allcoefficients are zero). The default depends on the sample size |
alpha | The elasticnet mixing parameter, with
|
offset | A vector of length |
family | A description of the error distribution and link function to beused in the model. This is the result of a call to a family function. Defaultis |
standardize | Logical flag for x variable standardization, prior tofitting the model sequence. The coefficients are always returned on theoriginal scale. Default is |
intercept | Should intercept be fitted (default=TRUE) or set to zero (FALSE)? |
thresh | Convergence threshold for coordinate descent. Each innercoordinate-descent loop continues until the maximum change in the objectiveafter any coefficient update is less than thresh times the null deviance.Default value is |
maxit | Maximum number of passes over the data; default is |
penalty.factor | Separate penalty factors can be applied to eachcoefficient. This is a number that multiplies |
exclude | Indices of variables to be excluded from the model. Default isnone. Equivalent to an infinite penalty factor. |
lower.limits | Vector of lower limits for each coefficient; default |
upper.limits | Vector of upper limits for each coefficient; default |
trace.it | Controls how much information is printed to screen. Default is |
Details
glmnet.path solves the elastic net problem for a path of lambda values.It generalizesglmnet::glmnet in that it works for any GLM family.
Sometimes the sequence is truncated beforenlambda values of lambdahave been used. This happens whenglmnet.path detects that the decreasein deviance is marginal (i.e. we are near a saturated fit).
Value
An object with class "glmnetfit" and "glmnet".
a0 | Intercept sequence of length |
beta | A |
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, thelargest lambda reported does not quite give the zero coefficients reported(lambda=inf would in principle). Instead, the largest lambda for alpha=0.001is used, and the sequence of lambda values is derived from this. |
dev.ratio | The fraction of (null) deviance explained. The deviancecalculations incorporate weights if present in the model. The deviance isdefined to be 2*(loglike_sat - loglike), where loglike_sat is the log-likelihoodfor 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 be2*(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 internaldebugging). |
offset | A logical variable indicating whether an offset was includedin the model. |
call | The call that produced this object. |
family | Family used for the model. |
nobs | Number of observations. |
Examples
set.seed(1)x <- matrix(rnorm(100 * 20), nrow = 100)y <- ifelse(rnorm(100) > 0, 1, 0)# binomial with probit linkfit1 <- glmnet:::glmnet.path(x, y, family = binomial(link = "probit"))convert a data frame to a data matrix with one-hot encoding
Description
Converts a data frame to a data matrix suitable for input toglmnet.Factors are converted to dummy matrices via "one-hot" encoding. Options dealwith missing values and sparsity.
Usage
makeX(train, test = NULL, na.impute = FALSE, sparse = FALSE, ...)Arguments
train | Required argument. A dataframe consisting of vectors, matricesand factors |
test | Optional argument. A dataframe matching 'train' for use astesting data |
na.impute | Logical, default |
sparse | Logical, default |
... | additional arguments, currently unused |
Details
The main function is to convert factors to dummy matrices via "one-hot"encoding. Having the 'train' and 'test' data present is useful if somefactor levels are missing in either. Since a factor with k levels leads to asubmatrix with 1/k entries zero, with large k thesparse=TRUE optioncan be helpful; a large matrix will be returned, but stored in sparse matrixformat. Finally, the function can deal with missing data. The currentversion has the option to replace missing observations with the mean fromthe training data. For dummy submatrices, these are the mean proportions ateach level.
Value
If only 'train' was provided, the function returns a matrix 'x'. Ifmissing values were imputed, this matrix has an attribute containing itscolumn means (before imputation). If 'test' was provided as well, a listwith two components is returned: 'x' and 'xtest'.
Author(s)
Trevor Hastie
Maintainer: Trevor Hastiehastie@stanford.edu
See Also
glmnet
Examples
set.seed(101)### Single data frameX = matrix(rnorm(20), 10, 2)X3 = sample(letters[1:3], 10, replace = TRUE)X4 = sample(LETTERS[1:3], 10, replace = TRUE)df = data.frame(X, X3, X4)makeX(df)makeX(df, sparse = TRUE)### Single data freame with missing valuesXn = XXn[3, 1] = NAXn[5, 2] = NAX3n = X3X3n[6] = NAX4n = X4X4n[9] = NAdfn = data.frame(Xn, X3n, X4n)makeX(dfn)makeX(dfn, sparse = TRUE)makeX(dfn, na.impute = TRUE)makeX(dfn, na.impute = TRUE, sparse = TRUE)### Test data as wellX = matrix(rnorm(10), 5, 2)X3 = sample(letters[1:3], 5, replace = TRUE)X4 = sample(LETTERS[1:3], 5, replace = TRUE)dft = data.frame(X, X3, X4)makeX(df, dft)makeX(df, dft, sparse = TRUE)### Missing data in test as wellXn = XXn[3, 1] = NAXn[5, 2] = NAX3n = X3X3n[1] = NAX4n = X4X4n[2] = NAdftn = data.frame(Xn, X3n, X4n)makeX(dfn, dftn)makeX(dfn, dftn, sparse = TRUE)makeX(dfn, dftn, na.impute = TRUE)makeX(dfn, dftn, sparse = TRUE, na.impute = TRUE)Helper function to fit coxph model for survfit.coxnet
Description
This function constructs the coxph call needed to run the "hack" ofcoxph with 0 iterations. It's a separate function as we have to deal withfunction options like strata, offset and observation weights.
Usage
mycoxph(object, s, ...)Arguments
object | A class |
s | The value of the penalty parameter lambda at which the survivalcurve is required. |
... | The same ... that was passed to survfit.coxnet. |
Helper function to amend ... for new data in survfit.coxnet
Description
This function amends the function arguments passed to survfit.coxnetvia ... if new data was passed to survfit.coxnet. It's a separatefunction as we have to deal with function options like newstrataand newoffset.
Usage
mycoxpred(object, s, ...)Arguments
object | A class |
s | The response for the fitted model. |
... | The same ... that was passed to survfit.coxnet. |
Replace the missing entries in a matrix columnwise with the entries in asupplied vector
Description
Missing entries in any given column of the matrix are replaced by the columnmeans or the values in a supplied vector.
Usage
na.replace(x, m = rowSums(x, na.rm = TRUE))Arguments
x | A matrix with potentially missing values, and also potentially insparse matrix format (i.e. inherits from "sparseMatrix") |
m | Optional argument. A vector of values used to replace the missingentries, columnwise. If missing, the column means of 'x' are used |
Details
This is a simple imputation scheme. This function is called bymakeXif thena.impute=TRUE option is used, but of course can be used onits own. If 'x' is sparse, the result is sparse, and the replacements aredone so as to maintain sparsity.
Value
A version of 'x' is returned with the missing values replaced.
Author(s)
Trevor Hastie
Maintainer: Trevor Hastiehastie@stanford.edu
See Also
makeX andglmnet
Examples
set.seed(101)### Single data frameX = matrix(rnorm(20), 10, 2)X[3, 1] = NAX[5, 2] = NAX3 = sample(letters[1:3], 10, replace = TRUE)X3[6] = NAX4 = sample(LETTERS[1:3], 10, replace = TRUE)X4[9] = NAdfn = data.frame(X, X3, X4)x = makeX(dfn)m = rowSums(x, na.rm = TRUE)na.replace(x, m)x = makeX(dfn, sparse = TRUE)na.replace(x, m)Elastic net objective function value
Description
Returns the elastic net objective function value.
Usage
obj_function(y, mu, weights, family, lambda, alpha, coefficients, vp)Arguments
y | Quantitative response variable. |
mu | Model's predictions for |
weights | Observation weights. |
family | A description of the error distribution and link function to beused in the model. This is the result of a call to a family function. |
lambda | A single value for the |
alpha | The elasticnet mixing parameter, with |
coefficients | The model's coefficients (excluding intercept). |
vp | Penalty factors for each of the coefficients. |
Elastic net penalty value
Description
Returns the elastic net penalty value without thelambda factor.
Usage
pen_function(coefficients, alpha = 1, vp = 1)Arguments
coefficients | The model's coefficients (excluding intercept). |
alpha | The elasticnet mixing parameter, with |
vp | Penalty factors for each of the coefficients. |
Details
The penalty is defined as
(1-\alpha)/2 \sum vp_j \beta_j^2 + \alpha \sum vp_j |\beta|.
Note the omission of the multiplicativelambda factor.
plot the cross-validation curve produced by cv.glmnet
Description
Plots the cross-validation curve, and upper and lower standard deviationcurves, as a function of thelambda values used. If the object hasclass"cv.relaxed" a different plot is produced, showing bothlambda andgamma
Usage
## S3 method for class 'cv.glmnet'plot(x, sign.lambda = -1, ...)## S3 method for class 'cv.relaxed'plot(x, se.bands = TRUE, sign.lambda = -1, ...)Arguments
x | fitted |
sign.lambda | Either plot against |
... | Other graphical parameters to plot |
se.bands | Should shading be produced to show standard-error bands;default is |
Details
A plot is produced, and nothing is returned.
Author(s)
Jerome Friedman, Trevor Hastie and Rob Tibshirani
Maintainer:Trevor Hastiehastie@stanford.edu
References
Friedman, J., Hastie, T. and Tibshirani, R. (2008)Regularization Paths for Generalized Linear Models via CoordinateDescent
See Also
glmnet andcv.glmnet.
Examples
set.seed(1010)n = 1000p = 100nzc = trunc(p/10)x = matrix(rnorm(n * p), n, p)beta = rnorm(nzc)fx = (x[, seq(nzc)] %*% beta)eps = rnorm(n) * 5y = drop(fx + eps)px = exp(fx)px = px/(1 + px)ly = rbinom(n = length(px), prob = px, size = 1)cvob1 = cv.glmnet(x, y)plot(cvob1)title("Gaussian Family", line = 2.5)cvob1r = cv.glmnet(x, y, relax = TRUE)plot(cvob1r)frame()set.seed(1011)par(mfrow = c(2, 2), mar = c(4.5, 4.5, 4, 1))cvob2 = cv.glmnet(x, ly, family = "binomial")plot(cvob2)title("Binomial Family", line = 2.5)## set.seed(1011)## cvob3 = cv.glmnet(x, ly, family = "binomial", type = "class")## plot(cvob3)## title("Binomial Family", line = 2.5)plot coefficients from a "glmnet" object
Description
Produces a coefficient profile plot of the coefficient paths for a fitted"glmnet" object.
Usage
## S3 method for class 'glmnet'plot( x, xvar = c("lambda", "norm", "dev"), label = FALSE, sign.lambda = -1, ...)## S3 method for class 'mrelnet'plot( x, xvar = c("lambda", "norm", "dev"), label = FALSE, sign.lambda = -1, type.coef = c("coef", "2norm"), ...)## S3 method for class 'multnet'plot( x, xvar = c("lambda", "norm", "dev"), label = FALSE, sign.lambda = -1, type.coef = c("coef", "2norm"), ...)## S3 method for class 'relaxed'plot( x, xvar = c("lambda", "dev"), label = FALSE, sign.lambda = -1, gamma = 1, ...)Arguments
x | fitted |
xvar | What is on the X-axis. |
label | If |
sign.lambda | If |
... | Other graphical parameters to plot |
type.coef | If |
gamma | Value of the mixing parameter for a "relaxed" fit |
Details
A coefficient profile plot is produced. Ifx is a multinomial model,a coefficient plot is produced for each class.
Author(s)
Jerome Friedman, Trevor Hastie and Rob Tibshirani
Maintainer:Trevor Hastiehastie@stanford.edu
References
Friedman, J., Hastie, T. and Tibshirani, R. (2008)Regularization Paths for Generalized Linear Models via CoordinateDescent
See Also
glmnet, andprint,predict andcoefmethods.
Examples
x=matrix(rnorm(100*20),100,20)y=rnorm(100)g2=sample(1:2,100,replace=TRUE)g4=sample(1:4,100,replace=TRUE)fit1=glmnet(x,y)plot(fit1)plot(fit1,xvar="lambda",label=TRUE)fit3=glmnet(x,g4,family="multinomial")plot(fit3,pch=19)make predictions from a "cv.glmnet" object.
Description
This function makes predictions from a cross-validated glmnet model, usingthe stored"glmnet.fit" object, and the optimal value chosen forlambda (andgamma for a 'relaxed' fit.
Usage
## S3 method for class 'cv.glmnet'predict(object, newx, s = c("lambda.1se", "lambda.min"), ...)## S3 method for class 'cv.relaxed'predict( object, newx, s = c("lambda.1se", "lambda.min"), gamma = c("gamma.1se", "gamma.min"), ...)Arguments
object | Fitted |
newx | Matrix of new values for |
s | Value(s) of the penalty parameter |
... | Not used. Other arguments to predict. |
gamma | Value (single) of 'gamma' at which predictions are to be made |
Details
This function makes it easier to use the results of cross-validation to makea prediction.
Value
The object returned depends on the ... argument which is passedon to thepredict method forglmnet objects.
Author(s)
Jerome Friedman, Trevor Hastie and Rob Tibshirani
Maintainer:Trevor Hastiehastie@stanford.edu
References
Friedman, J., Hastie, T. and Tibshirani, R. (2008)Regularization Paths for Generalized Linear Models via CoordinateDescent (2010), Journal of Statistical Software, Vol. 33(1), 1-22,doi:10.18637/jss.v033.i01.
Simon, N., Friedman, J., Hastie, T. and Tibshirani, R. (2011)Regularization Paths for Cox's ProportionalHazards Model via Coordinate Descent, Journal of Statistical Software, Vol.39(5), 1-13,doi:10.18637/jss.v039.i05.
Hastie, T., Tibshirani, Robert and Tibshirani, Ryan (2020)Best Subset,Forward Stepwise or Lasso? Analysis and Recommendations Based on Extensive Comparisons,Statist. Sc. Vol. 35(4), 579-592,https://arxiv.org/abs/1707.08692.
Glmnet webpage with four vignettes,https://glmnet.stanford.edu.
See Also
glmnet, andprint, andcoef methods, andcv.glmnet.
Examples
x = matrix(rnorm(100 * 20), 100, 20)y = rnorm(100)cv.fit = cv.glmnet(x, y)predict(cv.fit, newx = x[1:5, ])coef(cv.fit)coef(cv.fit, s = "lambda.min")predict(cv.fit, newx = x[1:5, ], s = c(0.001, 0.002))cv.fitr = cv.glmnet(x, y, relax = TRUE)predict(cv.fit, newx = x[1:5, ])coef(cv.fit)coef(cv.fit, s = "lambda.min", gamma = "gamma.min")predict(cv.fit, newx = x[1:5, ], s = c(0.001, 0.002), gamma = "gamma.min")Get predictions from aglmnetfit fit object
Description
Gives fitted values, linear predictors, coefficients and number of non-zerocoefficients from a fittedglmnetfit object.
Usage
## S3 method for class 'glmnetfit'predict( object, newx, s = NULL, type = c("link", "response", "coefficients", "nonzero"), exact = FALSE, newoffset, ...)Arguments
object | Fitted "glmnetfit" object. |
newx | Matrix of new values for |
s | Value(s) of the penalty parameter lambda at which predictions arerequired. Default is the entire sequence used to create the model. |
type | Type of prediction required. Type "link" gives the linearpredictors (eta scale); Type "response" gives the fitted values (mu scale).Type "coefficients" computes the coefficients at the requested values for s.Type "nonzero" returns a list of the indices of the nonzero coefficients foreach value of s. |
exact | This argument is relevant only when predictions are made at valuesof |
newoffset | If an offset is used in the fit, then one must be supplied formaking predictions (except for type="coefficients" or type="nonzero"). |
... | This is the mechanism for passing arguments like |
Value
The object returned depends on type.
print a cross-validated glmnet object
Description
Print a summary of the results of cross-validation for a glmnet model.
Usage
## S3 method for class 'cv.glmnet'print(x, digits = max(3, getOption("digits") - 3), ...)Arguments
x | fitted 'cv.glmnet' object |
digits | significant digits in printout |
... | additional print arguments |
Details
A summary of the cross-validated fit is produced, slightly different for a'cv.relaxed' object than for a 'cv.glmnet' object. Note that a 'cv.relaxed'object inherits from class 'cv.glmnet', so by directly invokingprint.cv.glmnet(object) will print the summary as ifrelax=TRUE had not been used.
Author(s)
Jerome Friedman, Trevor Hastie and Rob Tibshirani
Maintainer:Trevor Hastiehastie@stanford.edu
References
Friedman, J., Hastie, T. and Tibshirani, R. (2008)Regularization Paths for Generalized Linear Models via CoordinateDescent
https://arxiv.org/abs/1707.08692
Hastie, T.,Tibshirani, Robert, Tibshirani, Ryan (2019)Extended Comparisons ofBest Subset Selection, Forward Stepwise Selection, and the Lasso
See Also
glmnet,predict andcoef methods.
Examples
x = matrix(rnorm(100 * 20), 100, 20)y = rnorm(100)fit1 = cv.glmnet(x, y)print(fit1)fit1r = cv.glmnet(x, y, relax = TRUE)print(fit1r)## print.cv.glmnet(fit1r) ## CHECK WITH TREVORprint a glmnet object
Description
Print a summary of the glmnet path at each step along the path.
Usage
## S3 method for class 'glmnet'print(x, digits = max(3, getOption("digits") - 3), ...)Arguments
x | fitted glmnet object |
digits | significant digits in printout |
... | additional print arguments |
Details
The call that produced the objectx is printed, followed by athree-column matrix with columnsDf,%Dev andLambda.TheDf column is the number of nonzero coefficients (Df is areasonable name only for lasso fits).%Dev is the percent devianceexplained (relative to the null deviance). In the case of a 'relaxed' fit,an additional column is inserted,%Dev R which gives the percentdeviance explained by the relaxed model. For a "bigGlm" model, a simplersummary is printed.
Value
The matrix above is silently returned
References
Friedman, J., Hastie, T. and Tibshirani, R. (2008). Regularization Paths for Generalized Linear Models via Coordinate Descent
See Also
glmnet,predict andcoef methods.
Examples
x = matrix(rnorm(100 * 20), 100, 20)y = rnorm(100)fit1 = glmnet(x, y)print(fit1)Make response for coxnet
Description
Internal function to make the response y passed to glmnet suitablefor coxnet (i.e. glmnet with family = "cox"). Sanity checks are performedhere too.
Usage
response.coxnet(y)Arguments
y | Response variable. Either a class "Surv" object or a two-columnmatrix with columns named 'time' and 'status'. |
Details
If y is a class "Surv" object, this function returns y with no changes. Ify is a two-column matrix with columns named 'time' and 'status', it isconverted into a "Surv" object.
Value
A class "Surv" object.
Generate multinomial samples from a probability matrix
Description
Generate multinomial samples
Usage
rmult(p)Arguments
p | matrix of probabilities, with number of columns the number of classes |
Details
Simple function that calls thermultinom function. It generates a class labelfor each row of its input matrix of class probabilities.
Value
a vector of class memberships
Author(s)
Trevor Hastie
Maintainer: Trevor Hastiehastie@stanford.edu
Add strata to a Surv object
Description
Helper function to add strata as an attribute to a Surv object. Theoutput of this function can be used as the response inglmnet()for fitting stratified Cox models.
Usage
stratifySurv(y, strata = rep(1, length(y)))Arguments
y | A Surv object. |
strata | A vector of length equal to the number of observations iny, indicating strata membership. Default is all belong to same strata. |
Details
When fitting a stratified Cox model withglmnet(), strata shouldbe added to aSurv response with this helper function. Note thatit is not sufficient to add strata as an attribute to theSurvresponse manually: if the result does not have classstratifySurv,subsetting of the response will not work properly.
Value
An object of classstratifySurv (in addition to all theclassesy belonged to).
Examples
y <- survival::Surv(1:10, rep(0:1, length.out = 10))strata <- rep(1:3, length.out = 10)y2 <- stratifySurv(y, strata) # returns stratifySurv objectCompute a survival curve from a coxnet object
Description
Computes the predicted survivor function for a Cox proportional hazardsmodel with elastic net penalty.
Usage
## S3 method for class 'coxnet'survfit(formula, s = NULL, ...)Arguments
formula | A class |
s | Value(s) of the penalty parameter lambda at which the survivalcurve is required. Default is the entire sequence used to create the model.However, it is recommended that |
... | This is the mechanism for passing additional arguments like(i) x= and y= for the x and y used to fit the model,(ii) weights= and offset= when the model was fit with these options,(iii) arguments for new data (newx, newoffset, newstrata), and(iv) arguments to be passed to survfit.coxph(). |
Details
To be consistent with other functions inglmnet, ifsis not specified, survival curves are returned for the entire lambdasequence. This is not recommended usage: it is best to callsurvfit.coxnet with a single value of the penalty parameterfor thes option.
Value
Ifs is a single value, an object of class "survfitcox"and "survfit" containing one or more survival curves. Otherwise, a listof such objects, one element for each value ins.Methods defined for survfit objects are print, summary and plot.
Examples
set.seed(2)nobs <- 100; nvars <- 15xvec <- rnorm(nobs * nvars)xvec[sample.int(nobs * nvars, size = 0.4 * nobs * nvars)] <- 0x <- matrix(xvec, nrow = nobs)beta <- rnorm(nvars / 3)fx <- x[, seq(nvars / 3)] %*% beta / 3ty <- rexp(nobs, exp(fx))tcens <- rbinom(n = nobs, prob = 0.3, size = 1)y <- survival::Surv(ty, tcens)fit1 <- glmnet(x, y, family = "cox")# survfit object for Cox model where lambda = 0.1sf1 <- survival::survfit(fit1, s = 0.1, x = x, y = y)plot(sf1)# example with new datasf2 <- survival::survfit(fit1, s = 0.1, x = x, y = y, newx = x[1:3, ])plot(sf2)# example with stratay2 <- stratifySurv(y, rep(1:2, length.out = nobs))fit2 <- glmnet(x, y2, family = "cox")sf3 <- survival::survfit(fit2, s = 0.1, x = x, y = y2)sf4 <- survival::survfit(fit2, s = 0.1, x = x, y = y2, newx = x[1:3, ], newstrata = c(1, 1, 1))Compute a survival curve from a cv.glmnet object
Description
Computes the predicted survivor function for a Cox proportional hazardsmodel with elastic net penalty from a cross-validated glmnet model.
Usage
## S3 method for class 'cv.glmnet'survfit(formula, s = c("lambda.1se", "lambda.min"), ...)Arguments
formula | A class |
s | Value(s) of the penalty parameter lambda at which predictionsare required. Default is the value s="lambda.1se" stored on the CV object.Alternatively s="lambda.min" can be used. If s is numeric, it is takenas the value(s) of lambda to be used. |
... | Other arguments to be passed to |
Details
This function makes it easier to use the results of cross-validationto compute a survival curve.
Value
Ifs is a single value, an object of class "survfitcox"and "survfit" containing one or more survival curves. Otherwise, a listof such objects, one element for each value ins.Methods defined for survfit objects are print, summary and plot.
Examples
set.seed(2)nobs <- 100; nvars <- 15xvec <- rnorm(nobs * nvars)x <- matrix(xvec, nrow = nobs)beta <- rnorm(nvars / 3)fx <- x[, seq(nvars / 3)] %*% beta / 3ty <- rexp(nobs, exp(fx))tcens <- rbinom(n = nobs, prob = 0.3, size = 1)y <- survival::Surv(ty, tcens)cvfit <- cv.glmnet(x, y, family = "cox")# default: s = "lambda.1se"survival::survfit(cvfit, x = x, y = y)# s = "lambda.min"survival::survfit(cvfit, s = "lambda.min", x = x, y = y)Check if glmnet should call cox.path
Description
Helper function to check if glmnet() should call cox.path().
Usage
use.cox.path(x, y)Arguments
x | Design matrix. |
y | Response variable. |
Details
Forfamily="cox", we only call the original coxnet() function if(i) x is not sparse, (ii) y is right-censored data, and (iii) we arenot fitting a stratified Cox model. This function also throws an errorif y has a "strata" attribute but is not of type "stratifySurv".
Value
TRUE if cox.path() should be called, FALSE otherwise.
Helper function to compute weighted mean and standard deviation
Description
Helper function to compute weighted mean and standard deviation.Deals gracefully whether x is sparse matrix or not.
Usage
weighted_mean_sd(x, weights = rep(1, nrow(x)))Arguments
x | Observation matrix. |
weights | Optional weight vector. |
Value
A list with components.
mean | vector of weighted means of columns of x |
sd | vector of weighted standard deviations of columns of x |