14

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:

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

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

But the inputs are the same size:

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

What is causing this error?

MichaelChirico
  • 33,841
  • 14
  • 113
  • 198
Ian Fiddes
  • 2,821
  • 5
  • 29
  • 49

2 Answers2

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.

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

fit <- glmnet(X, y)
justin1.618
  • 691
  • 5
  • 15
0

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

In your example, you could achieve this using:

glmnet(seq.trans, as.vector(rsem.trans))
Richard
  • 56,349
  • 34
  • 180
  • 251