Search code examples
rglmnetlasso-regressionbinomial-coefficients

Using glmnet on binomial data error


I imported some data as follows

surv <- read.table("http://www.stat.ufl.edu/~aa/glm/data/Student_survey.dat",header = T)
x <- as.matrix(select(surv,-ab))
y <- as.matrix(select(surv,ab))
glmnet::cv.glmnet(x,y,alpha=1,,family="binomial",type.measure = "auc")

and I am getting the following error.

NAs introduced by coercion
 Show Traceback
Error in lognet(x, is.sparse, ix, jx, y, weights, offset, alpha, nobs, : NA/NaN/Inf in foreign function call (arg 5)

What is a good fix for this?


Solution

  • The documentation of the glmnet package has the information that you need,

    surv <- read.table("http://www.stat.ufl.edu/~aa/glm/data/Student_survey.dat", header = T, stringsAsFactors = T)
    
    x <- surv[, -which(colnames(surv) == 'ab')]          # remove the 'ab' column
    
    y <- surv[, 'ab']                                    # the 'binomial' family takes a factor as input (too)
    
    xfact = sapply(1:ncol(x), function(y) is.factor(x[, y]))   # separate the factor from the numeric columns
    
    xfactCols = model.matrix(~.-1, data = x[, xfact])          # one option is to build dummy variables from the factors (the other option is to convert to numeric)
    
    xall = as.matrix(cbind(x[, !xfact], xfactCols))            # cbind() numeric and dummy columns 
    
    fit = glmnet::cv.glmnet(xall,y,alpha=1,family="binomial",type.measure = "auc")       # run glmnet error free
    
    str(fit)
    List of 10
     $ lambda    : num [1:89] 0.222 0.202 0.184 0.168 0.153 ...
     $ cvm       : num [1:89] 1.12 1.11 1.1 1.07 1.04 ...
     $ cvsd      : num [1:89] 0.211 0.212 0.211 0.196 0.183 ...
     $ cvup      : num [1:89] 1.33 1.32 1.31 1.27 1.23 ...
     $ cvlo      : num [1:89] 0.908 0.9 0.89 0.874 0.862 ...
     $ nzero     : Named int [1:89] 0 2 2 3 3 3 4 4 5 6 ...
     .....