Lasso Regression Example using glmnet package in R

More details please refer to the link below: (https://web.stanford.edu/~hastie/glmnet/glmnet_alpha.html#lin)

This post shows how to use glmnet package to fit lasso regression and how to visualize the output. The description of data is shown in here.

dt <- readRDS(url("https://s3.amazonaws.com/pbreheny-data-sets/whoari.rds"))
attach(dt)
fit <- glmnet(X, y)

Visualize the coefficients

plot(fit)

Label the path

plot(fit, label = TRUE)

The summary table below shows from left to right the number of nonzero coefficients (DF), the percent (of null) deviance explained (%dev) and the value of \(\lambda\) (Lambda).

We can get the actual coefficients at a specific \(\lambda\) whin the range of sequence:

coeffs <- coef(fit, s = 0.1) 
coeffs.dt <- data.frame(name = coeffs@Dimnames[[1]][coeffs@i + 1], coefficient = coeffs@x) 

# reorder the variables in term of coefficients
coeffs.dt[order(coeffs.dt$coefficient, decreasing = T),]
##           name   coefficient
## 1  (Intercept)  3.010501e+00
## 7          hfa  4.634502e-01
## 12         aro  2.139111e-01
## 9          inc  1.298769e-01
## 6       convul  9.284738e-02
## 16         whz  9.018865e-02
## 14         afe  8.749033e-02
## 10         lcw  7.538421e-02
## 15        absu  5.758727e-02
## 13         qcr  2.952464e-02
## 11         str  2.298848e-02
## 3         temp  9.842032e-03
## 5           rr  7.309241e-03
## 8          hfe  4.870858e-03
## 2         wght -3.138053e-05
## 4          age -8.724823e-03

Also, it can allow people to make predictions at specific \(\lambda\) with new input data:

nx = matrix(rnorm(nrow(dt$X)*ncol(dt$X)), nrow = nrow(dt$X), ncol = ncol(dt$X))
pred <- predict(fit, newx = nx, s = c(0.1, 0.05)) 
head(pred, 20)
##              1           2
##  [1,] 2.080845 -0.01705815
##  [2,] 4.160575  3.04018157
##  [3,] 3.160477  1.34054635
##  [4,] 2.593602  0.22584393
##  [5,] 2.658068  0.25195436
##  [6,] 2.908883 -0.04704266
##  [7,] 3.696432  1.45482506
##  [8,] 3.761241  2.25169531
##  [9,] 2.468738  0.29583127
## [10,] 2.939002  1.63358758
## [11,] 3.201428  0.73051237
## [12,] 3.696131  2.03432205
## [13,] 3.223581  0.51639924
## [14,] 2.446100  0.15892742
## [15,] 2.957618  0.11268098
## [16,] 2.691755  0.16644053
## [17,] 3.020437  1.65803981
## [18,] 3.355161  1.86430816
## [19,] 3.476690  1.03333070
## [20,] 4.132615  3.67994484

cv.glmnet is the function to do cross-validation here.

X <- dt$X
y <- dt$y
cv.fit <- cv.glmnet(X, y)

Plotting the object gives the selected \(\lambda\) and corresponding Mean-Square Error.

plot(cv.fit)

We can view the selected \(\lambda\)’s and the corresponding coefficients, For example,

cv.fit$lambda.min
## [1] 0.02751064
cv.fit$lambda.1se
## [1] 0.08401352

lambda.min returns the value of \(\lambda\) that gives minimum mean cross-validated error. The other \(\lambda\) saved is lambda.lse, which gives the most regularized model such that error is within one standard error of the minimum. To use that, we only need to replace lambda.min with lambda.lse above.

# create a function to transform coefficient of glmnet and cvglmnet to data.frame
coeff2dt <- function(fitobject, s) {
  coeffs <- coef(fitobject, s) 
  coeffs.dt <- data.frame(name = coeffs@Dimnames[[1]][coeffs@i + 1], coefficient = coeffs@x) 

  # reorder the variables in term of coefficients
  return(coeffs.dt[order(coeffs.dt$coefficient, decreasing = T),])
}

coeff2dt(fitobject = cv.fit, s = "lambda.min") %>% head(20)
##           name coefficient
## 19         str 0.624028696
## 30         whz 0.552566043
## 10         hfa 0.520301406
## 9       convul 0.367161468
## 24         aro 0.305231265
## 32      puskin 0.246791663
## 12         fde 0.204677903
## 26         afe 0.162886449
## 16         inc 0.142234777
## 34         oab 0.132001766
## 1  (Intercept) 0.126502259
## 11         hfe 0.109667313
## 4         temp 0.084079719
## 18         lcw 0.075124732
## 31        smi2 0.051082663
## 25         qcr 0.048926715
## 17         sr1 0.019747399
## 28         crs 0.014597356
## 20         gru 0.008735240
## 7           rr 0.007203584
coeffs.table <- coeff2dt(fitobject = cv.fit, s = "lambda.min")
ggplot(data = coeffs.table) +
  geom_col(aes(x = name, y = coefficient, fill = {coefficient > 0})) +
  xlab(label = "") +
  ggtitle(expression(paste("Lasso Coefficients with ", lambda, " = 0.0275"))) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1),
        legend.position = "none") 

Elastic net

As an example, we can set \(\alpha=0.2\)

fit2 <- glmnet(X, y, alpha = 0.2, weights = c(rep(1, 716), rep(2, 100)), nlambda = 20)

print(fit2, digits = 3)
## 
## Call:  glmnet(x = X, y = y, weights = c(rep(1, 716), rep(2, 100)), alpha = 0.2,      nlambda = 20) 
## 
##       Df  %Dev   Lambda
##  [1,]  0 0.000 3.250000
##  [2,]  7 0.140 2.000000
##  [3,] 12 0.270 1.230000
##  [4,] 13 0.338 0.759000
##  [5,] 18 0.380 0.467000
##  [6,] 23 0.413 0.288000
##  [7,] 29 0.432 0.177000
##  [8,] 39 0.451 0.109000
##  [9,] 47 0.462 0.067200
## [10,] 52 0.468 0.041400
## [11,] 57 0.472 0.025500
## [12,] 58 0.473 0.015700
## [13,] 60 0.474 0.009670
## [14,] 60 0.474 0.005950
## [15,] 64 0.474 0.003670
## [16,] 65 0.474 0.002260
## [17,] 66 0.474 0.001390
## [18,] 66 0.474 0.000856
## [19,] 66 0.474 0.000527

According to the default internal settings, the computations stop if either the fractional change in deviance down the path is less than \(10^{-5}\) or the fraction of explained deviance reaches 0.999.

plot(fit2, xvar = "lambda", label = TRUE)

# plot against %deviance
plot(fit2, xvar = "dev", label = TRUE)

predict(fit2, newx = X[1:5, ], type = "response", s = 0.03)
##             1
## [1,] 3.170055
## [2,] 4.869413
## [3,] 4.255960
## [4,] 5.018313
## [5,] 2.945014