glmnet


> install.packages("glmnet", repos = "http://cran.us.r-project.org")
> library(glmnet)
> data(QuickStartExample)
> fit = glmnet(x, y)
> plot(fit)

Rplot


> coef(fit,s=0.1)

21 x 1 sparse Matrix of class "dgCMatrix"
                       1
(Intercept)  0.150928072
V1           1.320597195
V2           .          
V3           0.675110234
V4           .          
V5          -0.817411518
V6           0.521436671
V7           0.004829335
V8           0.319415917
V9           .          
V10          .          
V11          0.142498519
V12          .          
V13          .          
V14         -1.059978702
V15          .          
V16          .          
V17          .          
V18          .          
V19          .          
V20         -1.021873704

> nx = matrix(rnorm(10*20),10,20)
> predict(fit,newx=nx,s=c(0.1,0.05))
               1           2
 [1,] -5.5637700 -5.83388941
 [2,]  0.1583885 -0.07526616
 [3,]  1.0761634  1.05031528
 [4,]  0.7341100  0.58998483
 [5,] -4.7027890 -4.82573938
 [6,] -2.7505943 -2.87508310
 [7,]  4.6762363  4.70497532
 [8,] -1.7977951 -1.83678975
 [9,]  2.1365770  2.10036218
[10,]  0.6937184  0.87952438
> cvfit = cv.glmnet(x, y)
> plot(cvfit)

Rplot01


> cvfit$lambda.min
[1] 0.06896889
> coef(cvfit, s = "lambda.min")
21 x 1 sparse Matrix of class "dgCMatrix"
                       1
(Intercept)  0.147927056
V1           1.337393911
V2           .          
V3           0.704086480
V4           .          
V5          -0.842853149
V6           0.549403480
V7           0.032703914
V8           0.342430521
V9           .          
V10          0.001206607
V11          0.178995989
V12          .          
V13          .          
V14         -1.079993473
V15          .          
V16          .          
V17          .          
V18          .          
V19          .          
V20         -1.061382443

> predict(cvfit, newx = x[1:5,], s = "lambda.min")
              1
[1,] -1.3626977
[2,]  2.5736774
[3,]  0.5767335
[4,]  2.0062604
[5,]  1.5410061

Logistic Regression


> data(BinomialExample)
> fit = glmnet(x, y, family = "binomial")
> plot(fit, xvar = "dev", label = TRUE)

Rplot02


> predict(fit, newx = x[1:5,], type = "class", s = c(0.05, 0.01))
     1   2  
[1,] "0" "0"
[2,] "1" "1"
[3,] "1" "1"
[4,] "0" "0"
[5,] "1" "1"
> cvfit = cv.glmnet(x, y, family = "binomial", type.measure = "class")
> plot(cvfit)

Rplot03


> cvfit$lambda.min
[1] 0.03741031
> cvfit$lambda.1se
[1] 0.05427596
> coef(cvfit, s = "lambda.min")
31 x 1 sparse Matrix of class "dgCMatrix"
                      1
(Intercept)  0.21531808
V1           .         
V2           0.35204197
V3          -0.25501389
V4          -0.77948728
V5          -0.07772289
V6          -0.54451004
V7           .         
V8          -0.29072606
V9           0.35493516
V10         -0.88664559
V11          .         
V12          .         
V13          .         
V14          .         
V15          .         
V16          0.01632705
V17          .         
V18          .         
V19          .         
V20          .         
V21          .         
V22          0.13882866
V23          0.15552657
V24          .         
V25          0.34738315
V26         -0.20631840
V27          .         
V28          0.05213348
V29         -0.07602940
V30          .         
> predict(cvfit, newx = x[1:10,], s = "lambda.min", type = "class")
      1  
 [1,] "0"
 [2,] "1"
 [3,] "1"
 [4,] "0"
 [5,] "1"
 [6,] "1"
 [7,] "0"
 [8,] "0"
 [9,] "1"
[10,] "1"

Cox Model


> data(CoxExample)
> y[1:5,]
           time status
[1,] 1.76877757      1
[2,] 0.54528404      1
[3,] 0.04485918      0
[4,] 0.85032298      0
[5,] 0.61488426      1
> fit = glmnet(x, y, family = "cox")
> plot(fit)

Rplot04


> coef(fit, s = 0.05)
30 x 1 sparse Matrix of class "dgCMatrix"
              1
V1   0.37693638
V2  -0.09547797
V3  -0.13595972
V4   0.09814146
V5  -0.11437545
V6  -0.38898545
V7   0.24291400
V8   0.03647596
V9   0.34739813
V10  0.03865115
V11  .         
V12  .         
V13  .         
V14  .         
V15  .         
V16  .         
V17  .         
V18  .         
V19  .         
V20  .         
V21  .         
V22  .         
V23  .         
V24  .         
V25  .         
V26  .         
V27  .         
V28  .         
V29  .         
V30  .         
> cvfit = cv.glmnet(x, y, family = "cox")
> plot(cvfit)

Rplot05


> cvfit$lambda.1se
[1] 0.04436439
> coef.min = coef(cvfit, s = "lambda.min")
> active.min = which(coef.min != 0)
> index.min = coef.min[active.min]
> index.min
 [1]  0.496699041 -0.177917576 -0.222445759  0.178507885 -0.190073472 -0.494708144
 [7]  0.339530800  0.093719350  0.454738096  0.119110422  0.019623132 -0.020382328
[13] -0.004743974 -0.003861639 -0.025818950  0.004390694 -0.010499361
> coef.min
30 x 1 sparse Matrix of class "dgCMatrix"
               1
V1   0.496699041
V2  -0.177917576
V3  -0.222445759
V4   0.178507885
V5  -0.190073472
V6  -0.494708144
V7   0.339530800
V8   0.093719350
V9   0.454738096
V10  0.119110422
V11  .          
V12  .          
V13  0.019623132
V14  .          
V15  .          
V16  .          
V17 -0.020382328
V18  .          
V19  .          
V20  .          
V21 -0.004743974
V22 -0.003861639
V23  .          
V24  .          
V25 -0.025818950
V26  .          
V27  0.004390694
V28  .          
V29  .          
V30 -0.010499361