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

后端 未结 1 568
轻奢々
轻奢々 2020-12-14 06:20

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

相关标签:
1条回答
  • 2020-12-14 06:58

    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.

    0 讨论(0)
提交回复
热议问题