简体   繁体   中英

How can I perform and store linear regression models between all continuous variables in a data frame?

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 (ie all pairwise comparisons). Any help would be greatly appreciated.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM