Basic Rolling Regression

For posting your own programs to share with others

Moderators: EViews Gareth, EViews Moderator

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13294
Joined: Tue Sep 16, 2008 5:38 pm

Basic Rolling Regression

Postby EViews Gareth » Fri May 22, 2009 1:45 pm

Below is a very basic rolling regression program. For more complicated/sophisticated approaches, you should look in the ROLL section of the sample programs provided with your copy of EViews.

This one might be a little easier to follow for programming beginners though.

Code: Select all

'create some data
create u 800

series y=nrnd
series x1=nrnd
series x2=nrnd
series z=nrnd

'-------------------------------------------------------------------------------------
'run rolling regression

' set window size
!window = 750
 
' set step size
!step = 40
 
' get size of workfile
!length = @obsrange
 
' declare equation for estimation
equation eq1
 
'calculate number of rolls
!nrolls = @round((!length-!window)/!step)

'matrix to store coefficient estimates
matrix(3,!nrolls)  coefmat ' where 3 is the number of coefficients

'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 to estimation period
   smpl @first+!i-1 @first+!i+!window-2

   ' estimate equation - where the equation is y=c(1) + c(2)*x1 + c(3)*x2
   eq1.ls y c x1 x2

   'store coefficients
   colplace(coefmat,eq1.@coefs,!j)
next

show coefmat

Follow us on Twitter @IHSEViews

Vaal1
Posts: 8
Joined: Wed Jul 29, 2009 6:16 am

Re: Basic Rolling Regression

Postby Vaal1 » Sat Dec 26, 2009 9:42 pm

I would like to generate a rolling regression in a similar manner to this, but instead of storing the coefficients, I would like to store the P-values (Prob.). To do this, I have to change the @coefs part in the line colplace(coefmat,eq1.@coefs,!j), but I am not sure what it should be. I have checked the command reference, but can't find anything for a matrix of Prob. values. Can anyone assist please?

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13294
Joined: Tue Sep 16, 2008 5:38 pm

Re: Basic Rolling Regression

Postby EViews Gareth » Mon Dec 28, 2009 11:01 am

There isn't a data member that holds the p-values. You'll have to calculate them from the t-stats.
Follow us on Twitter @IHSEViews

Vaal1
Posts: 8
Joined: Wed Jul 29, 2009 6:16 am

Re: Basic Rolling Regression

Postby Vaal1 » Thu Feb 04, 2010 7:10 pm

Is there anyway to store the R-square and Adjusted R-square values? Is there a command that can be used to replace the @coefs part in the line colplace(coefmat,eq1.@coefs,!j) to store these R-squares of the rolling regressions?

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13294
Joined: Tue Sep 16, 2008 5:38 pm

Re: Basic Rolling Regression

Postby EViews Gareth » Fri Apr 01, 2011 8:10 am

Make a vector to hold them (rather than a matrix), then inside the loop put something like:

Code: Select all

myr2s(!i) = eq1.@r2


where myr2s is the name of the vector.
Follow us on Twitter @IHSEViews

EViews Esther
EViews Developer
Posts: 149
Joined: Fri Sep 03, 2010 7:57 am

Re: Basic Rolling Regression

Postby EViews Esther » Wed May 25, 2011 9:04 am

Posted a simple program to generate 4-period-ahead forecasts, based on LS equation.

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)

'matrix to store coefficient estimates
matrix(3,!nrolls) coefmat ' where 3 is the number of coefficients

'series to store forecast estimates
series fcast

'redundant series for catching start and end points
series ser = 1   
%start = @otod(@ifirst(ser))   
%end = @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 coefficients
   colplace(coefmat,eq1.@coefs,!j)
   
   ' 4-period-ahead forecast
   %4pers = @otod(@dtoo(%start)+!i+!window-1)      'start point
   %4pere = @otod(@dtoo(%start)+!i+!window+2)   'end point: %4pere - %4pers +1 = 4
   if {%end} < {%4pere} then   'check whether the forecast end point is greater than the workfile end point
      return
   endif
   
   ' set smpl for forecasting period
   smpl {%4pers} {%4pere}   
   
   ' forecast with command *forecast* (see also *fit*)
   eq1.forecast(f=na) yf      
   
   ' set sampl to obtain the 4th period observation
   smpl {%4pere} {%4pere}   
   
   ' store forecasts
   fcast = yf
next

smpl @all
show coefmat
show fcast.line

d(noerr) ser



MehranAdeli
Posts: 1
Joined: Sat Jun 11, 2011 5:07 am

Re: Basic Rolling Regression

Postby MehranAdeli » Sun Jun 12, 2011 5:22 am

Hi,

I used your first code (and changed it accordingly to suit my problem). However I have realised that only the first regression is correct. The second time (when the window is rolled down) it does not exclude the end of the previous window observation, so it follows (say for a window of size 53):

1-53
1-54
1-55

While it should be

1-53
2-54
3-55

Do you know what I'm doing wrong??

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13294
Joined: Tue Sep 16, 2008 5:38 pm

Re: Basic Rolling Regression

Postby EViews Gareth » Sun Jun 12, 2011 10:09 am

Sounds like you aren't changing the start of the sample - only the end of the sample.
Follow us on Twitter @IHSEViews

maxime
Posts: 3
Joined: Mon Jun 27, 2011 11:00 am

Re: Basic Rolling Regression

Postby maxime » Mon Jun 27, 2011 11:13 am

Hello,
I was wondering if it was possible to perform a monthly rolling regression. For example instead of rolling for a certain amount of days the regression would be rolled at the begining of every month, seeing as not all months have the same amount of business days. Thank you.

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13294
Joined: Tue Sep 16, 2008 5:38 pm

Re: Basic Rolling Regression

Postby EViews Gareth » Mon Jun 27, 2011 11:26 am

We'll need more information. What is the frequency of your workfile? Do you want to roll a month at a time (so only one month of data is used), or do you want to roll over each month of the year (so that in the first iteration, all Januarys are used, in the second iteration, all Februarys are used etc....).
Follow us on Twitter @IHSEViews

maxime
Posts: 3
Joined: Mon Jun 27, 2011 11:00 am

Re: Basic Rolling Regression

Postby maxime » Mon Jun 27, 2011 12:25 pm

Hi,
So I am rolling a Garch model with 5yrs of estimation sample (1250 days) and a total sample of 12yrs (3000 days) to forecast violatility for the following month (about 22 days). I would like my estimation period to move up one month at a time which can sometimes be 19, 20, or 22 days depending on the number of business days in the month in question. Basically I would require a changing !step which would reflect the number of days in that month. As for the forecasted period it would have to equal the number of days in the following month.

Here is an attempt at the code...

Code: Select all

' rolling Garch forecast on cretx11703 single daily serie with 5yr estimation sample (1999 to 2003)
'and one month forecasting to be repeated every month until end of total sample
'ttldys is a monthly series with total number of days within month, different tab in wf...

' get size of total sample (12yrs)
smpl @first @last
!length = @obs(cretx11703)'#of days (daily data)
!mlength =@obs(ttldys)'#of months (monthly data)
' set window size
smpl @first 12/31/2003
!window = @obs(cretx11703)'# of days in sample (daily data)
!mwindow=@obs(ttldys)'#of months in sample (monthly data)

' declare equation for estimation
equation eq1

' declare series for final results (forecast)
series cvarf
'monthly increase
!y=0   
!dmonth=@elem(ttdys,!mwindow+!y)                   
' move sample for the 84 months left in sample     
for !i=1 to !length-!window  step !dmonth 'number of days in next month...
!y=!y+1
   ' set sample to estimation period
   smpl @first+!i-1 @first+!window+!i-2
   ' estimate equation
   eq1.arch(1,1,h) cretx11703 c
   ' reset sample to forecast period
   smpl @first+!window+!i-1 @first+!window+!dmonth+!i-2 'forecast !dmonth days ahead, will no longer work after first loop
   ' make forecasts in temporary series first
   eq1.forecast tmp_cvarf
   ' copy data in current forecast sample
   cvarf = tmp_cvarf    
next


Thank you.

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13294
Joined: Tue Sep 16, 2008 5:38 pm

Re: Basic Rolling Regression

Postby EViews Gareth » Mon Jun 27, 2011 1:44 pm

Here's an example that does something similar.

Code: Select all

'create some data.  Daily workfile 1990 to 2000
create d5 1990 2000
series y=nrnd
series x1=nrnd
series x2=nrnd


series year = @year
!minyear = @min(year)  'the first year in the workfile
series monthnum = @month + 12*(year-!minyear)   'create a series containing the month number, running from 1 for the first month in the workfile file, up to the last month in the workfile
!nummonths = @max(monthnum)  'total number of months in the workfile.

vector(!nummonths) R2s    'vector to store r-squareds

for !i=1 to !nummonths   'loop over the months
   smpl if monthnum=!i  'set the sample equal to the current month
   equation eq1.ls y c x1 x2  'estimate
   R2s(!i) = eq1.@r2  'store r-squared
next

smpl @all
Follow us on Twitter @IHSEViews

maxime
Posts: 3
Joined: Mon Jun 27, 2011 11:00 am

Re: Basic Rolling Regression

Postby maxime » Thu Jun 30, 2011 1:03 pm

Perfect, Thanks alot.

oybar
Posts: 2
Joined: Fri Mar 25, 2011 3:03 am

Re: Basic Rolling Regression

Postby oybar » Fri Jul 01, 2011 5:54 am

Hi!
First, I would like to thank the eviewsteam for making this forum such a great place of information
I am working on my master thesis and need some help on the last regression I need for the thesis to be complete.

I have a forecasting model on excess returns :
The model is: r_it – rf_t= c +outputgap(-1) + b(r_mkt – rf_t)
I have named the series y = (r_it – rf_t)
Series x1 = outputgap(-1)
Series x2 = r_mkt – rf_t)

I want to estimate the following regression ri = c + x1 + x2 with either a 60 mnth rolling window or just an expanding window. I have 369 monthly observations, beginning in 1980M01, ending in 2010M10. Every month I want to store the coefficients.
I am using eviews 5, but I have also access to eviews 6.
I have tried this and get following errors after typing enterbutton:
!window = 60
!step = 1
!length = @obsrange
equation eq1
!nrolls = @floor((!length-!window)/!step) Error: !step is not defined
matrix(2,!nrolls) coefmat Error: !nrolls is not defined
!j=0
for !i 0 1 to length-!window+1-!step step !step Error: Flow of control statement executed from the command line
!j=!j+1 Error: !j is not defined
smpl @first+!i-1 @first+!i+!window-2 Error: !i is not defined
eq1.lsn y c x1 x2
Colplace(coefmat,eq1.@coefs,!j) Error: !j is not defined
next Error: Flow of control statement executed from the command line
show coefmat Error: coefmat is not defined


It seems to me as eviews cant store the information when using the command !step = 1 or any other commands. And I am not sure if my code is correct.
I would be very thankful if anyone could help me.
Best,
Oystein

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13294
Joined: Tue Sep 16, 2008 5:38 pm

Re: Basic Rolling Regression

Postby EViews Gareth » Fri Jul 01, 2011 10:24 am

You need to run the code in an EViews program (File->New Program).
Follow us on Twitter @IHSEViews


Return to “Program Repository”

Who is online

Users browsing this forum: No registered users and 6 guests