'run recursive regression

' set window size
!window = 240

' set step size
!step = 1

' get size of workfile
!length = @obsrange

' declare equation for estimation
equation BARROSO_EQ2 

'calculate number of rolls
!nrolls = @floor((!length-!window)/!step)

'matrix to store coefficient estimates
matrix(2,!nrolls) coefmat_wml ' where 2 is the number of coefficients


'series to store forecast estimates
series fcast_wml

'*EDITED: catching start and end points
%start = "@first" '@otod(@ifirst(ser))   
%end = "@last"   '@otod(@ilast(ser))
   
'variable keeping track of how many rolls we've done
!j=0

' move sample !step obs at a time
for !i = 1  to  !length-!window+1-!step step !step
   !j=!j+1
      
   ' set sample for estimation period         
   %first = @otod(@dtoo(%start))
   %last = @otod(@dtoo(%start)+!i+!window-2)
   smpl {%first} {%last}      

   ' estimate equation 
   BARROSO_EQ2.ls rv_wml c rv_wml(-1)
   
   ' store coefficients
   colplace(coefmat_wml,BARROSO_EQ2.@coefs,!j)


   ' 1-period-ahead forecast
   %1pers = @otod(@dtoo(%start)+!i+!window-1)     'start point
   %1pere = @otod(@dtoo(%start)+!i+!window)      'end point
  if @dtoo(%end) < @dtoo(%1pere) then   'check whether the forecast end point is greater than the workfile end point
      exitloop
   endif
   
   ' set smpl for forecasting period
   smpl {%1pers} {%1pere}   
   
   ' forecast with command *forecast* (see also *fit*)
   BARROSO_EQ2.forecast(f=na) yf      
   
   ' set sampl to obtain the 4th period observation
   smpl {%1pere} {%1pere}  
   
   ' store forecasts
   fcast_wml = yf
next

smpl @all

d(noerr) yf
