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 ?

1
  • 1
    See here, or here, or here for a variety of different approaches to this problem. Commented Mar 20, 2019 at 14:32

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.