简体   繁体   English

通过R中线性回归的两个预测变量组合

[英]By two combinations of predictors in linear regression in R

Suppose that I have X1,...,X14 potential predictors. 假设我有X1,...,X14潜在预测变量。

Now for a given Y i want to make the OLS scheme: 现在,对于给定的Y我想制定OLS方案:

Y~X1+X2
Y~X1+X3
 ....
Y~X1+X14
....
Y~X14+X13

which is basically all the by two combinations of all the predictors. 这基本上是所有预测变量的两个组合。 After all those regressions are created I want to use them in the predict function (if possible). 创建所有这些回归之后,我想在predict函数中使用它们(如果可能)。

My question is: How do i make all those regressions with all by two combinations of the regressors? 我的问题是:如何通过回归变量的两个组合对所有这些回归进行回归?

You can use combn for all the combinations and then use an apply to create all the formulas: 您可以使用combn的全部组合中,然后使用apply创建的所有公式:

#all the combinations
all_comb <- combn(letters, 2)

#create the formulas from the combinations above and paste
text_form <- apply(all_comb, 2, function(x) paste('Y ~', paste0(x, collapse = '+')))

Output 产量

> text_form
  [1] "Y ~ a+b" "Y ~ a+c" "Y ~ a+d" "Y ~ a+e" "Y ~ a+f" "Y ~ a+g".....

Then you can feed the above formulas into your regression using as.formula to convert the texts into formulas (most likely in another apply ). 然后,您可以使用as.formula将上述公式输入到回归中,以将文本转换为公式(最有可能在另一个apply )。

You could also put them into formulas in one line like this: 您也可以将它们放在公式的一行中,如下所示:

mySpecs <- combn(letters[1:3], 2, FUN=function(x) reformulate(x, "Y"),
                 simplify=FALSE)

which returns a list that can be used in lapply to run regressions: 它返回一个可以在lapply用于运行回归的列表:

mySpecs
[[1]]
Y ~ a + b
<environment: 0x4474ca0>

[[2]]
Y ~ a + c
<environment: 0x4477e68>

[[3]]
Y ~ b + c
<environment: 0x447ae38>

You would then do the following to get a list of regression results. 然后,您将执行以下操作以获取回归结果列表。

myRegs <- lapply(mySpecs, function(i) lm(i, data=df))

暂无
暂无

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

相关问题 R中具有多个虚拟编码预测变量的线性回归模型的箱线图 - Boxplot of Linear Regression Model with several Dummy coded predictors in R 将线性回归 model(&gt; 3 个预测变量)中的两条(!)回归线绘制到具有置信区间(sjPlot 或 ggplot)的相同 plot 中 - Plotting two(!) regression lines from a linear regression model ( > 3 predictors) into the same plot with confidence intervals (sjPlot or ggplot) 具有类别预测变量的R中的Logistic回归 - Logistic Regression in R with Categorical Predictors 具有多个预测变量的 R 中的多项式回归 - Polynomial regression in R with multiple predictors 在R中:如何在交互的预测变量上运行多元线性回归,而不对未交互的变量进行回归? - In R: How do I run a multiple linear regression on interacted predictors without regressing on the variables not interacted? 如何在R中进行多因素回归(一般线性模型)而不预先知道预测变量的数量? - How to make a multi-factorial regression (general linear model) in R without knowing in advance the number of predictors? Matlab / R-具有分类和连续预测变量的线性回归-为什么连续预测变量平方? - Matlab/R - linear regression with categorical & continuous predictors - why is the continuous predictor squared? 随着我们逐步添加预测变量,获取线性回归模型的R平方值列表 - Get list of R-squared values for linear regression model as we incrementally add predictors 如何在R中的多元线性回归模型中运行所有可能的组合 - How to run all possible combinations in multiple linear regression model in R R中两个栅格图像之间的线性回归 - Linear regression between two raster images in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM