“x”和“w”必须有相同的长度误差。

时间:2022-01-16 16:14:53

I am having a problem with the package glmnet in R. I am trying to use it off-the-shelf, and am getting the following problem:

我在r的package glmnet上有一个问题,我正在尝试使用它的现成的,并且正在得到如下的问题:

test <- glmnet(seq.trans,rsem.trans)

Error in weighted.mean.default(y, weights) : 'x' and 'w' must have the same length

加权的误差。默认值(y,权重):“x”和“w”的长度必须相同。

But the inputs are the same size:

但是输入的大小是一样的:

dim(seq.trans)
# [1]    28 17763
dim(rsem.trans)
# [1]    28 17763

What is causing this error?

是什么导致了这个错误?

2 个解决方案

#1


2  

I had the same problem, but found the solution was that both X and y should be matrices. I was running the code below without the as.matrix function and getting the same error. Then I tried this and it worked. Also see the example in this tutorial by loading the data that should come in the package, and you'll see that both x and y in the first example are both matrices.

我有同样的问题,但发现解是X和y都应该是矩阵。我正在运行下面的代码。矩阵函数和得到相同的误差。然后我试了一下,结果成功了。在本教程中,还可以通过加载应该包含在包中的数据来查看示例,您将看到第一个示例中的x和y都是两个矩阵。

library(glmnet)
library(dplyr)
X <- as.matrix(select(mtcars, -mpg))
y <- as.matrix(select(mtcars, mpg))

fit <- glmnet(X, y)

#2


0  

In the context of glmnet(x,y) the variable y should be a vector.

在glmnet(x,y)的上下文中,变量y应该是一个向量。

In your example, you could achieve this using:

在您的示例中,您可以使用:

glmnet(seq.trans, as.vector(rsem.trans))

#1


2  

I had the same problem, but found the solution was that both X and y should be matrices. I was running the code below without the as.matrix function and getting the same error. Then I tried this and it worked. Also see the example in this tutorial by loading the data that should come in the package, and you'll see that both x and y in the first example are both matrices.

我有同样的问题,但发现解是X和y都应该是矩阵。我正在运行下面的代码。矩阵函数和得到相同的误差。然后我试了一下,结果成功了。在本教程中,还可以通过加载应该包含在包中的数据来查看示例,您将看到第一个示例中的x和y都是两个矩阵。

library(glmnet)
library(dplyr)
X <- as.matrix(select(mtcars, -mpg))
y <- as.matrix(select(mtcars, mpg))

fit <- glmnet(X, y)

#2


0  

In the context of glmnet(x,y) the variable y should be a vector.

在glmnet(x,y)的上下文中,变量y应该是一个向量。

In your example, you could achieve this using:

在您的示例中,您可以使用:

glmnet(seq.trans, as.vector(rsem.trans))