Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in Cross Validation in GLMNET package R for Binomial Target Variable

Tags:

r

glmnet

This is in reference to https://stats.stackexchange.com/questions/72251/an-example-lasso-regression-using-glmnet-for-binary-outcome I am trying to use the Cross Validation in GLMNET (i.e. cv.glmnet) for a binomial target variable. The glmnet works fine but the cv.glmnet throws an error here is the error log:

Error in storage.mode(y) = "double" : invalid to change the storage mode of a factor
In addition: Warning messages:

1: In Ops.factor(x, w) : ‘*’ not meaningful for factors
2: In Ops.factor(y, ybar) : ‘-’ not meaningful for factors

Data Types:

'data.frame':   490 obs. of  13 variables:

$ loan_id          : Factor w/ 614 levels "LP001002","LP001003",..: 190 381 259 310 432 156 179 24 429 408 ...
$ gender           : Factor w/ 2 levels "Female","Male": 2 2 2 2 2 2 2 2 2 1 ...
$ married          : Factor w/ 2 levels "No","Yes": 2 2 2 2 1 2 2 2 2 1 ...
$ dependents       : Factor w/ 4 levels "0","1","2","3+": 1 1 1 3 1 4 2 3 1 1 ...
$ education        : Factor w/ 2 levels "Graduate","Not Graduate": 1 1 1 2 1 1 1 2 1 2 ...     
$ self_employed    : Factor w/ 2 levels "No","Yes": 1 1 1 1 1 1 1 1 1 1 ...
$ applicantincome  : int  9328 3333 14683 7667 6500 39999 3750 3365 2920 2213 ...
$ coapplicantincome: num  0 2500 2100 0 0 ...
$ loanamount       : int  188 128 304 185 105 600 116 112 87 66 ...
$ loan_amount_term : Factor w/ 10 levels "12","36","60",..: 6 9 9 9 9 6 9 9 9 9 ...
$ credit_history   : Factor w/ 2 levels "0","1": 2 2 2 2 2 2 2 2 2 2 ...
$ property_area    : Factor w/ 3 levels "Rural","Semiurban",..: 1 2 1 1 1 2 2 1 1 1 ...
$ loan_status      : Factor w/ 2 levels "0","1": 2 2 1 2 1 2 2 1 2 2 ...

Codes Used:

xfactors<-model.matrix(loan_status ~ gender+married+dependents+education+self_employed+loan_amount_term+credit_history+property_area,data=data_train)[,-1]
x<-as.matrix(data.frame(applicantincome,coapplicantincome,loanamount,xfactors))
glmmod<-glmnet(x,y=as.factor(loan_status),alpha=1,family='binomial')
plot(glmmod,xvar="lambda")
grid()

cv.glmmod <- cv.glmnet(x,y=loan_status,alpha=1) #This Is Where It Throws The Error
like image 861
Anurag H Avatar asked Feb 06 '16 22:02

Anurag H


People also ask

What does CV Glmnet do in R?

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.

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.

How does 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.


1 Answers

The credit for the answer goes to @user20650.

Suspect you need to add the familyto cv.glmnet as well. An example:

x <- model.matrix(am ~ 0 + . , data=mtcars)
cv.glmnet(x, y=factor(mtcars$am), alpha=1)
cv.glmnet(x, y=factor(mtcars$am), alpha=1, family="binomial")
like image 93
Anurag H Avatar answered Sep 19 '22 17:09

Anurag H