library(glmnet)
set.seed(65587)
x <- matrix(rnorm(100), 25, 4)
#x <- as.data.frame(x)
#colnames(x) <- c('x1', 'x2', 'x3', 'x4')
y1 <- 0.9*x[,1] + 0.00*x[,2] + 1.2*x[,3] + 0.3*x[,4]
y2 <- 1.1*x[,1] + 0.02*x[,2] + 0.8*x[,3] + 0.4*x[,4]
y3 <- 0.9*x[,1] + 0.01*x[,2] + 1.3*x[,3] + 0.5*x[,4]
y <- cbind(y1, y2, y3)
mfit = glmnet(x, y, family = "mgaussian")
plot(mfit,type.coef="2norm", label = TRUE)
Extract coefficients at a single value of lambda.
coef(mfit, s=1)
## $y1
## 5 x 1 sparse Matrix of class "dgCMatrix"
## 1
## (Intercept) 0.1288243
## V1 0.5088899
## V2 .
## V3 0.7053134
## V4 .
##
## $y2
## 5 x 1 sparse Matrix of class "dgCMatrix"
## 1
## (Intercept) 0.1443105
## V1 0.5745998
## V2 .
## V3 0.5525302
## V4 .
##
## $y3
## 5 x 1 sparse Matrix of class "dgCMatrix"
## 1
## (Intercept) 0.1301095
## V1 0.5149819
## V2 .
## V3 0.7736209
## V4 .
coef(mfit, s=0.1)
## $y1
## 5 x 1 sparse Matrix of class "dgCMatrix"
## 1
## (Intercept) 0.01255091
## V1 0.86321564
## V2 .
## V3 1.14735011
## V4 0.26789238
##
## $y2
## 5 x 1 sparse Matrix of class "dgCMatrix"
## 1
## (Intercept) 0.01461891
## V1 1.04740569
## V2 .
## V3 0.77978597
## V4 0.35457122
##
## $y3
## 5 x 1 sparse Matrix of class "dgCMatrix"
## 1
## (Intercept) 0.01234107
## V1 0.86660998
## V2 .
## V3 1.24301940
## V4 0.44445080
coef(mfit, s=0.001)
## $y1
## 5 x 1 sparse Matrix of class "dgCMatrix"
## 1
## (Intercept) 0.006957324
## V1 0.879686389
## V2 .
## V3 1.170550691
## V4 0.282269463
##
## $y2
## 5 x 1 sparse Matrix of class "dgCMatrix"
## 1
## (Intercept) 0.00792238
## V1 1.07340039
## V2 .
## V3 0.78606260
## V4 0.37622367
##
## $y3
## 5 x 1 sparse Matrix of class "dgCMatrix"
## 1
## (Intercept) 0.00672596
## V1 0.88298571
## V2 .
## V3 1.26665282
## V4 0.46996604
According to the result, x2 is never chosen. When regulation is large, x4 is also not chosen. x1 and x3 are cosidered to be important.