R glmnet as.matrix() 错误信息

28
我正在尝试在数据集上使用glmnet包。 我使用cv.glmnet()获取glmnet()的lambda值。 我排除了1,2,7,12列,因为它们是:id列、响应列、包含NA和包含NA。
以下是数据集和错误消息:
> 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(as.matrix(t2[,-c(1,2,7,12)]), t2[,2], family="binomial")
Error in as.matrix(cbind2(1, newx) %*% nbeta) : 
  error in evaluating the argument 'x' in selecting a method for function 'as.matrix': Error in t(.Call(Csparse_dense_crossprod, y, t(x))) : 
  error in evaluating the argument 'x' in selecting a method for function 't': Error: invalid class 'NA' to dup_mMatrix_as_dgeMatrix
> cv1 <- cv.glmnet(as.matrix(t2[,-c(1,2,7,12)]), t2[,2], family="multinomial")
Error in t(.Call(Csparse_dense_crossprod, y, t(x))) : 
  error in evaluating the argument 'x' in selecting a method for function 't': Error: invalid class 'NA' to dup_mMatrix_as_dgeMatrix

有什么建议吗?


15
我自己解决了。我需要使用data.matrix()而不是as.matrix()。 - screechOwl
我对这个软件包不是很熟悉,但看起来你在方程的两侧都提供了二项式响应... x=t[,c(1,2,7,12)] AND y=t[,2] ... 如果你发现你的模型看起来太好以至于难以置信,那可能就是原因。 - Brandon Bertelsen
不确定是否存在图形错误,但输入向量为x=t [,-c(1,2,7,12)]。 c()前面的“ - ”表示排除这些列并保留其他所有内容,因此响应应仅位于方程的一侧。 - screechOwl
screechOwl,那是一个完全有效的答案,请发布您自己的答案,我会点赞;那个错误信息真的没用。我也遇到了一个分类矩阵的问题。 - smci
在这里推荐我的一个包:glmnetUtils,它可以让你使用公式+数据框的语法来调用glmnet,希望能够解决这类问题。 - Hong Ooi
3个回答

41

由于某种原因,glmnet 更喜欢使用 data.matrix() 而不是 as.matrix()

cv1 <- cv.glmnet(data.matrix(t2[,-c(1,2,7,12)]), t2[,2], family="multinomial")

应该能胜任这份工作。


8

我遇到了同样的错误信息,但对我来说使用 data.matrix() 并不像它看起来那么简单。

这个错误发生在输入矩阵和模型系数的叉积中。

在 predict.glmnet 中:

nfit = as.matrix(cbind2(1, newx) %*% nbeta)

让我解决问题的方法是将 x 强制转换为 dgCMatrix。我真的不明白为什么,但这对我有效。

predict(object = lm, newx =  as(x, "dgCMatrix"), type = "response")

我在许多关于该错误的问题中都没有看到这个答案,因此我想在这里发布它。


1
仅供未来参考,我遇到了同样的问题,使用as(x, "dgCMatrix")与as.vector(classifications)相结合似乎解决了问题。示例代码: cvfit = cv.glmnet(x= as(data, "dgCMatrix"), y = as.vector(classification), family = "binomial") - Peter Maguire

6

当我使用cv.glmnet时,出现了类似的错误信息。在RStudio中,cv.glmnet可以正常使用,但在使用Rscript时却无法工作。对我来说,解决方法是在我的脚本顶部添加:

require(methods)

看起来“glmnet”包可能使用了“methods”包中的某些函数,但是当使用Rscript时,该包未在启动时加载。然而,“methods”包通常会在R中默认加载。

这里是关于这个Rscript功能的信息:

Rscript does not load methods package, R does -- why, and what are the consequences?

我希望这能帮助遇到与我相同问题的人。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接