向前逐步回归 [英] forward stepwise regression
问题描述
在 R 逐步向前回归中,我指定了一个最小模型和一组要添加(或不添加)的变量:
In R stepwise forward regression, I specify a minimal model and a set of variables to add (or not to add):
min.model = lm(y ~ 1)
fwd.model = step(min.model, direction='forward', scope=(~ x1 + x2 + x3 + ...))
有没有办法指定使用矩阵/data.frame 中的所有变量,这样我就不必枚举它们?
Is there any way to specify using all variables in a matrix/data.frame, so I don't have to enumerate them?
示例说明我想做什么,但它们不起作用:
Examples to illustrate what I'd like to do, but they don't work:
# 1
fwd.model = step(min.model, direction='forward', scope=(~ ., data=my.data.frame))
# 2
min.model = lm(y ~ 1, data=my.data.frame)
fwd.model = step(min.model, direction='forward', scope=(~ .))
推荐答案
scope
expects (quoting the help page ?step
)
scope
expects (quoting the help page ?step
)
单个公式或包含的列表组件上"和下",都是公式.见有关如何指定公式及其方式的详细信息用过.
either a single formula, or a list containing components ‘upper’ and ‘lower’, both formulae. See the details for how to specify the formulae and how they are used.
您可以提取并使用~"对应的公式.像这样:
You can extract and use the formula corresponding to "~." like this:
> my.data.frame=data.frame(y=rnorm(20),foo=rnorm(20),bar=rnorm(20),baz=rnorm(20))
> min.model = lm(y ~ 1, data=my.data.frame)
> biggest <- formula(lm(y~.,my.data.frame))
> biggest
y ~ foo + bar + baz
> fwd.model = step(min.model, direction='forward', scope=biggest)
Start: AIC=0.48
y ~ 1
Df Sum of Sq RSS AIC
+ baz 1 2.5178 16.015 -0.44421
<none> 18.533 0.47614
+ foo 1 1.3187 17.214 0.99993
+ bar 1 0.4573 18.075 1.97644
Step: AIC=-0.44
y ~ baz
Df Sum of Sq RSS AIC
<none> 16.015 -0.44421
+ foo 1 0.41200 15.603 1.03454
+ bar 1 0.20599 15.809 1.29688
>
这篇关于向前逐步回归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!