简体   繁体   English

如何在数据框中的所有连续变量之间执行和存储线性回归模型?

[英]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).假设我在 R 中使用mtcars ,我想在数字变量的所有可能组合之间执行线性回归,并将它们存储(在列表中,或者如果有更好的数据结构,则可能存储在不同的数据结构中)。 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.我看过类似的帖子,但这种方法只运行 col1 对 col2、col1 对 col3 等的回归。 I want a solution that also runs regressions between col2 and col3 (ie all pairwise comparisons).我想要一个在 col2 和 col3 之间也运行回归的解决方案(即所有成对比较)。 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:假设您需要在mtcars的所有列之间进行成对比较,您可以使用combn() function 查找所有成对比较 (2),并执行所有线性模型:

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. all将是您的列表,其中包含所有线性模型,两个变量之间关联的正向和反向方向。

If you want combinations between three variables, you can do combn(colnames(mtcars), 3) , and so on.如果你想要三个变量之间的组合,你可以做combn(colnames(mtcars), 3)等等。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在数据框中的一列和所有其他列之间执行线性回归并将 r 平方值保存在新数据框中? - How to perform linear regression between one column and all other columns in a data frame and save r squared values in new data frame? 数据框中变量之间的快速成对简单线性回归 - Fast pairwise simple linear regression between variables in a data frame 如何执行线性回归而没有错误? - How can I perform Linear regression without error? 如何通过组合R中的所有变量来修改这些dplyr代码以进行多元线性回归 - How can I modify these dplyr code for multiple linear regression by combination of all variables in R 如何对我的数据执行非线性回归 - How to perform a non linear regression for my data 对同一数据框内的不同因素执行相同的线性回归 - Perform same linear regression on different factors within same data frame 如何使用 dplyr 运行所有子集回归并获得每个线性回归的变量 p 值作为 dataframe? - How can I run all subset regression and get p-values of variables per each linear regression as dataframe using dplyr? Data.frame和不同规格的线性回归模型 - Data.frame and different specifications linear regression models R:对所有变量重复线性回归并将结果保存在新数据框中 - R: repeat linear regression for all variables and save results in a new data frame 如何 plot 具有分类和连续变量的逻辑二项式回归模型? - How to plot logistic binomial regression models with categorical and continuous variables?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM