I\'m trying to use the glmnet
package on a dataset. I\'m using cv.glmnet()
to get a lambda value for glmnet()
. Here\'s the dataset a
cv.glmnet
expects a matrix of predictors, not a data frame. Generally you can obtain this via
X <- model.matrix(, 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.