Rolling Regression-extracting only of the coefficients

For questions regarding programming in the EViews programming language.

Moderators: EViews Gareth, EViews Jason, EViews Moderator, EViews Matt

Billy1989
Posts: 7
Joined: Wed Jul 25, 2018 12:14 pm

Rolling Regression-extracting only of the coefficients

Postby Billy1989 » Thu Jul 26, 2018 11:43 am

Hello,

I am a new user and normally do not perform analysis in EViews but I am helping out an ill co-worker. I am winging it so bear with me. I will need to run a rolling regression for 6 currencies against 9 commodities. Rather than using the add-in and performing the task 54 times and manually pulling the coefficient data and then the tstat data via the dialog prompts, I am attempting to write a simple program. I may be asked to do this for a variety of time periods or with other currencies. Finding a way to do this programmatically will be much better.

I found the basic code for rolling regression in the forums here. The rolling part is running fine (compared it against the using add-in program). However, my first obstacle is that I really only need one of the coefficients (the DLOG) and then I want to store that in a series. I will then eventually group all the series into one group. From what I see matrix objects cannot be grouped hence the need to put the data in a series.

It is when I attempt the MTOS portion that I run into problems. I tried to extract just the coeffiecient column I need in a rather clunky matter using transpose then subextract. That is okay. But no matter what I cannot get the MTOS to work.

I appreciate your help!
Attachments
simple_roll.prg
(1.02 KiB) Downloaded 265 times
test.wf1
(115.22 KiB) Downloaded 240 times

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

Re: Rolling Regression-extracting only of the coefficients

Postby EViews Gareth » Thu Jul 26, 2018 12:00 pm

When using MTOS you are assigning value from a vector into a series. To do that, you have to ensure that the number of observations you are copying is the same in both objects. For a series object the number of observations will be equal to the number of observations in the current workfile sample.

Your coefficient vector has 1180 rows.

The current sample has 104.

You'll need to change the sample to be 1180 observations:

Code: Select all

smpl @first @first+1179
Follow us on Twitter @IHSEViews

Billy1989
Posts: 7
Joined: Wed Jul 25, 2018 12:14 pm

Re: Rolling Regression-extracting only of the coefficients

Postby Billy1989 » Thu Jul 26, 2018 1:04 pm

Thank you, Gareth! It ran!

Now, I have a few other questions. As I mentioned I want to run this 54 times. So far I have been asked to run it with a 2 year window. But that could change. If I have all my data in excel as one spreadsheet, can I have program it to read through the header names to get the variable data or must I pass the set of variables individually through a loop? For example, I will run the AUD currency against LEAD, then AUD against NICKEL. The second currency will be USD against LEAD, then against NICKEL. So my data set in Excel would contain 54 columns with a header indicating what it is. I guess I am asking what way you think it best to run through the 54 combinations?

Second, my data has a lot of NAs and it is not consistent. For some currencies I am only missing a few weeks, but for others quite a bit. Same with the commodities. When testing, I just set the error limit to high to ignore the NAs, but there must a better way. I normally like the error level set to 1 so it makes debugging easier.

This is not my normal job, but I am having fun learning something new so that is a plus!

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

Re: Rolling Regression-extracting only of the coefficients

Postby EViews Gareth » Thu Jul 26, 2018 1:33 pm

There's always a trade off between time spent learning how to automate something and time to just do it manually...

Yes, you can read the names from Excel, either as part of the data import, or separately into a table if you want (importtbl). If you bring the series in and just want to grab the names of the series, you can use @wlookup.

Also, if you're new, I recommend looking here:

http://www.eviews.com/Learning/programming.html
http://www.eviews.com/help/helpintro.ht ... ng.html%23
viewtopic.php?f=5&t=1638
Follow us on Twitter @IHSEViews

Billy1989
Posts: 7
Joined: Wed Jul 25, 2018 12:14 pm

Re: Rolling Regression-extracting only of the coefficients

Postby Billy1989 » Thu Jul 26, 2018 7:52 pm

Gareth, plugging in the number worked great for this run, but what if my sample size changes? I would like to not hard code it. Is there a way to do so? Using @last?

And a few other questions:

Second to clean up my workfile, can I delete the series (SER01, SER02) that are created when performing the MTOS without causing harm to the new groups? I thought I read that the groups are actively tied to the series created on the fly by doing the MTOS.

Is there a way to rename the display at the top of the group that say SER01 to better indicate the variable I used?

Finally if I want to add the results of the tstats (group 2) to group 1 must I do that manually or can it be coded?

Thanks again

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

Re: Rolling Regression-extracting only of the coefficients

Postby EViews Gareth » Fri Jul 27, 2018 7:41 am

I don't know which number you are talking about, so can't answer the first.

You can use the rename command to rename ser01 etc..., but then the groups will be screwed, so you'd have to redefine the groups.

I find it better to first create the series with the names I want, make a group out of those series, then use mtos to fill the values in that group.

Once you have a group you can add a new series or group to it with the groupname.add proc.
Follow us on Twitter @IHSEViews

Billy1989
Posts: 7
Joined: Wed Jul 25, 2018 12:14 pm

Re: Rolling Regression-extracting only of the coefficients

Postby Billy1989 » Fri Jul 27, 2018 8:25 am

I was referring to using the hard coded number 1179 to indicate the number in my sample to do the MTOS (my question from the first post). I know it will change as I add new data. So here is what I did.

'set sample size for number of extracts
smpl @first+!window @last

With new data sets of different sample sizes it appears to be working.

I will give your suggestion a try for the series/group question. I am trying to write as clean as a program as I can. My current job has nothing to do with analyzing and I will be passing this program to others who do. For the most part I provide support in writing macros and simple programs (VB mainly).

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

Re: Rolling Regression-extracting only of the coefficients

Postby EViews Gareth » Fri Jul 27, 2018 8:47 am

You can check the length of a vector with @rows(vectorname)
Follow us on Twitter @IHSEViews

Billy1989
Posts: 7
Joined: Wed Jul 25, 2018 12:14 pm

Re: Rolling Regression-extracting only of the coefficients

Postby Billy1989 » Fri Jul 27, 2018 10:00 am

Gareth, pardon my lack of understanding. Unfortunately, I am more of a coder than a forecaster so I am having trouble understanding where or how I should use the @row(vector) function. Can you provide and example? I am really not sure which of my data elements is a vector.

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

Re: Rolling Regression-extracting only of the coefficients

Postby EViews Gareth » Fri Jul 27, 2018 10:30 am

Sorry, I meant matrix not vector.
Follow us on Twitter @IHSEViews

Billy1989
Posts: 7
Joined: Wed Jul 25, 2018 12:14 pm

Re: Rolling Regression-extracting only of the coefficients

Postby Billy1989 » Fri Jul 27, 2018 11:44 am

Okay, got it. Did it in two steps like this

!num_of_rows = @rows(coefmat_transpose)
smpl @first @first+(!num_of_rows-1)

But then I tried your suggestion of creating two series, adding them to a created group and then performing the MTOS. My two series came out with the right number of rows but the group I created resulted in something entirely different. I am kind of giving up with that. So far I got the program to work but am not sure there is still something wrong with the MTOS.

-------------------------------------------------------------------------------------------------------------
'RUN ROLLING REGRESSION AND EXTRACT COEFFICIENT/TSTATS PROGRAM
'--------------------------------------------------------------------------------------------------------------
%0="ars"
%1="pall"

'DECLARATIONS
'-----------------------
!window = 104 'set window size
%suffix = @str(!window/52) 'number of years in roll process
!step = 1 'set step size
!length = @obsrange 'get size of workfile
equation eq1 'declare equation for estimation
!nrolls = @round((!length-!window)/!step) 'calculate number of rolls
matrix(2,!nrolls) coefmat 'matrix to store coefficient estimates (2=num of coefficients)
matrix(2,!nrolls) tstat 'matrix to store tstat estimates (2=num of coefficients)
!j=0 'variable keeping track of how many rolls we've done

'Delete previous final extracts
'delete {%0}_{%1}_{%suffix}yr_coefs
'delete {%0}_{%1}_{%suffix}yr_tstats

'ROLL PROCESS
'--------------------------
'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
eq1.ls dlog({%0}) c dlog({%1})

'store coefficients
colplace(coefmat, eq1.@coefs, !j)
colplace(tstat, eq1.@tstats, !i)

'EXTRACTION PROCESS
'-----------------------------------
'transpose, copy, extract and rename

matrix coefmat_transpose = @transpose(coefmat)
matrix tstat_transpose = @transpose(tstat)

matrix coefmat_column1 = @subextract(coefmat_transpose, 1,2)
matrix tstat_column1 = @subextract(tstat_transpose, 1,2)

'set sample size for number of extracts
!num_of_rows = @rows(coefmat_transpose)
smpl @first @first+(!num_of_rows-1)

mtos(coefmat_column1, g1)
mtos(tstat_column1, g2)

rename ser01 {%0}_{%1}_{%suffix}yr_coefs
rename ser02 {%0}_{%1}_{%suffix}yr_tstats

group grp_{%0}_{%1}.add {%0}_{%1}_{%suffix}yr_coefs {%0}_{%1}_{%suffix}yr_tstats

'clean up workfile from unneccesary objects
delete coefmat
delete tstat
delete coefmat_transpose
delete tstat_transpose
delete coefmat_column1
delete tstat_column1
delete eq1
delete g1
delete g2


Return to “Programming”

Who is online

Users browsing this forum: No registered users and 28 guests