Lasso error in glmnet for y[train]

6.4k views Asked by At

I have a problem with the package glmnet in R. I am trying to use it but I am getting the following problem: {

 > names(mtcars)

#lasso
## 75% of the sample size


 > smp_size <- floor(0.75 * nrow(mtcars))

## set the seed to make your partition reproductible
> set.seed(123)

> train_ind <- sample(seq_len(nrow(mtcars)), size = smp_size)

> train <- mtcars[train_ind, ]

> names(train)

> y<-train["hp"]

> c(y)

> yvector<-c(y)

> is.vector(yvector)

> grid=10^seq(10,-2,length=24)

> lasso.mod=glmnet(train,yvector, alpha=1, lambda=grid)

Error in glmnet(train, yvector, alpha = 1, lambda = grid) : number of observations in y (1) not equal to the number of rows of x (24)

> lasso.mod=glmnet(train,y, alpha=1, lambda=grid)

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

> lasso.mod=glmnet(train,train, alpha=1, lambda=grid)

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

Each time I change the last line (glmnet program) for y (entering matrix or vector as the same length), it repeats the last three errors!!! What should I do?

1

There are 1 answers

0
Hong Ooi On BEST ANSWER

In this line

y <- train["hp"]

The result is a data frame containing one variable (hp). What you want is to extract that variable into a vector:

y <- train[["hp"]]
# or
y <- train$hp

You can also use my glmnetUtils package to handle the mechanics of setting up the response vector and model matrix.

devtools::install_github("hong-revo/glmnetUtils")
library(glmnetUtils)
lasso.mod <- glmnet(hp ~ ., data=train, alpha=1, lambda=grid)