I have a vector of 4 names called var.names. Using combn(), I obtain all unique combinations of these 4 names (e.g., each alone, unique combinations of 2, 3, and all 4 of them) stored in com.names.
How can I make lm() formulas from each column in com.names with a + sign between those that are more than 1 name?
For example, in com.names[[1]][,1], we have only one name ("gear") so my formula will be mpg ~ gear. BUT, in com.names[[2]][,1], we have two names ("gear" "cyl") so my formula will be mpg ~ gear + cyl and so on (overall, we'll have 15 formulas).
This and This answer may be helpful?
var.names <- c("gear", "cyl", "drat", "disp") # from BASE R 'mtcars' dataset
com.names <- lapply(seq_along(var.names), function(i)combn(var.names, i)) # all combinations
# My incomplete attempt:
lapply(com.names, function(x, d) lm(as.formula("mpg ~ ") )), data = mtcars) # ???