【发布时间】:2013-10-28 06:00:48
【问题描述】:
我今天花了一整天来解决这个问题..请帮助我。 虽然我在这里只写了一个非常简单的例子,但我的原始数据有大量的变量——大约 2000 个。因此,要运行回归,我需要选择某些变量。 我确实需要开发很多模型,所以我应该自动执行此过程。
- 我运行 stepwie。
- 不知道逐步选择了多少变量。
-
选择变量后,我运行滚动回归进行预测。
library(car) library(zoo) # run regression m <- lm(mpg~., data=mtcars) # run stepwise s<-step(m, direction="both") # select variables variable<- attr(s$terms,"term.labels") b<-paste(dep,paste(s, collapse="+"),sep = "~") rollapply(mtcars, width = 2, FUN = function(z) coef(lm(b, data = as.data.frame(z))), by.column = FALSE, align = "right")#这是我开发的自动模型..
models2 <- lapply(1:11, function(x) { dep<-names(mtcars)[x] ind<-mtcars[-x] w<-names(ind) indep<-paste(dep,paste(w, collapse="+"),sep = "~") m<-lm(indep,data=mtcars) s<-step(m, direction="both") b<-paste(dep,paste(s, collapse="+"),sep = "~") rollapply(mtcars, width = 2, FUN = function(z) coef(lm(b, data = as.data.frame(z))), by.column = FALSE, align = "right")})
我想通过滚动回归计算预测..
但是,设置起来非常困难 data.frame 没有关于自变量的预知识..
There is a similar one here, but in this model independent variables are known already.
【问题讨论】:
标签: r