Basic Rolling Regression
Moderators: EViews Gareth, EViews Moderator

 Posts: 7
 Joined: Sat Jan 21, 2017 3:30 am
Re: Basic Rolling Regression
Thank you for your help. I used the roll addin 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 Rsquared of the residuals of a rolling regression. In my case, I want the s.e of the regression and not the Rsquared, 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.
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 Rsquared of the residuals of a rolling regression. In my case, I want the s.e of the regression and not the Rsquared, 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.

 Fe ddaethom, fe welon, fe amcangyfrifon
 Posts: 11260
 Joined: Tue Sep 16, 2008 5:38 pm
Re: Basic Rolling Regression
The addin doesn't support saving the ses. But it should be straight forward for you to edit the addin code to do it.
Follow us on Twitter @IHSEViews

 Posts: 7
 Joined: Sat Jan 21, 2017 3:30 am
Re: Basic Rolling Regression
EViews Gareth wrote:The addin doesn't support saving the ses. But it should be straight forward for you to edit the addin 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.

 Fe ddaethom, fe welon, fe amcangyfrifon
 Posts: 11260
 Joined: Tue Sep 16, 2008 5:38 pm
Re: Basic Rolling Regression
Just look at the current rolling regression program and change the bit that stores the rsquared (equation.@r2) to store the standard error instead (equation.@se)
Follow us on Twitter @IHSEViews
Re: Basic Rolling Regression
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 (24stepahead 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 123. I want to do these 24step 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)+!i1)
%last = @otod(@dtoo(%start)+!i+!window2)
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)
' 1periodahead forecast
%24pers = @otod(@dtoo(%start)+!i+!window1)
%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
I try to do univariate electricity price forecast the following 24 hours (24stepahead 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 123. I want to do these 24step 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)+!i1)
%last = @otod(@dtoo(%start)+!i+!window2)
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)
' 1periodahead forecast
%24pers = @otod(@dtoo(%start)+!i+!window1)
%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
Re: Basic Rolling Regression
Have you tried changing the parts:
and then setting it the sample to
Not sure if that does what you are looking for.
Code: Select all
' 1periodahead forecast
%24pers = @otod(@dtoo(%start)+!i+!window1)
%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.
Re: Basic Rolling Regression
Thank you very much "The Yoyo", it worked. I changed it to +22 instead of +23.

 Posts: 6
 Joined: Fri Apr 21, 2017 6:28 am
Re: Basic Rolling Regression
Hello,
In my dissertation, I want to apply the rolling coefficient for 5year 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.
In my dissertation, I want to apply the rolling coefficient for 5year 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.
Re: Basic Rolling Regression
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).
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).

 Posts: 6
 Joined: Fri Apr 21, 2017 6:28 am
Re: Basic Rolling Regression
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.

 Posts: 6
 Joined: Fri Apr 21, 2017 6:28 am
Re: Basic Rolling Regression
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+!i1 @first+!i+!window2
' estimate equation  where the equation is (SPOT(1)SPOT) = C (FORWARDSPOT)
eq1.LS(WGT=CXSUR) (SPOT(1)SPOT) C (FORWARDSPOT)
'store coefficients
colplace(coefmat,eq1.@coefs,!j)
next
show coefmat
Re: Basic Rolling Regression
You should set the "3" in the part that reads:
to the actual number of coefficients you estimate in your equation "eq1.LS(WGT=CXSUR) (SPOT(1)SPOT) C (FORWARDSPOT)"
By the looks of it, it should be 2.
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 (FORWARDSPOT)"
By the looks of it, it should be 2.

 Posts: 6
 Joined: Fri Apr 21, 2017 6:28 am
Re: Basic Rolling Regression
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 (FORWARDSPOT)"
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.
Re: Basic Rolling Regression
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 (FORWARDSPOT)"
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+!i1 @first+!i+!window2
' estimate equation  where the equation is (SPOT(1)SPOT) = C (FORWARDSPOT)
eq1.LS (SPOT(1)SPOT) C (FORWARDSPOT)
'store the coefficient vector of this estimation in a matrixrow (transposing it first, for conveniency)
rowplace(coefmat,@transpose(eq1.@coefs),!i)
next
show coefmat

 Posts: 6
 Joined: Fri Apr 21, 2017 6:28 am
Re: Basic Rolling Regression
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 xaxis instead of the observations? Thank you for your help.
 Attachments

 problem.png (120.94 KiB) Viewed 757 times
Return to “Program Repository”
Who is online
Users browsing this forum: No registered users and 1 guest