0
votes

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.

  1. I run stepwie.
  2. I don't know how many variables are selected by stepwise.
  3. 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.

1

1 Answers

0
votes

You do not need to know the independent variables! If you provide a data.frame that contains all variables, the predict function will select the necessary ones. Similar to the post you have linked, you could to this:

mtcars[,"int"] <- seq(nrow(mtcars)) # add variable used to choose newdata
models2 <- lapply(1:11, function(x) {
  dep <- names(mtcars)[x]
  ind <- mtcars[-x]
  w <- names(ind)
  form <- paste(dep,paste(w, collapse="+"),sep = "~")
  m <- lm(form, data=mtcars)
  s <- step(m, direction="both", trace=0) # model selection (don't print trace)
  b <- formula(s) # This is clearer than your version
  rpl <- rollapply(mtcars, width = 20, # if you use width=2, your model will always be overdetermined
          FUN = function(z) {
            nextD <- max(z[,'int'])+1 # index of row for new data
            fit <- lm(b, data = as.data.frame(z)) # fit the model 
            c(coef=coef(fit), # coefficients
              predicted=predict(fit, newdata=mtcars[nextD,])) # predict using the next row
          },
          by.column = FALSE, align = "right")
  rpl
})