Suppose we have a dataset with an outcome variable y and 5 covariates. Suppose we want to fit a regression model where y is regressed on each possible combination of covariates. So, since we have 5 covariates we have 5! = 120 regression equations. I've been trying to write a solution to automate this with reformulate()
and update()
:
match_variables <- c("x1", "x2","x3", "x4", "x5")
match_equation <- y ~ x1
matchvar_list <- lapply(match_variables, function(x, orig = match_equation) {
new <- reformulate(c(x,'.'))
update(orig, new)})
matchvar_list
[[1]]
y ~ x1
[[2]]
y ~ x2 + x1
[[3]]
y ~ x3 + x1
[[4]]
y ~ x4 + x1
[[5]]
y ~ x5 + x1
The ultimate goal is to have a list of length 120 where each element is one of the possible combinations of covariates. I'm about 4% of the way there and you can imagine using a brute force approach to close the gap but it seems like there should be a simple modification here that I'm not seeing.
Update###
Actually I made a stupid mistake and the math is wrong. It should be 31 regression equations. y ~ x1 + x2 is the same as y ~ x2 + x1 so we have: choose(5,5) + choose(5,4) + choose(5,3) + choose(5,2) + choose(5,1) = 31