0

我有一个回归模型列表,但我想使用 forward 方法逐步运行。我试图用 lapply 修改这个函数,但它并没有真正起作用..

我有数据

test<-data.frame(X1=rnorm(50,mean=50,sd=10),
             X2=rnorm(50,mean=5,sd=1.5),
             X3=rnorm(50,mean=200,sd=25))
test$X1[10]<-5
test$X2[10]<-5
test$X3[10]<-530

我运行回归模型

varlist <- names(test)

models <- lapply(varlist, function(x) {
lm(substitute(i~., list(i = as.name(x))), data = data 
})

然后逐步运行..

lapply(models, function(x){step(x,direction="forward")})

但是,它不会影响我对逐步方法的修改——向前。我如何修改 lapply 上的内置函数?

非常感谢你。

4

1 回答 1

0

For stepwise using 'forward' or 'both', you need to include a scope argument. For you, the scope would be something like the full potential equation. For example on X1: scope='X1~X2+X3'. However, after step 1 of models, you have a list of 3 equations, where each equation uses all the possible variables. Therefore, stepwise cannot possibly add any new variables.

Instead, try "backward". This does not require a scope argument, and it shows that the syntax is correct. I've corrected your code below:

models <- lapply(varlist, function(x) {
  lm(substitute(i~., list(i = as.name(x))), data = test) 
})

And then run stepwise, but backward.

lapply(models, function(x){step(x,direction="backward")})
于 2013-08-23T14:28:52.267 回答