0

So I am trying to univariate logistic regression analysis on some data I have.

Basically I have a data frame with 1 response variable and 50 predictors.

In order to analyse it I just use the glm function as:

glm(response_var~predictor_var1, data = mydata, family = binomial(link=logit))

However, I don't want to do that manually for all 50 predictors, and it doesn't seem like looping works here. I have tried to say something like this:

predictors <- colnames(mydata)[-c(1)]

glm_list <- list()
i <- 1
for (predictor in predictors) {
    model <- glm(response_var~predictor, data = mydata, family = binomial(link=logit))
    glm_list[[i]] <- model
    i <- i + 1
}

So here I just create a list with the names of the predictors in the data frame through colnames.

But when doing this I just get the error:

variable lengths differ (found for 'predictors')

What am I doing wrong here ?

Denver Dang
  • 2,433
  • 3
  • 38
  • 68
  • 1
    See [here](https://stackoverflow.com/q/25036007/324364), or [here](https://stackoverflow.com/q/46493011/324364), or [here](https://stackoverflow.com/q/41241806/324364) for a variety of different approaches to this problem. – joran Mar 20 '19 at 14:32

1 Answers1

1

Try with lapply and as.formula():

"%+%" <- function(x,y) paste(x, y, sep = "")

lapply(predictors, function(x){
  glm(as.formula("response_var ~ " %+% x), data = mydata, family = binomial(link = logit))
})

You are passing a character vector, and first you must coerce it to formula.

Hope it helps.