Basic Rolling Regression

For posting your own programs to share with others

Moderators: EViews Gareth, EViews Moderator

Charles_gre
Posts: 7
Joined: Sat Jan 21, 2017 3:30 am

Re: Basic Rolling Regression

Postby Charles_gre » Mon Apr 03, 2017 7:09 am

Thank you for your help. I used the roll add-in and I managed to get the desired output.

I do not know if you can help me please.

I am trying to extract the s.e of the regression from a number of rolling regressions. I have looked at the commands and programs in the help manual. However, I did not manage the code to extract the s.e of the rolling regression like the one shown hereunder:

myroll.extractresidstat(stat=r2s) roll_r2

I am understanding that the above code extracts the R-squared of the residuals of a rolling regression. In my case, I want the s.e of the regression and not the R-squared, what shall I write instead of r2s? I do have a lot of rolling regressions, therefore knowing this would make the process a lot easier.

As indicated in my previous post this is urgent as I need this for my thesis. I appreciate all the help which you give me.

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 Apr 03, 2017 8:43 am

The add-in doesn't support saving the ses. But it should be straight forward for you to edit the add-in code to do it.
Follow us on Twitter @IHSEViews

Charles_gre
Posts: 7
Joined: Sat Jan 21, 2017 3:30 am

Re: Basic Rolling Regression

Postby Charles_gre » Mon Apr 03, 2017 9:45 am

EViews Gareth wrote:The add-in doesn't support saving the ses. But it should be straight forward for you to edit the add-in code to do it.

Dear Gareth, Thank you for your answer, please note that I am a beginner here and I do not know how to do this.

How am I going to produce a code so that Eviews can extract the s.e of the regression? I am assuming that it is similar to this one:

logmode +addins
include .\globals.prg

%name = "roll_r2"
%stat = ""
!result = 1
call r2diag(!result, %name,%stat)
if !result = -1 then
stop
endif

call r2extract(%name, %stat)

which is the code for the r2.

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 Apr 03, 2017 2:14 pm

Just look at the current rolling regression program and change the bit that stores the r-squared (equation.@r2) to store the standard error instead (equation.@se)
Follow us on Twitter @IHSEViews

umut
Posts: 2
Joined: Mon Apr 10, 2017 7:16 am

Re: Basic Rolling Regression

Postby umut » Mon Apr 10, 2017 8:00 am

Thank you very much for this topic. It helped a lot to me as a beginner in programming. I am benefited a lot from Esther's and Gareth's codes. However, I still couldn't reach the result.

I try to do univariate electricity price forecast the following 24 hours (24-step-ahead dynamic forecast) by using the previous 2 year's hourly data (17520 observations). I changed the Esther's code, but I can only get the forecast for the 24th hours, not for the hours 1-23. I want to do these 24-step ahead forecasts in a rolling window scheme for all days of the third year by using the actual values. I share the changed version of the program below. If you could help me, I will be very grateful.

'run rolling regression

' set window size
!window = 17520

' set step size
!step = 24

' get size of workfile
!length = @obsrange

' declare equation for estimation
equation eq01

'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

'*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 P =c(1) + c(2)*P1(-1)
eq01.ls RES C RES(-1) RES(-24)

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

' 1-period-ahead forecast
%24pers = @otod(@dtoo(%start)+!i+!window-1)
%24pere = @otod(@dtoo(%start)+!i+!window)
if @dtoo(%end) < @dtoo(%24pere) then 'check whether the forecast end point is greater than the workfile end point
exitloop
endif

' set smpl for forecasting period
smpl {%24pers} {%24pere}

' forecast with command *forecast* (see also *fit*)
eq01.forecast(f=na) yf

' set sampl to obtain the 24th period observation
smpl {%24pere} {%24pere}

' store forecasts
fcast = yf
next

smpl @all
show coefmat
show fcast.line

d(noerr) ser

The Yoyo
Posts: 9
Joined: Thu Apr 04, 2013 2:30 pm

Re: Basic Rolling Regression

Postby The Yoyo » Mon Apr 10, 2017 8:36 am

Have you tried changing the parts:

Code: Select all

' 1-period-ahead forecast
%24pers = @otod(@dtoo(%start)+!i+!window-1)
%24pere = @otod(@dtoo(%start)+!i+!window+23)

and then setting it the sample to

Code: Select all

' set sampl to obtain the 24th period observation
smpl {%24pers} {%24pere}


Not sure if that does what you are looking for.

umut
Posts: 2
Joined: Mon Apr 10, 2017 7:16 am

Re: Basic Rolling Regression

Postby umut » Tue Apr 11, 2017 7:06 am

Thank you very much "The Yoyo", it worked. I changed it to +22 instead of +23.

Alice Thai
Posts: 6
Joined: Fri Apr 21, 2017 6:28 am

Re: Basic Rolling Regression

Postby Alice Thai » Fri Apr 21, 2017 8:07 am

Hello,

In my dissertation, I want to apply the rolling coefficient for 5-year window (my data is monthly so window size = 60), and step size = 1. Could someone please help me how can i perform the test for pooled panel data (consist of 8 countries) using fixed effect and GLS weight method is Cross section SUR. I am really grateful for any advice since I have such limited experience on Eviews and the deadline is near. Many thanks for your help.

The Yoyo
Posts: 9
Joined: Thu Apr 04, 2013 2:30 pm

Re: Basic Rolling Regression

Postby The Yoyo » Fri Apr 21, 2017 8:28 am

What exactly are you looking for? Forecasts? Or just repeated GLS estimations?
It would be easier if you could provide the code you have written so far (or look for a example of a code in this forum).

Alice Thai
Posts: 6
Joined: Fri Apr 21, 2017 6:28 am

Re: Basic Rolling Regression

Postby Alice Thai » Fri Apr 21, 2017 8:49 am

The Yoyo wrote:What exactly are you looking for? Forecasts? Or just repeated GLS estimations?
It would be easier if you could provide the code you have written so far (or look for a example of a code in this forum).

Hi, I just want to repeat the estimation the see how the coefficient change over the tested period (from December 2002 to January 2017). Acctually I have no idea where to begin because this is the first time I come to Eviews and see this thread.

Alice Thai
Posts: 6
Joined: Fri Apr 21, 2017 6:28 am

Re: Basic Rolling Regression

Postby Alice Thai » Sat Apr 22, 2017 1:15 pm

This is my code so far, but when I press enter, a notification pops up "COEFMAT is not defined". What should I do in this situation? By the way, I want to show the rolling graph for my coefficent as well, what should I add in my code? Thank you.

Code: Select all

'run rolling regression

' set window size
!window = 60
 
' set step size
!step = 1
 
' 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 (SPOT(-1)-SPOT) = C (FORWARD-SPOT)
   eq1.LS(WGT=CXSUR) (SPOT(-1)-SPOT) C (FORWARD-SPOT)

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

show coefmat

The Yoyo
Posts: 9
Joined: Thu Apr 04, 2013 2:30 pm

Re: Basic Rolling Regression

Postby The Yoyo » Sun Apr 23, 2017 1:18 am

You should set the "3" in the part that reads:

Code: Select all

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


to the actual number of coefficients you estimate in your equation "eq1.LS(WGT=CXSUR) (SPOT(-1)-SPOT) C (FORWARD-SPOT)"
By the looks of it, it should be 2.

Alice Thai
Posts: 6
Joined: Fri Apr 21, 2017 6:28 am

Re: Basic Rolling Regression

Postby Alice Thai » Sun Apr 23, 2017 9:14 am

The Yoyo wrote:You should set the "3" in the part that reads:

Code: Select all

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


to the actual number of coefficients you estimate in your equation "eq1.LS(WGT=CXSUR) (SPOT(-1)-SPOT) C (FORWARD-SPOT)"
By the looks of it, it should be 2.


Thank you The Yoyo for the correction. However, when I press enter the code line !nrolls = @round((!length-!window)/!step) a message "!STEP is not defined". But i have defined !step = 1 on the above code. How can I solve this problem? Thank you so much.

The Yoyo
Posts: 9
Joined: Thu Apr 04, 2013 2:30 pm

Re: Basic Rolling Regression

Postby The Yoyo » Sun Apr 23, 2017 9:45 am

Alice Thai wrote:
The Yoyo wrote:You should set the "3" in the part that reads:

Code: Select all

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


to the actual number of coefficients you estimate in your equation "eq1.LS(WGT=CXSUR) (SPOT(-1)-SPOT) C (FORWARD-SPOT)"
By the looks of it, it should be 2.


Thank you The Yoyo for the correction. However, when I press enter the code line !nrolls = @round((!length-!window)/!step) a message "!STEP is not defined". But i have defined !step = 1 on the above code. How can I solve this problem? Thank you so much.



Are you running the entire script as a program? (File>New>Program)? Don't run the code line by line in the top view bar.
(Or similary, running:)

Code: Select all

'run rolling regression

' set window size
!window = 60

' set step size
!step = 1

' get size of workfile
!length = @obsrange

'calculate number of rolls
!nrolls = @floor((!length-!window)/!step)


'create series to store coefficients.  We'll be running !nrolls regressions  for 2 coefficients, so 2 series
matrix(!nrolls,2) coefmat

' declare equation for estimation
equation eq1

'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 (SPOT(-1)-SPOT) = C (FORWARD-SPOT)
   eq1.LS (SPOT(-1)-SPOT) C (FORWARD-SPOT)
'store the coefficient vector of this estimation in a matrix-row (transposing it first, for conveniency)
  rowplace(coefmat,@transpose(eq1.@coefs),!i)

next

show coefmat

Alice Thai
Posts: 6
Joined: Fri Apr 21, 2017 6:28 am

Re: Basic Rolling Regression

Postby Alice Thai » Sun Apr 23, 2017 1:02 pm

Dear The Yoyo, thank you so much, I am making progress on this, I have successfully created the rolling window. However, another problem arises since the result only displays around 250 observations (I have attached an image for the issue, I think this could be my data is pooled panel data so the program can only run smoothly for the 170 observations of the first country.). In addition, I want to estimate the change of the coefficients over the time series, so is there any way to display the year on the x-axis instead of the observations? Thank you for your help.
Attachments
problem.png
problem.png (120.94 KiB) Viewed 224015 times


Return to “Program Repository”

Who is online

Users browsing this forum: No registered users and 8 guests