Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'x' and 'w' must have same length - error in weighted.mean.default

Tags:

r

glmnet

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?

like image 552
Ian Fiddes Avatar asked Jan 10 '14 20:01

Ian Fiddes


1 Answers

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)
like image 110
justin1.618 Avatar answered Sep 19 '22 10:09

justin1.618