Getting glmnet coefficients at 'best' lambda

前端 未结 3 1838
我寻月下人不归
我寻月下人不归 2021-02-04 03:51

I am using following code with glmnet:

> library(glmnet)
> fit = glmnet(as.matrix(mtcars[-1]), mtcars[,1])
> plot(fit, xvar=\'lambda\')
相关标签:
3条回答
  • 2021-02-04 03:54

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

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

    lm.fit pick the l with the highest ML value

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

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

    0 讨论(0)
  • 2021-02-04 04:08

    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:

    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.

    0 讨论(0)
  • 2021-02-04 04:18

    To extract the optimal lambda, you could type fit$lambda.min

    To obtain the coefficients corresponding to the optimal lambda, use coef(fit, s = fit$lambda.min) - please reference p.6 of the Glmnet vignette.

    I think the coefficients are produced by a model fitted to the full data, not just testing sets, as mentioned in this page.

    0 讨论(0)
提交回复
热议问题