简体   繁体   中英

looping through glm in R

I would like to create a script to automate the glm function for several (n=45) variables. Currently I perform this individually, which is very tedious;

df1
ID  status   B01 B02  B03  B04  B05.......B045
01  0        0   1     1   0    0         0
02  1        1   0     0   0    1         0
03  1        1   1     0   0    0         0
04  0        0   0     0   0    1         1
05  0        1   0     0   0    0         1

ItB01 <- (glm(status ~ B01, data=df1, family = binomial(link = 'logit')))
#for tidy export
library(broom)
tidy_ItB01 <-tidy(ItB01)
tidy_ItB01
write.csv(tidy_ItB01, "ItB01_coef.csv")
#convert to OR and export
library(epiDisplay)
logistic.display(ItB01) -> table2
attributes(table2)
table2$table
ItB01 <- as.data.frame(table2$table)
write.csv(ItB01, file="ItB01_OR.csv")

I repeat this whole process for variables B02 to B45. Any suggestions how to automate the process?

You can build your formulas as strings and then use a loop:

library(broom)
library(epiDisplay)

vars = names(df1)[-(1:2)]
formula_strings = sprintf("status ~ %s", vars)
file_prefix = sprintf("It%s_", vars)
for (i in seq_along(vars)) {
  mod = glm(as.formula(formula_strings[i]), data=df1, family = binomial(link = 'logit'))
  coefs = tidy(mod)
  write.csv(coefs, paste0(file_prefix[i], "coef.csv")

  #convert to OR and export
  OR = as.data.frame(logistic.display(mod)$table)
  write.csv(OR, file = paste0(file_prefix[i], "OR.csv"))
}

You may also want to save the intermediate results in lists. If so, add the following code:

## before the loop, initialize empty lists
coefs = list()
odds_ratios = list()

## inside the loop, assign to the lists
coefs[[vars[i]]] = coefs
odds_ratios[[vars[i]]] = OR

## after the loop, access elements with [[
## e.g., coefs[["B04"]]

You can do the whole thing with mapply like this:

library(broom)
library(epiDisplay)

mapply(function(x, y) {
  mod <- glm(df1$status ~ x, family = binomial(link = 'logit'))
  write.csv(tidy(mod), paste0("It", y, "_coef.csv"))
  write.csv(as.data.frame(logistic.display(mod)$table),  paste0("It", y, "_OR.csv"))
  }, df1[-(1:2)], names(df1[-(1:2)]))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM