2
votes

For a different question, this procedure for a recursive regression of X on Y, starting at say the first 20 observations and increasing the regression window by one observation at a time until it covers the full sample, was suggested:

X1 <- runif(50, 0, 1)

X2 <- runif(50, 0, 10) 

Y <- runif(50, 0, 1)

df <- data.frame(X1,X2,Y)


rolling_lms <- lapply( seq(20,nrow(df) ), function(x) lm( Y ~ X1+X2, data = df[1:x , ]) )

This works fine, but is there a way to:

  1. Get the residuals for the first 20 observations.
  2. Adding on the residuals one by one for each regression.

So that the 21. residual is the one from the regression including 21 observations, the 22. residual is the one from the regression with 22 observation and so on?

1
lapply(rolling_lms, function(x) coef(summary(x))[,"Std. Error"])coffeinjunky
In general, it is helpful to look at objects with str(). For instance, a lot of information is stored in an lm object, and additional information is calculated by summary. To see what is stored and to get a sense of how to access it, I typically look at str(object).coffeinjunky

1 Answers

2
votes

Here is a possibile solution for your problem.

set.seed(1)
X1 <- runif(50, 0, 1)
X2 <- runif(50, 0, 10) 
Y <- runif(50, 0, 1)
df <- data.frame(X1,X2,Y)
rolling_lms <- lapply(seq(20,nrow(df)), function(x) lm(Y ~ X1+X2, data = df[1:x , ]))

resk <- function(k) if(k==1) rolling_lms[[k]]$residuals else tail(rolling_lms[[k]]$residuals,1)
unlist(sapply(1:length(rolling_lms), resk))

############
           1            2            3            4            5            6 
 0.051243613 -0.284725835 -0.209235819  0.677747763  0.085196300 -0.077111032 
           7            8            9           10           11           12 
-0.185700617  0.016194254  0.422214060 -0.067994796  0.265315143  0.130531648 
          13           14           15           16           17           18 
-0.083662353 -0.098826853 -0.298235953 -0.459746026  0.282954796 -0.281752756 
          19           20           21           22           23           24 
-0.037180134  0.152774597  0.576060893 -0.121303797  0.001336554 -0.357956306 
          25           26           27           28           29           30 
 0.205847757 -0.111231524 -0.082662882 -0.291013740 -0.223480493  0.051223304 
          31           32           33           34           35           36 
 0.082970698 -0.393398739 -0.428164426  0.122919273  0.457861478  0.148282532 
          37           38           39           40           41           42 
 0.081855106  0.023024731  0.500627476  0.005097244  0.189354101  0.092481013 
          43           44           45           46           47           48 
-0.245542247 -0.217881519  0.234771342 -0.023343600 -0.328489644  0.242163946 
          49           50 
-0.358311100  0.373917319