对于另一个问题,这个程序用于Y在Y上的递归回归,从前20个观察开始,一次增加回归窗口一次,直到它覆盖整个样本,建议:
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 , ]) )
这很好用,但有办法:
因此21.残差是回归中包含21个观测值的那个,22个残差是来自回归的那个,有22个观测值,等等?
答案 0 :(得分:2)
这是一个可能的问题解决方案。
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