R - Shrinkage Method (Ridge Regression and Lasso)

Card Puncher Data Processing


Unlike subset and forward stepwise regression, which controls the complexity of a model by restricting the number of variables, ridge regression keeps all the variables in and shrinks the coefficients towards zero.


Missing values

There are some missing values here, so before we proceed we will remove them.


The package package=ISLR contains the data set

Formula parameters

As the package package=glmnet does not use the model formula language, we are then require to give it:



Ridge is doing:

  • shrinkage
  • but not variable selection.


The ridge-regression model is fitted by calling the glmnet function with alpha=0 (When alpha equals 1 you fit a lasso model). For alphas in between 0 and 1, you get what's called elastic net models, which are in between ridge and lasso.


Ridge Regression Glmnet Plot

It makes a plot as a function of log of lambda, and is plotting the coefficients. glmnet develops a whole part of models on a grid of values of lambda (about 100 values of lambda).

When log of lambda is 12, all the coefficients are essentially zero. Then as we relax lambda, the coefficients grow away from zero in a nice smooth way, and the sum of squares of the coefficients is getting bigger and bigger until we reach a point where lambda is effectively zero, and the coefficients are unregularized.

Model Selection

Ridge regression gives a whole path of model and we need to pick one. glmnet's got a built-in function, called CV.glmnet that will do cross validation


Cv Glmnet Plot

In the beginning, the mean squared error is very high, and the coefficients are restricted to be too small, and then at some point, it kind of levels off. This seems to indicate that the full model is doing a good job.

There's two vertical lines.

  • The one is at the minimum,
  • and the other vertical line is within one standard error of the minimum. The second line is a slightly more restricted model that does almost as well as the minimum, and sometimes we'll go for that.

At the top of the plot, you actually see how many non-zero variables coefficients are in the model. There's all 20 variables in the model (19 variables plus the intercept) and no coefficient is zero.


lasso is doing:

  • shrinkage
  • and variable selection.


To fit the lasso model, you can specify alpha=1 to the fitting function (or not as it's the default)


Glmnet Lasso Plot


The plot has various choices (see help file). The deviance shows the percentage of deviance explained, (equivalent to r squared in case of regression)


Lasso Deviance Explained Plot

A lot of the r squared was explained for quite heavily shrunk coefficients. And towards the end, with a relatively small increase in r squared from between 0.4 and 0.5, coefficients grow very large. This may be an indication that the end of the path is overfitting.

There's different ways of plotting the coefficients that give us different information about the coefficients and about the nature of the path.

Model Selection

This section shows how to do model selection:

  • with the built-in cross validation procedure
  • or with the help of two sets (training/validation)
Cross validation

Lasso Cv Plot

The fit.lasso will have the whole path of coefficients. which is roughly 100 coefficient vectors dependent on each value, indexed by different values of lambda.

Coefficient Extraction

There's a coefficient function extractor that works on a cross validation object and pick the coefficient vector corresponding to the best model,


The output below has 6 non-zero coefficients which shows that the function has chosen the second vertical second line on the cross-validation plot (within one standard error of the minimum) because cross validation error is measured with some variance.

21 x 1 sparse Matrix of class "dgCMatrix"
(Intercept) 127.95694754
AtBat         .         
Hits          1.42342566
HmRun         .         
Runs          .         
RBI           .         
Walks         1.58214111
Years         .         
CAtBat        .         
CHits         .         
CHmRun        .         
CRuns         0.16027975
CRBI          0.33667715
CWalks        .         
LeagueA       .         
LeagueN       .         
DivisionW    -8.06171262
PutOuts       0.08393604
Assists       .         
Errors        .         
NewLeagueN    .  

Train/validation set
rmse= sqrt(apply((y[-train]-pred)^2,2,mean))

Documentation / References

Share this page:
Follow us:
Task Runner