I have an issue with looping over results of three logistic regression analyses. I am trying to put the exp(b)/odds ratio and their 95% confidence intervals into a data frame, side-by-side. I would like to have the model name (e.g., model 1, model 2, and model 3) in the columns and the variables on the rows. I have been trying to modify code I have found from this site--(I thought I would check here before I would post my question, but nothing is spot on, but if I missed it please point me in that direction). Unfortunately, this appears to be a bit above my knowledge base. This is the code that I am trying to use. It does the base calculations, and creates a tibble with just the coefficients, but I need the exp(b)/odds ratio and the CIs for each model as I said with the model name in the columns, and the variables in the rows.
library(tidyverse)
mydata <- read.csv("https://stats.idre.ucla.edu/stat/data/binary.csv")
forms <- list(model1 = admit ~ gre, model2 = admit ~ gre gpa, model3 = admit ~ gre gpa rank)
map_df(forms, ~coef(glm(.x, mydata, family = "binomial"))) %>%
select(-1)
How would I modify the script to collect the information I need, and in the data frame style my study will need?
CodePudding user response:
broom::tidy()
is very handy for these problems. Try
m1 <- (forms
|> map(glm, data = mydata, family = "binomial")
|> map_dfr(broom::tidy, exponentiate = TRUE, conf.int = TRUE, .id = "model")
)
print(m1)
select()
and pivot_wider()
are useful for converting, e.g.
(m1
|> dplyr::select(model, term, estimate, conf.low, conf.high)
|> pivot_wider(names_from = "term",
values_from = c("estimate", "conf.low", "conf.high"))
)