### load the data

library(glmnet)
load("QuickStartExample.RData") # Gaussian linear model

class(x) # x and y are two matricies
## [1] "matrix"
class(y)
## [1] "matrix"

### fit the model

fit = glmnet(x, y) 

### visualize the coefficients

Each curve corresponds to each variable. The curve shows the path of the coefficient against the $$\lambda_{1}$$ -norm (regularization parameter) of the whole coefficient vector as $$\lambda$$ varies, so as Lambda approaches zero, the loss function of the model approaches the OLS (ordinary least squares) Therefore, when lambda is very small, the LASSO solution should be very close to the OLS solution, and all of your coefficients are in the model. As lambda grows, the regularization term has greater effect and you will see fewer variables in your model (because more and more coefficients will be zero valued).

http://stats.stackexchange.com/questions/68431/interpretting-lasso-variable-trace-plots

plot(fit, label = TRUE)

### view a summary of fit model

This shows the number of non-zero coefficients (Df), the percent (of null) deviance explained (%Dev), and the value of $$\lambda$$

print(fit)
##
## Call:  glmnet(x = x, y = y)
##
##       Df    %Dev   Lambda
##  [1,]  0 0.00000 1.631000
##  [2,]  2 0.05528 1.486000
##  [3,]  2 0.14590 1.354000
##  [4,]  2 0.22110 1.234000
##  [5,]  2 0.28360 1.124000
##  [6,]  2 0.33540 1.024000
##  [7,]  4 0.39040 0.933200
##  [8,]  5 0.45600 0.850300
##  [9,]  5 0.51540 0.774700
## [10,]  6 0.57350 0.705900
## [11,]  6 0.62550 0.643200
## [12,]  6 0.66870 0.586100
## [13,]  6 0.70460 0.534000
## [14,]  6 0.73440 0.486600
## [15,]  7 0.76210 0.443300
## [16,]  7 0.78570 0.404000
## [17,]  7 0.80530 0.368100
## [18,]  7 0.82150 0.335400
## [19,]  7 0.83500 0.305600
## [20,]  7 0.84620 0.278400
## [21,]  7 0.85550 0.253700
## [22,]  7 0.86330 0.231200
## [23,]  8 0.87060 0.210600
## [24,]  8 0.87690 0.191900
## [25,]  8 0.88210 0.174900
## [26,]  8 0.88650 0.159300
## [27,]  8 0.89010 0.145200
## [28,]  8 0.89310 0.132300
## [29,]  8 0.89560 0.120500
## [30,]  8 0.89760 0.109800
## [31,]  9 0.89940 0.100100
## [32,]  9 0.90100 0.091170
## [33,]  9 0.90230 0.083070
## [34,]  9 0.90340 0.075690
## [35,] 10 0.90430 0.068970
## [36,] 11 0.90530 0.062840
## [37,] 11 0.90620 0.057260
## [38,] 12 0.90700 0.052170
## [39,] 15 0.90780 0.047540
## [40,] 16 0.90860 0.043310
## [41,] 16 0.90930 0.039470
## [42,] 16 0.90980 0.035960
## [43,] 17 0.91030 0.032770
## [44,] 17 0.91070 0.029850
## [45,] 18 0.91110 0.027200
## [46,] 18 0.91140 0.024790
## [47,] 19 0.91170 0.022580
## [48,] 19 0.91200 0.020580
## [49,] 19 0.91220 0.018750
## [50,] 19 0.91240 0.017080
## [51,] 19 0.91250 0.015570
## [52,] 19 0.91260 0.014180
## [53,] 19 0.91270 0.012920
## [54,] 19 0.91280 0.011780
## [55,] 19 0.91290 0.010730
## [56,] 19 0.91290 0.009776
## [57,] 19 0.91300 0.008908
## [58,] 19 0.91300 0.008116
## [59,] 19 0.91310 0.007395
## [60,] 19 0.91310 0.006738
## [61,] 19 0.91310 0.006140
## [62,] 20 0.91310 0.005594
## [63,] 20 0.91310 0.005097
## [64,] 20 0.91310 0.004644
## [65,] 20 0.91320 0.004232
## [66,] 20 0.91320 0.003856
## [67,] 20 0.91320 0.003513
summary(fit)
##           Length Class     Mode
## a0          67   -none-    numeric
## beta      1340   dgCMatrix S4
## df          67   -none-    numeric
## dim          2   -none-    numeric
## lambda      67   -none-    numeric
## dev.ratio   67   -none-    numeric
## nulldev      1   -none-    numeric
## npasses      1   -none-    numeric
## jerr         1   -none-    numeric
## offset       1   -none-    logical
## call         3   -none-    call
## nobs         1   -none-    numeric

### actual coefficients

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

### generate a new dataset and score

nx = matrix(rnorm(10*20), 10, 20)
predict(fit, newx = nx, s = c(0.1, 0.05))
##                1          2
##  [1,]  4.8932309  5.0315623
##  [2,]  0.9607650  1.0054044
##  [3,] -0.6459853 -0.7635143
##  [4,]  4.5198268  4.6249124
##  [5,] -1.2258511 -1.2852343
##  [6,] -5.2791269 -5.4963834
##  [7,] -1.7957678 -1.9934963
##  [8,]  1.3008635  1.2737895
##  [9,]  2.2500902  2.1922471
## [10,] -0.3529606 -0.2302519

### cross validation

cvfit = cv.glmnet(x, y)
plot(cvfit)