R glmnet :“(列表)对象不能被强制键入“double”; ”
我正在尝试在数据集上使用 glmnet
包。我正在使用 cv.glmnet()
获取 glmnet()
的 lambda 值。这是数据集和错误消息:
> head(t2)
X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12
1 1 1 0.7661266 45 2 0.80298213 9120 13 0 6 0 2
2 2 0 0.9571510 40 0 0.12187620 2600 4 0 0 0 1
3 3 0 0.6581801 38 1 0.08511338 3042 2 1 0 0 0
4 4 0 0.2338098 30 0 0.03604968 3300 5 0 0 0 0
5 5 0 0.9072394 49 1 0.02492570 63588 7 0 1 0 0
6 6 0 0.2131787 74 0 0.37560697 3500 3 0 1 0 1
> str(t2)
'data.frame': 150000 obs. of 12 variables:
$ X1 : int 1 2 3 4 5 6 7 8 9 10 ...
$ X2 : int 1 0 0 0 0 0 0 0 0 0 ...
$ X3 : num 0.766 0.957 0.658 0.234 0.907 ...
$ X4 : int 45 40 38 30 49 74 57 39 27 57 ...
$ X5 : int 2 0 1 0 1 0 0 0 0 0 ...
$ X6 : num 0.803 0.1219 0.0851 0.036 0.0249 ...
$ X7 : int 9120 2600 3042 3300 63588 3500 NA 3500 NA 23684 ...
$ X8 : int 13 4 2 5 7 3 8 8 2 9 ...
$ X9 : int 0 0 1 0 0 0 0 0 0 0 ...
$ X10: int 6 0 0 0 1 1 3 0 0 4 ...
$ X11: int 0 0 0 0 0 0 0 0 0 0 ...
$ X12: int 2 1 0 0 0 1 0 0 NA 2 ...
> cv1 <- cv.glmnet(t2[,-c(1,2,7,12)], t2[,2], family="multinomial")
Error in lognet(x, is.sparse, ix, jx, y, weights, offset, alpha, nobs, :
(list) object cannot be coerced to type 'double'
我排除了第 1、2、7、12 列,因为它们是:id 列、响应列、包含 NA 和包含 NA。任何建议都会很棒。
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 and error message:
> head(t2)
X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12
1 1 1 0.7661266 45 2 0.80298213 9120 13 0 6 0 2
2 2 0 0.9571510 40 0 0.12187620 2600 4 0 0 0 1
3 3 0 0.6581801 38 1 0.08511338 3042 2 1 0 0 0
4 4 0 0.2338098 30 0 0.03604968 3300 5 0 0 0 0
5 5 0 0.9072394 49 1 0.02492570 63588 7 0 1 0 0
6 6 0 0.2131787 74 0 0.37560697 3500 3 0 1 0 1
> str(t2)
'data.frame': 150000 obs. of 12 variables:
$ X1 : int 1 2 3 4 5 6 7 8 9 10 ...
$ X2 : int 1 0 0 0 0 0 0 0 0 0 ...
$ X3 : num 0.766 0.957 0.658 0.234 0.907 ...
$ X4 : int 45 40 38 30 49 74 57 39 27 57 ...
$ X5 : int 2 0 1 0 1 0 0 0 0 0 ...
$ X6 : num 0.803 0.1219 0.0851 0.036 0.0249 ...
$ X7 : int 9120 2600 3042 3300 63588 3500 NA 3500 NA 23684 ...
$ X8 : int 13 4 2 5 7 3 8 8 2 9 ...
$ X9 : int 0 0 1 0 0 0 0 0 0 0 ...
$ X10: int 6 0 0 0 1 1 3 0 0 4 ...
$ X11: int 0 0 0 0 0 0 0 0 0 0 ...
$ X12: int 2 1 0 0 0 1 0 0 NA 2 ...
> cv1 <- cv.glmnet(t2[,-c(1,2,7,12)], t2[,2], family="multinomial")
Error in lognet(x, is.sparse, ix, jx, y, weights, offset, alpha, nobs, :
(list) object cannot be coerced to type 'double'
I'm excluding columns 1,2,7,12 as they are: id column, response column, contain NA's, and contain NA's. Any suggestions would be great.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
cv.glmnet
需要预测变量矩阵,而不是数据框。一般来说,您可以通过以下方式获得此信息,但就您而言,您可能可以更轻松地到达那里,
因为您似乎没有任何因素变量或其他可能使问题复杂化的问题。
由于这个答案得到了大量点击: glmnetUtils 包 提供了一个基于公式的界面glmnet,就像用于大多数 R 建模函数的那样。它包括用于
glmnet
和cv.glmnet
的方法,以及一个新的cva.glmnet
函数,用于对 alpha 和 lambda 进行交叉验证。上面的内容将成为
NA 的自动处理,因此您不必排除缺少值的列。
cv.glmnet
expects a matrix of predictors, not a data frame. Generally you can obtain this viabut in your case, you can probably get there more easily with
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
andcv.glmnet
, as well as a newcva.glmnet
function to do crossvalidation for both alpha and lambda.The above would become
NA's are handled automatically, so you don't have to exclude columns with missing values.