ncvreg is an R package for fitting regularization paths for linear regression, GLM, and Cox regression models using lasso or nonconvex penalties, in particular the minimax concave penalty (MCP) and smoothly clipped absolute deviation (SCAD) penalty, with options for additional L2 penalties (the “elastic net” idea). Utilities for carrying out cross-validation as well as post-fitting visualization, summarization, inference, and prediction are also provided.

This site focuses mainly on illustrating the usage and syntax of ncvreg. For more on the algorithms used by ncvreg, see the original article:

For more about the marginal false discovery rate idea used for post-selection inference, see

Installation

ncvreg is on CRAN, so it can be installed via:

Brief introduction

Given a design matrix X and response vector y, we can fit a penalized regression model via:

By default, ncvreg fits a linear regression model with a minimax concave penalty (MCP). For more detail on other types of models available, see here. For more detail on other types of penalties available, see here.

Fitting a penalized regression model produces a path of coefficients, which we can plot with

Notice that variables enter the model one at a time, and that at any given value of \(\lambda\), several coefficients are zero. To see what the coefficients are, we could use the coef function:

The summary method can be used for post-selection inference:

In this case, it would appear that lcavol, svi, and lweight are clearly associated with the response, even after adjusting for the other variables in the model, while lbph, age, and pgg45 may be false positives selected simply by chance.

Typically, one would carry out cross-validation for the purposes of assessing the predictive accuracy of the model at various values of \(\lambda\):

The value of \(\lambda\) that minimizes the cross-validation error is given by cvfit$lambda.min, which in this case is 0.028. Applying coef to the output of cv.ncvreg returns the coefficients at that value of \(\lambda\):

Predicted values can be obtained via predict, which has a number of options:

Note that the original fit (to the full data set) is returned as cvfit$fit; it is not necessary to call both ncvreg and cv.ncvreg to analyze a data set. For example, plot(cvfit$fit) will produce the same coefficient path plot as plot(fit) above.