18

I am trying to create a model using glmnet, (currently using cv to find the lambda value) and I am getting an error NA/NaN/Inf in foreign function call (arg 5). I believe this has something to do with the NA values in my data set, because when I remove all data points with NAs the command runs successfully.

I was under the impression that glmnet can handle NA values. I'm not sure where the error is coming from:

> res <- cv.glmnet(features.mat, as.factor(tmp[,"outcome"]), family="binomial")
Error in lognet(x, is.sparse, ix, jx, y, weights, offset, alpha, nobs,  : 
  NA/NaN/Inf in foreign function call (arg 5)

The dataset looks something like this:

> head(features.mat)
6 x 8 sparse Matrix of class "dgCMatrix"
   a b   c  e  f  g  h i
1  1 1 138 NA NA 15 NA .
4  1 3 171 NA NA 17 NA .
7  1 1 156 NA NA  5 NA .
8  1 4  97 NA NA  7 NA .
9  1 1 219 NA NA 11 NA .
10 1 . 263 NA NA 20 NA .
> head(as.factor(tmp[,"outcome"]))
[1] 0 0 0 0 0 0
Levels: 0 1
mgoldwasser
  • 14,558
  • 15
  • 79
  • 103

4 Answers4

18

It appears that glmnet cannot handle NA values!

mgoldwasser
  • 14,558
  • 15
  • 79
  • 103
  • This is also the error I've get. How did you solved the problem ? – user2806363 Aug 09 '14 at 16:18
  • 2
    A few different solutions exist: 1) The approach I took was to create a second column col_x_is_na. Where the column is NA, this secondary column gets set to true. After creating this second column, you can set all NA values to 0. The secondary flag column will offset the values in the original column. 2) exclude these columns 3) impute the cells with NA values 4) use a package that can handle NA values, for example, ada – mgoldwasser Aug 11 '14 at 13:31
  • I had the same error having some categorical features (string values in the matrix) – roman-roman Jan 16 '18 at 15:32
  • @citral yes, there is glmnet-specific reason for this error message that is indeed unrelated to NA. The solution is explained here: https://stackoverflow.com/a/48230658/7769076 – Agile Bean Aug 24 '19 at 08:09
18

Addition: In case that you get this error without having NA's in your dataframe, you probably haven't defined your input matrix with the model.matrix function.

x <- model.matrix( ~ ., Data)

I know it is not the answer to your question but i had the same error as you and found this solution. So it might be helpful for others.

PeterD
  • 1,331
  • 12
  • 22
  • 4
    Might not be OP's answer, but surely it solves a really obscure issue. Thanks for the tip! Just to add on top of your comment, use this to avoid having 2 intercepts: ``` x <- model.matrix( ~ -1 + ., Data) ``` – kael Jan 25 '19 at 16:00
1

Chars datatypes that are converted to factors cannot be supported for cv.glmnet, as stated above, can't handle NA's. Either use as.numeric or as.double.

1

Note that this also happens if you include date feature. I have no NA's but got this error. When I removed the date variable eveything worked just fine.

Viðar Ingason
  • 265
  • 3
  • 11