I have spent whole day today for resolving this.. please help me. Although I just write very simple example here, my original data has a huge number of variables- about 2,000. Therefore, to run regression I need to pick certain variables. I do need to develop many models, so I should do this procedure automatically.
- I run stepwie.
- I don't know how many variables are selected by stepwise.
after selecting variables, I run rolling regression for prediction.
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")# Here is the automatic model I developed..
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")})
I want to calculate prediction from rolling regression..
However, it is very hard to set up data.frame without pre-knowldege about independent variables..
There is a similar one here, but in this model independent variables are known already.