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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…