ComputerNoob
ComputerNoob

Reputation: 518

Looping GLM model and Printing Results

I have a data set that I want to run a number of univariate regressions with 20 variables or so. Listed below is a truncated data frame showing just two of the variables of interest (Age and Anesthesia). The regression works fine but the issue I am running into is how to store the data. Since Age has only coefficient but Anesthesia has >4 and the error I get is:

Error in results[i, i] <- summary.glm(logistic.model)$coefficients : 
  number of items to replace is not a multiple of replacement length

Here is the truncated data.

COMPLICATION  Age     Anesthesia
0              45      General
1              23      Local
1              33      Lumbar
0              21      Other



 varlist <- c("Age", “Anesthesia")
 univars <- data.frame()  # create an empty data frame
    
    for (i in seq_along(varlist))
    {
  
  mod <- as.formula(sprintf("COMPLICATION ~ %s", varlist[i]))
  
  logistic.model<- glm(formula = mod, family = binomial, data=data.clean)
  
 results[i,i] <-summary.glm(logistic.model)$coefficients
  
}

Upvotes: 0

Views: 421

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388962

Filling the dataframe in a loop is not a good idea and it can be inefficient. Moreover, summary.glm(logistic.model)$coefficients returns more than 1 row hence you get the error. Try using this lapply approach.

varlist <- c("Age", "Anesthesia")

lapply(varlist, function(x) {
  mod <- glm(reformulate(x, 'COMPLICATION'), data.clean, family = binomial)
  summary.glm(mod)$coefficients
}) -> result

result

If you want to combine the result in one dataframe you can do :

result <- do.call(rbind, result)

Upvotes: 2

Related Questions