2017-08-06 70 views
0

使用glmnet我進口了一些數據如下二項分佈數據錯誤

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

,我收到以下錯誤。

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) 

對此有什麼好的解決方法?

+0

'select'可能是從非基本包。 –

+0

您是否注意到強制轉換爲矩陣將所有這些數字列轉換爲字符值? –

+0

@ 42-我沒有注意到。 'as.numeric'似乎不是一個很好的解決方案,因爲它產生了NAs。你會建議什麼? – Alex

回答

1

的glmnet包的文檔中有您所需要的信息,

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