Chapter 12 Regularization


Chapter Status: Currently this chapter is very sparse. It essentially only expands upon an example discussed in ISL, thus only illustrates usage of the methods. Mathematical and conceptual details of the methods will be added later.

We will use the Hitters dataset from the ISLR package to explore two shrinkage methods: ridge regression and lasso. These are otherwise known as penalized regression methods.

data(Hitters, package = "ISLR")

This dataset has some missing data in the response Salaray. We use the na.omit() function to clean the dataset for ease-of-use.

sum(is.na(Hitters))
## [1] 59
sum(is.na(Hitters$Salary))
## [1] 59
Hitters = na.omit(Hitters)
sum(is.na(Hitters))
## [1] 0

The feature variables are offensive and defensive statistics for a number of baseball players.

names(Hitters)
##  [1] "AtBat"     "Hits"      "HmRun"     "Runs"      "RBI"       "Walks"    
##  [7] "Years"     "CAtBat"    "CHits"     "CHmRun"    "CRuns"     "CRBI"     
## [13] "CWalks"    "League"    "Division"  "PutOuts"   "Assists"   "Errors"   
## [19] "Salary"    "NewLeague"

We use the glmnet() and cv.glmnet() functions from the glmnet package to fit penalized regressions.

library(glmnet)

Unfortunately, the glmnet function does not allow the use of model formulas, so we setup the data for ease of use with glmnet. Eventually we will use train() from caret which does allow for fitting penalized regression with the formula syntax, but to explore some of the details, we first work with the functions from glmnet directly.

X = model.matrix(Salary ~ ., Hitters)[, -1]
y = Hitters$Salary

Note, we’re being lazy and just using the full dataset as the training dataset.

First, we fit an ordinary linear regression, and note the size of the features’ coefficients, and features’ coefficients squared. (The two penalties we will use.)

fit = lm(Salary ~ ., Hitters)
coef(fit)
##  (Intercept)        AtBat         Hits        HmRun         Runs          RBI 
##  163.1035878   -1.9798729    7.5007675    4.3308829   -2.3762100   -1.0449620 
##        Walks        Years       CAtBat        CHits       CHmRun        CRuns 
##    6.2312863   -3.4890543   -0.1713405    0.1339910   -0.1728611    1.4543049 
##         CRBI       CWalks      LeagueN    DivisionW      PutOuts      Assists 
##    0.8077088   -0.8115709   62.5994230 -116.8492456    0.2818925    0.3710692 
##       Errors   NewLeagueN 
##   -3.3607605  -24.7623251
sum(abs(coef(fit)[-1]))
## [1] 238.7295
sum(coef(fit)[-1] ^ 2)
## [1] 18337.3

12.1 Ridge Regression

We first illustrate ridge regression, which can be fit using glmnet() with alpha = 0 and seeks to minimize

\[ \sum_{i=1}^{n} \left( y_i - \beta_0 - \sum_{j=1}^{p} \beta_j x_{ij} \right) ^ 2 + \lambda \sum_{j=1}^{p} \beta_j^2 . \]

Notice that the intercept is not penalized. Also, note that that ridge regression is not scale invariant like the usual unpenalized regression. Thankfully, glmnet() takes care of this internally. It automatically standardizes predictors for fitting, then reports fitted coefficient using the original scale.

The two plots illustrate how much the coefficients are penalized for different values of \(\lambda\). Notice none of the coefficients are forced to be zero.

par(mfrow = c(1, 2))
fit_ridge = glmnet(X, y, alpha = 0)
plot(fit_ridge)
plot(fit_ridge, xvar = "lambda", label = TRUE)

We use cross-validation to select a good \(\lambda\) value. The cv.glmnet()function uses 10 folds by default. The plot illustrates the MSE for the \(\lambda\)s considered. Two lines are drawn. The first is the \(\lambda\) that gives the smallest MSE. The second is the \(\lambda\) that gives an MSE within one standard error of the smallest.

fit_ridge_cv = cv.glmnet(X, y, alpha = 0)
plot(fit_ridge_cv)

The cv.glmnet() function returns several details of the fit for both \(\lambda\) values in the plot. Notice the penalty terms are smaller than the full linear regression. (As we would expect.)

# estimated coefficients, using 1-SE rule lambda, default behavior
coef(fit_ridge_cv)
## 20 x 1 sparse Matrix of class "dgCMatrix"
##                         1
## (Intercept) 226.844379891
## AtBat         0.086613902
## Hits          0.352962515
## HmRun         1.144213851
## Runs          0.569353372
## RBI           0.570074066
## Walks         0.735072618
## Years         2.397356090
## CAtBat        0.007295083
## CHits         0.027995153
## CHmRun        0.208112349
## CRuns         0.056146219
## CRBI          0.058060281
## CWalks        0.056586702
## LeagueN       2.850306094
## DivisionW   -20.329125634
## PutOuts       0.049296951
## Assists       0.007063169
## Errors       -0.128066380
## NewLeagueN    2.654025549
# estimated coefficients, using minimum lambda
coef(fit_ridge_cv, s = "lambda.min")
## 20 x 1 sparse Matrix of class "dgCMatrix"
##                        1
## (Intercept)  11.26303439
## AtBat         0.05681886
## Hits          0.93088191
## HmRun         0.37390139
## Runs          1.09069163
## RBI           0.87757438
## Walks         1.71399148
## Years         0.78041117
## CAtBat        0.01129281
## CHits         0.06105328
## CHmRun        0.42884413
## CRuns         0.12153596
## CRBI          0.12905186
## CWalks        0.04225513
## LeagueN      24.65329547
## DivisionW   -85.67844190
## PutOuts       0.17943174
## Assists       0.03550485
## Errors       -1.58633587
## NewLeagueN    8.37544584
# penalty term using minimum lambda
sum(coef(fit_ridge_cv, s = "lambda.min")[-1] ^ 2)
## [1] 8028.015
# estimated coefficients, using 1-SE rule lambda
coef(fit_ridge_cv, s = "lambda.1se")
## 20 x 1 sparse Matrix of class "dgCMatrix"
##                         1
## (Intercept) 226.844379891
## AtBat         0.086613902
## Hits          0.352962515
## HmRun         1.144213851
## Runs          0.569353372
## RBI           0.570074066
## Walks         0.735072618
## Years         2.397356090
## CAtBat        0.007295083
## CHits         0.027995153
## CHmRun        0.208112349
## CRuns         0.056146219
## CRBI          0.058060281
## CWalks        0.056586702
## LeagueN       2.850306094
## DivisionW   -20.329125634
## PutOuts       0.049296951
## Assists       0.007063169
## Errors       -0.128066380
## NewLeagueN    2.654025549
# penalty term using 1-SE rule lambda
sum(coef(fit_ridge_cv, s = "lambda.1se")[-1] ^ 2)
## [1] 436.8923
# predict using minimum lambda
predict(fit_ridge_cv, X, s = "lambda.min")
# predict using 1-SE rule lambda, default behavior
predict(fit_ridge_cv, X)
# calculate "train error"
mean((y - predict(fit_ridge_cv, X)) ^ 2)
## [1] 134397.5
# CV-MSEs
fit_ridge_cv$cvm
##   [1] 203621.2 202069.4 201387.4 201146.8 200884.0 200596.9 200283.7 199942.0
##   [9] 199569.4 199163.5 198721.6 198240.7 197718.1 197150.4 196534.4 195866.7
##  [17] 195143.8 194362.2 193518.2 192608.3 191628.7 190576.0 189446.9 188238.4
##  [25] 186947.5 185572.0 184110.1 182560.5 180922.8 179197.3 177385.4 175489.5
##  [33] 173513.1 171461.1 169339.5 167155.7 164918.3 162637.0 160322.6 157987.2
##  [41] 155643.4 153304.2 150983.2 148693.7 146449.0 144261.3 142142.5 140103.0
##  [49] 138151.9 136297.0 134543.4 132896.4 131358.8 129931.4 128614.2 127405.6
##  [57] 126302.7 125301.9 124398.8 123588.4 122865.2 122223.8 121658.4 121162.0
##  [65] 120734.8 120366.6 120053.8 119790.8 119573.6 119398.2 119263.5 119161.8
##  [73] 119089.7 119050.5 119032.7 119036.4 119063.4 119104.5 119159.7 119230.6
##  [81] 119307.9 119395.1 119488.5 119587.8 119690.2 119793.1 119897.2 120000.7
##  [89] 120103.0 120201.8 120298.1 120390.9 120478.6 120561.6 120639.9 120713.0
##  [97] 120780.5 120844.2 120902.0 120977.9
# CV-MSE using minimum lambda
fit_ridge_cv$cvm[fit_ridge_cv$lambda == fit_ridge_cv$lambda.min]
## [1] 119032.7
# CV-MSE using 1-SE rule lambda
fit_ridge_cv$cvm[fit_ridge_cv$lambda == fit_ridge_cv$lambda.1se]
## [1] 138151.9

12.2 Lasso

We now illustrate lasso, which can be fit using glmnet() with alpha = 1 and seeks to minimize

\[ \sum_{i=1}^{n} \left( y_i - \beta_0 - \sum_{j=1}^{p} \beta_j x_{ij} \right) ^ 2 + \lambda \sum_{j=1}^{p} |\beta_j| . \]

Like ridge, lasso is not scale invariant.

The two plots illustrate how much the coefficients are penalized for different values of \(\lambda\). Notice some of the coefficients are forced to be zero.

par(mfrow = c(1, 2))
fit_lasso = glmnet(X, y, alpha = 1)
plot(fit_lasso)
plot(fit_lasso, xvar = "lambda", label = TRUE)

Again, to actually pick a \(\lambda\), we will use cross-validation. The plot is similar to the ridge plot. Notice along the top is the number of features in the model. (Which changed in this plot.)

fit_lasso_cv = cv.glmnet(X, y, alpha = 1)
plot(fit_lasso_cv)

cv.glmnet() returns several details of the fit for both \(\lambda\) values in the plot. Notice the penalty terms are again smaller than the full linear regression. (As we would expect.) Some coefficients are 0.

# estimated coefficients, using 1-SE rule lambda, default behavior
coef(fit_lasso_cv)
## 20 x 1 sparse Matrix of class "dgCMatrix"
##                        1
## (Intercept) 193.74263886
## AtBat         .         
## Hits          1.21471320
## HmRun         .         
## Runs          .         
## RBI           .         
## Walks         1.28957901
## Years         .         
## CAtBat        .         
## CHits         .         
## CHmRun        .         
## CRuns         0.12923755
## CRBI          0.31515925
## CWalks        .         
## LeagueN       .         
## DivisionW     .         
## PutOuts       0.02533305
## Assists       .         
## Errors        .         
## NewLeagueN    .
# estimated coefficients, using minimum lambda
coef(fit_lasso_cv, s = "lambda.min")
## 20 x 1 sparse Matrix of class "dgCMatrix"
##                        1
## (Intercept)  129.4155569
## AtBat         -1.6130155
## Hits           5.8058915
## HmRun          .        
## Runs           .        
## RBI            .        
## Walks          4.8469340
## Years         -9.9724045
## CAtBat         .        
## CHits          .        
## CHmRun         0.5374550
## CRuns          0.6811938
## CRBI           0.3903563
## CWalks        -0.5560143
## LeagueN       32.4646094
## DivisionW   -119.3480842
## PutOuts        0.2741895
## Assists        0.1855978
## Errors        -2.1650837
## NewLeagueN     .
# penalty term using minimum lambda
sum(coef(fit_lasso_cv, s = "lambda.min")[-1] ^ 2)
## [1] 15463.18
# estimated coefficients, using 1-SE rule lambda
coef(fit_lasso_cv, s = "lambda.1se")
## 20 x 1 sparse Matrix of class "dgCMatrix"
##                        1
## (Intercept) 193.74263886
## AtBat         .         
## Hits          1.21471320
## HmRun         .         
## Runs          .         
## RBI           .         
## Walks         1.28957901
## Years         .         
## CAtBat        .         
## CHits         .         
## CHmRun        .         
## CRuns         0.12923755
## CRBI          0.31515925
## CWalks        .         
## LeagueN       .         
## DivisionW     .         
## PutOuts       0.02533305
## Assists       .         
## Errors        .         
## NewLeagueN    .
# penalty term using 1-SE rule lambda
sum(coef(fit_lasso_cv, s = "lambda.1se")[-1] ^ 2)
## [1] 3.255212
# predict using minimum lambda
predict(fit_lasso_cv, X, s = "lambda.min")
# predict using 1-SE rule lambda, default behavior
predict(fit_lasso_cv, X)
# calcualte "train error"
mean((y - predict(fit_lasso_cv, X)) ^ 2)
## [1] 127112.4
# CV-MSEs
fit_lasso_cv$cvm
##  [1] 202954.8 193660.9 184303.9 176690.8 169649.2 162259.9 155459.6 149707.1
##  [9] 144914.6 140933.9 137569.5 134525.5 131855.7 129594.0 127336.6 125195.3
## [17] 123403.7 121930.4 120606.0 119446.8 118494.4 117713.1 117074.2 116555.3
## [25] 116137.3 115844.0 115663.4 115556.6 115534.0 115577.1 115638.7 115689.7
## [33] 115722.4 115738.9 115765.4 115901.6 116121.2 116296.6 116163.9 115801.6
## [41] 115447.4 115048.9 114796.4 114591.7 114400.3 114210.5 113935.0 113721.9
## [49] 113566.4 113457.2 113436.9 113483.7 113581.5 113778.6 114015.5 114192.0
## [57] 114400.4 114564.6 114708.6 114818.5 114922.1 115018.1 115140.1 115242.8
## [65] 115360.6 115462.0 115575.5 115664.6 115767.6 115846.7 115951.1 116030.2
## [73] 116117.0 116199.8 116272.6 116364.1 116424.2 116488.7 116582.8 116649.0
# CV-MSE using minimum lambda
fit_lasso_cv$cvm[fit_lasso_cv$lambda == fit_lasso_cv$lambda.min]
## [1] 113436.9
# CV-MSE using 1-SE rule lambda

12.3 broom

Sometimes, the output from glmnet() can be overwhelming. The broom package can help with that.

library(broom)
# the output from the commented line would be immense
# fit_lasso_cv
tidy(fit_lasso_cv)
## # A tibble: 80 x 6
##    lambda estimate std.error conf.low conf.high nzero
##     <dbl>    <dbl>     <dbl>    <dbl>     <dbl> <int>
##  1   255.  202955.    30760.  172195.   233715.     0
##  2   233.  193661.    30072.  163589.   223733.     1
##  3   212.  184304.    28994.  155310.   213298.     2
##  4   193.  176691.    28100.  148591.   204791.     2
##  5   176.  169649.    27329.  142321.   196978.     3
##  6   160.  162260.    26700.  135559.   188960.     4
##  7   146.  155460.    26161.  129298.   181621.     4
##  8   133.  149707.    25723.  123985.   175430.     4
##  9   121.  144915.    25391.  119524.   170305.     4
## 10   111.  140934.    25122.  115812.   166056.     4
## # … with 70 more rows
# the two lambda values of interest
glance(fit_lasso_cv) 
## # A tibble: 1 x 3
##   lambda.min lambda.1se  nobs
##        <dbl>      <dbl> <int>
## 1       2.44       91.7   263

12.4 Simulated Data, \(p > n\)

Aside from simply shrinking coefficients (ridge and lasso) and setting some coefficients to 0 (lasso), penalized regression also has the advantage of being able to handle the \(p > n\) case.

set.seed(1234)
n = 1000
p = 5500
X = replicate(p, rnorm(n = n))
beta = c(1, 1, 1, rep(0, 5497))
z = X %*% beta
prob = exp(z) / (1 + exp(z))
y = as.factor(rbinom(length(z), size = 1, prob = prob))

We first simulate a classification example where \(p > n\).

# glm(y ~ X, family = "binomial")
# will not converge

We then use a lasso penalty to fit penalized logistic regression. This minimizes

\[ \sum_{i=1}^{n} L\left(y_i, \beta_0 + \sum_{j=1}^{p} \beta_j x_{ij}\right) + \lambda \sum_{j=1}^{p} |\beta_j| \]

where \(L\) is the appropriate negative log-likelihood.

library(glmnet)
fit_cv = cv.glmnet(X, y, family = "binomial", alpha = 1)
plot(fit_cv)

We’re being lazy again and using the entire dataset as the training data.

head(coef(fit_cv), n = 10)
## 10 x 1 sparse Matrix of class "dgCMatrix"
##                      1
## (Intercept) 0.02397452
## V1          0.59674958
## V2          0.56251761
## V3          0.60065105
## V4          .         
## V5          .         
## V6          .         
## V7          .         
## V8          .         
## V9          .
fit_cv$nzero
##  s0  s1  s2  s3  s4  s5  s6  s7  s8  s9 s10 s11 s12 s13 s14 s15 s16 s17 s18 s19 
##   0   2   3   3   3   3   3   3   3   3   3   3   3   3   3   3   3   3   3   3 
## s20 s21 s22 s23 s24 s25 s26 s27 s28 s29 s30 s31 s32 s33 s34 s35 s36 s37 s38 s39 
##   3   3   3   3   3   3   3   3   3   3   4   6   7  10  18  24  35  54  65  75 
## s40 s41 s42 s43 s44 s45 s46 s47 s48 s49 s50 s51 s52 s53 s54 s55 s56 s57 s58 s59 
##  86 100 110 129 147 168 187 202 221 241 254 269 283 298 310 324 333 350 364 375 
## s60 s61 s62 s63 s64 s65 s66 s67 s68 s69 s70 s71 s72 s73 s74 s75 s76 s77 s78 s79 
## 387 400 411 429 435 445 453 455 462 466 475 481 487 491 496 498 502 504 512 518 
## s80 s81 s82 s83 s84 s85 s86 s87 s88 s89 s90 s91 s92 s93 s94 s95 s96 s97 s98 s99 
## 523 526 528 536 543 550 559 561 563 566 570 571 576 582 586 590 596 596 600 599

Notice, only the first three predictors generated are truly significant, and that is exactly what the suggested model finds.

fit_1se = glmnet(X, y, family = "binomial", lambda = fit_cv$lambda.1se)
which(as.vector(as.matrix(fit_1se$beta)) != 0)
## [1] 1 2 3

We can also see in the following plots, the three features entering the model well ahead of the irrelevant features.

par(mfrow = c(1, 2))
plot(glmnet(X, y, family = "binomial"))
plot(glmnet(X, y, family = "binomial"), xvar = "lambda")

We can extract the two relevant \(\lambda\) values.

fit_cv$lambda.min
## [1] 0.03718493
fit_cv$lambda.1se
## [1] 0.0514969

TODO: use default of type.measure="deviance" but note that type.measure="class" exists.