2017-08-06 5 views
0

J'ai importé des données comme suitEn utilisant glmnet en cas d'erreur de données binomiale

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") 

et je reçois l'erreur suivante.

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) 

Qu'est-ce qu'une bonne solution pour cela?

+0

'select' est probablement un paquet non-base. –

+0

Avez-vous remarqué que la coercition à une matrice a fait toutes ces colonnes numériques en valeurs de caractères? –

+0

@ 42- Je n'ai pas remarqué ça. 'as.numeric' ne semble pas être une bonne solution car cela produit des NA. Que suggérerais-tu? – Alex

Répondre

1

La documentation du paquet glmnet a les informations dont vous avez besoin,

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