Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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].

like image 760
lareven Avatar asked Jun 10 '14 18:06

lareven


People also ask

What is Alpha in CV Glmnet?

glmnet .) alpha is for the elastic net mixing parameter α, with range α∈[0,1]. α=1 is lasso regression (default) and α=0 is ridge regression.

Does Glmnet standardize variables?

If standardize = F, glmnet doesn't standardize the x , it assumes that is was done prior . Well, guess we both should check the documentation again...

How does CV Glmnet choose Lambda?

By default glmnet chooses the lambda. 1se . It is the largest λ at which the MSE is within one standard error of the minimal MSE. Along the lines of overfitting, this usually reduces overfitting by selecting a simpler model (less non zero terms) but whose error is still close to the model with the least error.

How does CV Glmnet work?

cv. glmnet() performs cross-validation, by default 10-fold which can be adjusted using nfolds. A 10-fold CV will randomly divide your observations into 10 non-overlapping groups/folds of approx equal size. The first fold will be used for validation set and the model is fit on 9 folds.


1 Answers

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)))
like image 74
jbaums Avatar answered Sep 17 '22 00:09

jbaums