0

I would like to know how to select the final variables in (cv.glmnet) LASSO by passing the lm command (similar to the link below) but for binary outcome with binary predictors.

Thanks a lot.

Link to obtaining p values of final variables in LASSO with continuous outcome: LASSO Regression - p-values and coefficients

data <- read.csv(file = 'X.csv')
train_df <- na.omit(data)

# Predictors
xfactors<-model.matrix(Outcome ~ ., data=train_df)[,-1]
xfactors

yfactors<- train_df$Outcome
yfactors  

#create dummy variable matrix
x<-as.matrix(data.frame(xfactors)) 
x


CV = cv.glmnet(x, y=yfactors, alpha=1, family = "binomial", 
               type.measure = "class", nlambda = 100, nfolds = 10, intercept=TRUE)

CV = glmnet(x, y=yfactors, alpha = 1, family = "binomial",
             lambda = CV$lambda.min)

W <- as.matrix(coef(CV))
W

keep_X <- rownames(W)[W!=0]
keep_X <- keep_X[!keep_X == "(Intercept)"]
x <- x[,keep_X]

summary(lm(yfactors~x))

#### Getting this error below

Call:
lm(formula = yfactors ~ x)

Residuals:
Error in quantile.default(resid) : (unordered) factors are not allowed
In addition: Warning messages:
1: In model.response(mf, "numeric") :
  using type = "numeric" with a factor response will be ignored
2: In Ops.factor(y, z$residuals) : ‘-’ not meaningful for factors
3: In Ops.factor(r, 2) : ‘^’ not meaningful for factors

User1121
  • 1
  • 1
  • For a binary outcome, use function glm, not function lm. That might solve the issue, but not sure, because the code is not reproducible. (Note that computing p-values after performing variable selection makes the p-values invalid.) – Marjolein Fokkema Feb 26 '21 at 00:48
  • Thanks @MarjoleinFokkema – User1121 Feb 26 '21 at 01:36
  • Sorry, I am unable to upvote the comment, the option isn't available for me. @MarjoleinFokkema – User1121 Mar 20 '21 at 05:15
  • @MarjoleinFokkema, Can you kindly help me with this question at link: (https://stats.stackexchange.com/questions/514762/can-i-run-glm-after-mi-with-elastic-net-non-zeroed-coefficients-from-miselect) – User1121 Mar 20 '21 at 12:27

0 Answers0