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 ?
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.