title: Lagged Residual as Independent Variable in R
body: <p>I am building a factor model to estimate future equity returns. I'd like to include an autoregressive residual term in this model. I'd like to have yesterday's error (the difference between yesterday's predicted return and actual return) to be included in the regression as an independent variable. What type of autoregressive model is this called? I've searched through various time series econometrics texts and have not found this particular model described. My current solution in R is to rerun the regression at every discrete time step (t), and manually include yesterday's residual, but I am curious if there is a more efficient method or package that does this. </p>
  
  <p>Below is some sample code without the residual term included:</p>
  
  <pre><code>Data:
  # fake data 
  set.seed(333)
  df &lt;- data.frame(seq(as.Date("2017/1/1"), as.Date("2017/2/19"), "days"),
                   matrix(runif(50*506), nrow = 50, ncol = 506))
  
  names(df) &lt;- c("Date", paste0("var", 1:503), c("mktrf", "smb", "hml"))
  
  
  Then I store my necessary variables for regression:
  
  1.All the dep var
  x = df[,505:507]
  
  
  2.All the indep var
  y &lt;- df[,2:504]
  
  
  
  4.Fit all the models
  list_models_AR= lapply(y, function(y) 
         with(x, lm(y ~ mktrf +  smb + hml , na.action = na.exclude)))
  </code></pre>