Lasso error in glmnet NA/NaN/Inf
Asked Answered
G

2

11

I'm having an issue with glmnet in that I keep getting the error message

"Error in elnet(x, is.sparse, ix, jx, y, weights, offset, type.gaussian,  : NA/NaN/Inf in foreign function call (arg 5)
In addition: Warning message:
In elnet(x, is.sparse, ix, jx, y, weights, offset, type.gaussian,  : NAs introduced by coercion"

Below I can replicate the error with the 'iris' data set, but here is the simplified code for my particular data:

vars <- as.matrix(ind.vars)
lasso <- glmnet(vars, y=cup98$TARGET_D, alpha=1)

Here is something you can easily reproduce:

data(iris)
attach(iris)
x <- as.matrix(data.frame(Sepal.Width, Petal.Length, Petal.Width, Species))
y <- Sepal.Length
lasso <- glmnet(x,y=y, alpha=1)

Thanks a lot everybody!

Gory answered 7/2, 2014 at 18:48 Comment(0)
K
16

With as.matrix you are coercing the numeric values to character because you are leaving in "Species":

str(as.matrix(iris[, c('Sepal.Width', 'Petal.Length', 'Petal.Width', 'Species')]))
 chr [1:150, 1:4] "3.5" "3.0" "3.2" "3.1" "3.6" "3.9" ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:4] "Sepal.Width" "Petal.Length" "Petal.Width" "Species"

Also usually a very bad idea to use attach/detach, and if you do not know why you should not use it, then you most definitely should not use it.

data(iris); require(glmnet)
x<-as.matrix(iris[, c('Sepal.Width', 'Petal.Length', 'Petal.Width')])
y<-iris$Sepal.Length
lasso<-glmnet(x,y=y, alpha=1); lasso   # no error
Kif answered 8/2, 2014 at 5:48 Comment(0)
T
0

This error raisea when you try to match two or more unequal rows of the matrix for building a model or something else.

You can solve this problem by removing NA cells from your dataset and check the equality of dimensions. For building a model matrix, this is mandatory to instruct train, and test datasets carefully.

Try the code below:

z <- data[complete.cases(data), ] # For choosing cases without missed items#

I = sample(size =  round(nrow(z)/7,0),x =  1:nrow(z), replace = F) # Sampling from original data to construct test and train sets#

Datatrain = z[I,] #Introducing train set#
Datatest = z[-I,] #Introducing test set#
Thain answered 10/1, 2021 at 18:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.