Lets say i have 3 response variables A,C and M and i want to fit a model for all possible models ie fit Y ~ A, Y ~ C, Y ~ M, Y ~ A * C, Y ~ A * M, Y ~ C * M, etc. Is there a quick way to do this without manually specifiying the interactions each time?
i do not want to write
M1 = glm(Y ~ A , data = subs, family = "poisson")
M2 = glm(Y ~ C , data = subs, family = "poisson")
M3 = glm(Y ~ M , data = subs, family = "poisson")
M4 = glm(Y ~ A*C , data = subs, family = "poisson")
...
In reality i have more than 3 variables and would like some sort of loop, is this even possible. Thanks
Here is a sort of functional programming approach. You create your data, and as long as your
Y
is the first column, this code would take all the rest of the variables (no matter how many) and construct models on their combinations.Finally, since you've done it in this framework, you can call broom's
tidy
andconfint_tidy
to extract the results into an easy to filter dataset.