R glmnet : “(list) object cannot be coerced to type 'double' ”

梦想与她 提交于 2019-11-28 20:13:06

cv.glmnet expects a matrix of predictors, not a data frame. Generally you can obtain this via

X <- model.matrix(<formula>, data=<data>)

but in your case, you can probably get there more easily with

X <- as.matrix(t2[,-c(1,2,7,12)])

since you don't appear to have any factor variables or other issues that might complicate matters.


Since this answer is getting plenty of hits: the glmnetUtils package provides a formula-based interface to glmnet, like that used for most R modelling functions. It includes methods for glmnet and cv.glmnet, as well as a new cva.glmnet function to do crossvalidation for both alpha and lambda.

The above would become

cv.glmnet(X2 ~ ., data=t2[-1], family="multinomial")

NA's are handled automatically, so you don't have to exclude columns with missing values.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!