If you do not want a explicit loop, you could use map
from the purrr
package. This will do what you want. Below is example code based on your info. Probably not the best code, but should get you started. The glms are stored in the my_glms
list object. More info read the vignettes of purrr
and broom
.
library(purrr)
sample <- data.frame(Y = c(0,0,0,0,1,1,1,1),
x_1 = c(1,2,3,4,5,6,7,8),
x_2 = c(2,3,4,5,6,7,8,9))
my_glms <- map(sample[,-1], #remove Y otherwise glm also on Y
~ glm(Y ~ .x, data = sample, family = binomial))
#using the tidy function from broom
library(broom)
map(my_glms, tidy)
$x_1
term estimate std.error statistic p.value
1 (Intercept) -206.12214 365130.9 -0.0005645158 0.9995496
2 .x 45.80492 80643.9 0.0005679899 0.9995468
$x_2
term estimate std.error statistic p.value
1 (Intercept) -251.92706 445370.6 -0.0005656572 0.9995487
2 .x 45.80492 80643.9 0.0005679899 0.9995468
Of course all this can be done in 1 line of code but this shows the steps and you can adjust where needed.