I am trying to estimate a rolling regression of the form: y = a + bxi + e, where xi = 1, 2, ..., n. The examples I came across estimate yi = a + bx + e, instead, where i = 1, 2, ..., n. My aim is to use the first half of the sample to do the in-sample rolling estimation, and the second half for the out-of-sample forecasting. Can anyone help modify this code to achieve my objective? Thanks.
Data
Code: Select all
wfcreate (wf=examplefile) q 1970 2016
group xs
for %i GDP UNEMP INFL CPI M1
series {%i}=2+nrnd
xs.add {%i}
next
series yvar=nrnd
Code to modify
Code: Select all
matrix(46, 5) r2
matrix(46, 5) coef1
matrix(46, 5) coef2
matrix(46, 5) coef3
matrix(46, 5) coef4
matrix(46, 5) coef5
for !k = 1 to 5
statusline rolling series num{!k}
!window = 50
!step = 1
!lenght = @obsrange
equation eq{!k}
!nrolls = @round ((!lenght-!window)/!step)
' matrix (4,!nrolls) coefs{!k}
!rowcounter = 1
!j = 0
for !i = 1 to !lenght - !window +1-!step step !step
!j = !j+1
smpl @first+!i-1 @first+!i + !window - 2
if @obs(num{!k}) > 5 then
eq{!k}.ls num{!k} c xs 'change num{!k} to yvar.
r2(!rowcounter) = eq{!k}. @r2
!rowcounter = !rowcounter+1
'colplace(coefs{!k}, eq{!k}. @coefs, !j)
coef1(!j,!k) = eq{!k}.@coef(1)
coef2(!j,!k) = eq{!k}.@coef(2)
coef3(!j,!k) = eq{!k}.@coef(3)
coef4(!j,!k) = eq{!k}.@coef(4)
coef5(!j,!k) = eq{!k}.@coef(5)
endif
next
next