5

I am trying to use glmnet from the glmnet package to run a LASSO regression.

I am using the following command:

library(glmnet)
glmnet(a,b,family="binomial",alpha=1)

And am getting the error:

> Error in if (!all(o)) { : missing value where TRUE/FALSE needed

a is a matrix, with numerical values. b is a vector with a factor as values.

However, b has some missing values. I am suspecting this might be what is causing the error. However, I don't see an option to exclude NAs in the glmnet documentation.

mlegge
  • 6,763
  • 3
  • 40
  • 67
evt
  • 941
  • 2
  • 16
  • 24
  • Could you please add a `dput(a)` and `dput(b)` as well as the `traceback()` after you run your `glmnet` to help us replicate and diagnose? – mlegge Jan 27 '15 at 15:20
  • you have NAs in the class you are trying to predict – marbel Apr 07 '15 at 22:02

1 Answers1

5

Since glmnet doesn't accept the full data frame with a formula (and thus no na.omit), but uses separate response and predictor matrices, you will have to find which values in b are missing, and then subset your predictor matrix to exclude those rows.

library(glmnet)

set.seed(123)
a <- matrix(rnorm(100*20),100,20)
b <- as.factor(sample(0:1,100,replace = TRUE))

b[10] <- NA

na_index <- is.na(b)
res <- glmnet(a[!na_index, ], b[!na_index], family = "binomial", alpha = 1)