forward stepwise regression

痞子三分冷 提交于 2019-12-03 02:16:49

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
> 

You can do it in one step like this

fwd.model = step(lm(y ~ 1, data=my.data.frame), direction='forward', scope=~ x1 + x2 + x3 + ...)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!