Getting glmnet coefficients at 'best' lambda

前端 未结 3 1961
我寻月下人不归
我寻月下人不归 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 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.

提交回复
热议问题