Let's say I'm using mtcars
in R and I want to perform linear regressions between all possible combinations of numeric variables, and store them (in a list or maybe a different data structure if there's a better one). How would I accomplish that? I've seen similar posts like this one, but this approach only runs regressions with col1 against col2, col1 against col3 ...etc. I want a solution that also runs regressions between col2 and col3 (i.e. all pairwise comparisons). Any help would be greatly appreciated.
Asked
Active
Viewed 247 times
0

gpreising
- 5
- 2
-
Perhaps relevant: https://stackoverflow.com/a/28606996/6851825 – Jon Spring Jan 20 '22 at 23:43
-
1It would help to see what you've tried that hasn't worked – camille Jan 20 '22 at 23:55
-
You may want to consult [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) and [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Dion Groothof Jan 21 '22 at 00:02
-
2The subject asks for all combinations but the question refers to pairwise columns. Did you want to consider all subsets or just all pairs of columns? – G. Grothendieck Jan 21 '22 at 00:08
-
2You could make a list of formulas (perhaps using utils::combn and the paste0/sprintf functions), then use lapply to get back a list of model results. Caret has functions for stepwise regression, if that helps. Not sure what your goal is, but this is not a good modeling approach. – Bill O'Brien Jan 21 '22 at 00:25
1 Answers
2
Assuming you need pairwise comparisons between all columns of mtcars
, you can use combn()
function to find all pairwise comparisons (2), and perform all linear models with:
combinations <- combn(colnames(mtcars), 2)
forward <- list()
reverse <- list()
for(i in 1:ncol(combinations)){
forward[[i]] <- lm(formula(paste0(combinations[,i][1], "~", combinations[,i][2])), data = mtcars)
reverse[[i]] <- lm(formula(paste0(combinations[,i][2], "~", combinations[,i][1])), data = mtcars)
}
all <- c(forward, reverse)
all
will be your list with all of the linear models together, with both forward and reverse directions of associations between the two variables.
If you want combinations between three variables, you can do combn(colnames(mtcars), 3)
, and so on.

Fla28
- 197
- 11