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:
- Get the residuals for the first 20 observations.
- 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?
lapply(rolling_lms, function(x) coef(summary(x))[,"Std. Error"])
– coffeinjunkystr()
. For instance, a lot of information is stored in anlm
object, and additional information is calculated bysummary
. To see what is stored and to get a sense of how to access it, I typically look atstr(object)
. – coffeinjunky