Re: Basic Rolling Regression
Posted: Tue Dec 13, 2016 8:50 am
Change the line that does the simple regression to be the lines that do the rolling regression.
Code: Select all
matrix(2,10) francecoefs
matrix(2,10) becoefs
matrix(2,10) ukcoefs
for !i=1 to 10
equation eq1.ls fr{!i} c frmkt
next
colplace(francecoefs, eq1.@coefs,!i)
for !i=1 to 10
equation eq2.ls be{!i} c bemkt
next
colplace(beecoefs, eq2.@coefs,!i)
for !i=1 to 10
equation eq3.ls uk{!i} c ukmkt
next
colplace(ukcoefs, eq3.@coefs,!i)
Code: Select all
svector(230) annee
equation eq
for !i=1 to 230
equation eq.ls y{!i} c x
freeze(mytab) eq.ubreak 15
%text=mytab(10,1)
!dotpos=@instr(%text, "(")
%obstext=@mid(%text, !dotpos+1)
%obstext=@left(%obstext,@length(%obstext)-1)
annee(!i)=%obstext
d mytab
next
matrix(230,1) pvalue
!rowcounter=1
equation eq
for !i=1 to 230
equation eq.ls y{!i} c x
freeze(mytab) eq.ubreak 15
pvalue(!rowcounter,1)= @val(mytab(10,4))
!rowcounter=!rowcounter +1
d mytab
next
Code: Select all
equation eq
for !k=1 to 230
equation eq.ls y{!k} c x
freeze(mytab) eq.ubreak 15
%text=mytab(10,1)
!dotpos=@instr(%text, "(")
%obstext=@mid(%text, !dotpos+1)
%obstext=@left(%obstext,@length(%obstext)-1)
%obstext
d mytab
statusline rolling series y{!k}
!window = 250
!step = 1
!length = @obsrange
equation eq{!k}
!nrolls = @round ((!length-!window)/!step)
!rowcounter = 1
!j= 0
for !i = 1 to %obstext '!length - !window +1-!step step !step
!j = !j+1
smpl @first+!i-1 @first+!i+ !window - 2
eq{!k}.ls y{!k} c x
next
for !t = %obstext to !length - !window +1-!step step !step
!j = !j+1
smpl @first+!t-1 @first+!t+ !window - 2
eq{!k}.ls y{!k} c x
next
next
The Yoyo wrote:For anyone still looking for a code that does a 1-period ahead recursive regression forecast (expanding window), I put together something from posts all over this forum. Most of the credits for these codes go to Esther and Gareth.
Try the code below. Note that I left out the coefficient matrix.Code: Select all
'create some data
create u 100
series y=nrnd
series x1=nrnd
series x2=nrnd
series z=nrnd
'run rolling regression
' set window size
!window = 20
' set step size
!step = 1
' get size of workfile
!length = @obsrange
' declare equation for estimation
equation eq1
'calculate number of rolls
!nrolls = @floor((!length-!window)/!step)
'series to store forecast estimates
series fcast
'*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 - where the equation is y =c(1) + c(2)*x1 +c(3)*x2
eq1.ls y c x1 x2
' 1-period-ahead forecast
%1pers = @otod(@dtoo(%start)+!i+!window-1) 'start point
%1pere = @otod(@dtoo(%start)+!i+!window-1) 'end point
'*EDITED
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*)
eq1.forecast(f=na) yf
' set sampl to obtain the 4th period observation
smpl {%1pere} {%1pere}
' store forecasts
fcast = yf
next
smpl @all
show fcast.line
d(noerr) yf
I have no real programming experience, so keep in mind that the code might not work 100% correctly. Any improvements are appreciated.
' move sample !step obs at a time
for !i = 1 to @first+!i+!window-2
!j=!j+1
Code: Select all
' move sample !step obs at a time
for !i = 1 to !length-!window+1-!step step !step
!j=!j+1
Code: Select all
'create some data
create u 100
series y=nrnd
series x1=nrnd
series x2=nrnd
series z=nrnd
'run rolling regression
' set window size
!window = 36
' set step size
!step = 1
' get size of workfile
!length = @obsrange
' declare equation for estimation
equation eq1
'calculate number of rolls
!nrolls = @floor((!length-!window)/!step)
'create matrix to store coefficients. We'll be running !nrolls regressions (!nrolls rows) for 3 coefficients, so 3 columns.
matrix(!nrolls,3) coefmat
'series to store forecast estimates
series fcast
'*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)+!i-1)
%last = @otod(@dtoo(%start)+!i+!window-2)
smpl {%first} {%last}
' estimate equation - where the equation is y =c(1) + c(2)*x1 +c(3)*x2
eq1.ls y c x1 x2
'store the coefficient vector of this estimation in a matrix-row (transposing it first, for conveniency)
rowplace(coefmat,@transpose(eq1.@coefs),!i)
' 1-period-ahead forecast
%1pers = @otod(@dtoo(%start)+!i+!window-1) 'start point
%1pere = @otod(@dtoo(%start)+!i+!window-1) 'end point
'*EDITED
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*)
eq1.forecast(f=na) yf
' set sampl to obtain the 4th period observation
smpl {%1pere} {%1pere}
' store forecasts
fcast = yf
next
smpl @all
'show fcast.line
d(noerr) yf