evt
evt

Reputation: 971

Running glmnet package in R, getting error "missing value where TRUE/FALSE needed", maybe due to missing values?

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.

Upvotes: 5

Views: 9242

Answers (1)

user666993
user666993

Reputation:

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)

Upvotes: 6

Related Questions