0
我今天花了整整一天的时间来解决这个问题..请帮助我。 尽管我只是写了一个非常简单的例子,但我的原始数据有大量的变量 - 约2,000。因此,为了运行回归,我需要选择某些变量。 我确实需要开发很多模型,所以我应该自动执行此过程。R代码:滚动回归逐步
- 我运行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")})
我想从滚动回归计算预测..
然而,这是很难成立对于自变量没有预先知道的数据帧。
There is a similar one here, but in this model independent variables are known already.
哦天啊..!有用!!你救了我的生命..我已经花了整整一天的时间..非常感谢你。 – user976856