How can I force cv.glmnet not to drop one specific variable?

蓝咒 提交于 2019-12-20 09:53:57

问题


I am running a regression with 67 observasions and 32 variables. I am doing variable selection using cv.glmnet function from the glmnet package. There is one variable I want to force into the model. (It is dropped during normal procedure.) How can I specify this condition in cv.glmnet?

Thank you!

My code looks like the following:

glmntfit <- cv.glmnet(mydata[,-1], mydata[,1])
coef(glmntfit, s=glmntfit$lambda.1se)

And the variable I want is mydata[,2].


回答1:


This can be achieved by providing a penalty.factor vector, as described in ?glmnet. A penalty factor of 0 indicates that the "variable is always included in the model", while 1 is the default.

glmntfit <- cv.glmnet(mydata[,-1], mydata[, 1], 
                      penalty.factor=c(0, rep(1, ncol(mydata) - 2)))


来源:https://stackoverflow.com/questions/24148805/how-can-i-force-cv-glmnet-not-to-drop-one-specific-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!