[英]How Do I Generate All Possible Combinations of the Variables In A Model In R?
Assuing I have a table with several variables, a
- h
, where h
is the target/y/predicted variable:假设我有一个包含多个变量的表, a
- h
,其中h
是目标/y/预测变量:
a <- rnorm(10,5,1)
b <- rnorm(10,5,1)
c <- rnorm(10,5,1)
d <- rnorm(10,5,1)
e <- rnorm(10,5,1)
f <- rnorm(10,5,1)
g <- rnorm(10,5,1)
h <- rnorm(10,5,1)
df = data.frame(a,b,c,d,e,f,g,h)
I want to run the AIC to determine the best possible model for predicting h
.我想运行AIC以确定最好的 model 来预测h
。 To do that, I need to run every single combination of df[1:7]
.为此,我需要运行df[1:7]
的每一个组合。 So I'd need the AICs of:所以我需要以下 AIC:
lm(fomula= h ~ a+b+c+d+e+f+g)
lm(fomula= h ~ a+b+c+d+e+f)
lm(fomula= h ~ a+b+c+d+e)
As well as every other configuration of the variables.以及变量的所有其他配置。 Is there any way I can do this please?有什么办法可以做到这一点吗?
To get every possible formulation of the variables I've tried:要获得我尝试过的变量的所有可能公式:
library(combinat)
combn(colnames(df[,1:7]))
However, I only got:但是,我只得到:
[1] "a" "b" "c" "d" "e" "f" "g"
As the output of the above code which is a far cry from what I ultimately want.由于上面代码的output与我最终想要的相去甚远。
use the step
function. This should give you the best model:使用step
function。这应该给你最好的 model:
step(lm(h~., df),direction = 'both', trace = 0)
Call:
lm(formula = h ~ b + e + f, data = df)
Coefficients:
(Intercept) b e f
4.3494 -0.8705 -0.3266 1.2877
This model has the lowest AIC
.这个 model 具有最低的AIC
。 You can change trace = 1
, to look at the intermediate models that were run您可以更改trace = 1
以查看运行的中间模型
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.