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

≡放荡痞女 提交于 2019-12-06 01:43:56

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