我正在使用glmnet的以下代码:
> library(glmnet)
> fit = glmnet(as.matrix(mtcars[-1]), mtcars[,1])
> plot(fit, xvar='lambda')
但是,我想打印出最佳Lambda处的系数,就像岭回归一样。我看到拟合的结构如下:
> str(fit)
List of 12
$ a0 : Named num [1:79] 20.1 21.6 23.2 24.7 26 ...
..- attr(*, "names")= chr [1:79] "s0" "s1" "s2" "s3" ...
$ beta :Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
.. ..@ i : int [1:561] 0 4 0 4 0 4 0 4 0 4 ...
.. ..@ p : int [1:80] 0 0 2 4 6 8 10 12 14 16 ...
.. ..@ Dim : int [1:2] 10 79
.. ..@ Dimnames:List of 2
.. .. ..$ : chr [1:10] "cyl" "disp" "hp" "drat" ...
.. .. ..$ : chr [1:79] "s0" "s1" "s2" "s3" ...
.. ..@ x : num [1:561] -0.0119 -0.4578 -0.1448 -0.7006 -0.2659 ...
.. ..@ factors : list()
$ df : int [1:79] 0 2 2 2 2 2 2 2 2 3 ...
$ dim : int [1:2] 10 79
$ lambda : num [1:79] 5.15 4.69 4.27 3.89 3.55 ...
$ dev.ratio: num [1:79] 0 0.129 0.248 0.347 0.429 ...
$ nulldev : num 1126
$ npasses : int 1226
$ jerr : int 0
$ offset : logi FALSE
$ call : language glmnet(x = as.matrix(mtcars[-1]), y = mtcars[, 1])
$ nobs : int 32
- attr(*, "class")= chr [1:2] "elnet" "glmnet"
但是我无法获得最佳的Lambda和相应的系数。感谢您的帮助。
lambda = 10^seq(from=-10, to=15, by=1/3)
。 - Faris