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

∥☆過路亽.° 提交于 2020-01-02 08:11:23

问题


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.


回答1:


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)


来源:https://stackoverflow.com/questions/28172614/running-glmnet-package-in-r-getting-error-missing-value-where-true-false-neede

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!