得到glmnet系数的最佳值

[英]Getting glmnet coefficients at 'best' lambda


I am using following code with glmnet:

我正在用glmnet使用以下代码:

> library(glmnet)> fit = glmnet(as.matrix(mtcars[-1]), mtcars[,1])> plot(fit, xvar='lambda')

enter image description here

However, I want to print out the coefficients at best Lambda, like it is done in ridge regression. I see following structure of fit:

但是,我想打印出最好的系数,就像脊回归那样。我看到了以下的拟合结构:

> 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"

But I am not able to get the best Lambda and the corresponding coefficients. Thanks for your help.

但是我不能得到最好的和相应的系数。谢谢你的帮助。

2 个解决方案

#1


12  

Try this:

试试这个:

fit = glmnet(as.matrix(mtcars[-1]), mtcars[,1],     lambda=cv.glmnet(as.matrix(mtcars[-1]), mtcars[,1])$lambda.1se)coef(fit)

Or you can specify a specify a lambda value in coef:

或者您可以在coef中指定一个指定的lambda值:

fit = glmnet(as.matrix(mtcars[-1]), mtcars[,1])coef(fit, s = cv.glmnet(as.matrix(mtcars[-1]), mtcars[,1])$lambda.1se)

You need to pick a "best" lambda, and lambda.1se is a reasonable, or justifiable, one to pick. But you could use cv.glmnet(as.matrix(mtcars[-1]), mtcars[,1])$lambda.min or any other value of lambda that you settle upon as "best" for you.

你需要选择一个“最好的”,和。1se是一个合理的选择。但是你可以使用cv.glmnet(as.matrix(mtcars[-1]), mtcars[,1])$lambda。最小值或任何其他值,你认为是“最好”的你。

#2


0  

boxcox(){MASS} provides a maximum-likelihood plot showingwhich value of l provides the best fit in a linear model

boxcox(){质量}提供了一个最大似然图,显示l的哪个值最适合线性模型

boxcox(lm.fit) provides the maximum-likelihood plot for awide range of l’s in the linear model

boxcox(lm.fit)提供了线性模型中l的大范围的最大似然图

lm.fit pick the l with thehighest ML value

lm。选择ML值最高的l

boxcox(lm.fit,lambda=seq(-0.1, 0.1, 0.01)) if, forexample, the highest l is around 0.04, get a zoomed in plot aroundthat area

boxcox(lm.fit,lambda=seq(-0.1, 0.1, 0.01))例如,如果最高的l在0.04左右,则在该区域的绘图中放大

In the example, the function provides a plot between l =-0.1 and 0.1 in 0.01 increments.

在本例中,函数以0.01的增量提供l =-0.1和0.1之间的图。

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2015/06/01/d36505cafbed00553494b26dba85aa0c.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告