Reputation: 733
I have a dataframe that comprises of a binary outcome column (y
), and multiple independent predictor columns (x1
, x2
, x3
...).
I would like to run many single-variable logistic regression models (e.g. y ~ x1
, y ~ x2
, y ~ x3
), and extract the exponentiated coefficients (odds ratios), 95% confidence intervals and p-values for each model into rows of a dataframe/tibble. It seems to me that a solution should be possible using a combination of purrr and broom.
This question is similar, but I can't work out the next steps of:
Working from the example in the referenced question:
library(tidyverse)
library(broom)
df <- mtcars
df %>%
names() %>%
paste('am~',.) %>%
map(~glm(as.formula(.x), data= df, family = "binomial"))
Upvotes: 2
Views: 1301
Reputation: 733
After sleeping on it, the solution occurred to me. Requires the use of map_df
to run each model, and tidy
to extract the values from each model.
Hopefully this will be useful for others:
library(tidyverse)
library(broom)
df <- mtcars
output <- df %>%
select(-am) %>%
names() %>%
paste('am~',.) %>%
map_df(~tidy(glm(as.formula(.x),
data= df,
family = "binomial"),
conf.int=TRUE,
exponentiate=TRUE)) %>%
filter(term !="(Intercept)")
Upvotes: 4