Lag selection

For questions regarding programming in the EViews programming language.

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

huseyinky
Posts: 18
Joined: Mon Apr 20, 2009 1:59 pm

Lag selection

Postby huseyinky » Mon Apr 20, 2009 2:14 pm

Dear all
I have following problem: I am running regression and I need to select optimal lag lenght by using information criterion. As you know the model which has minimum information criterion is selected as the best model in literature. I have following regression equation y c x z. By holding z unchanged I have to run 0 to 12 lags of x variable and then select lag structure which has minimum information criterion. More clearly among following equations
eq1 y c x z
eq2 y c x x(-1) z
eq3 y c x x(-1) z
.......
eq12 y c x x(-1) x(-2) x(-3).....x(-12) z

I need to find which has minimum information criterion (say aic).

I have numbers of dependent variable becaouse of that I need more quick way for selection

Thank in advance.

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

Re: Lag selection

Postby EViews Gareth » Mon Apr 20, 2009 2:38 pm

There are plenty of examples of people doing something similar already. Take a look at the following two posts, for example:

viewtopic.php?f=5&t=290&p=955&hilit=aic#p955

viewtopic.php?f=5&t=220&p=633&hilit=aic#p633
Follow us on Twitter @IHSEViews

huseyinky
Posts: 18
Joined: Mon Apr 20, 2009 1:59 pm

Re: Lag selection

Postby huseyinky » Mon Apr 20, 2009 3:25 pm

Actually I investigate the several posts and combining some of them I wrote the following code

for %y ainf21 ainf31 ainf32
equation eq_{%y}
!mininfocrit=999999
for !lag=-12 to 0
%regs=""
for !j=-12 to !lag
%regs=%regs + " %y(" + @str(!j) + " ) "
next
eq_{%y}.ls {%y} c pc1 pc2 {%regs}
if eq_{%y}.@aic<!mininfocrit then
!bestlag=-!lag
!mininfocrit=eq_{%y}.@aic
endif
Next
eq_{%y}.ls {%y} c PC1 PC2 {%y}(!bestlag)
Next


But it did not work. I think there is something wrong in the following part of my code. I cannot solve it.
%regs=""
for !j=-12 to !lag
%regs=%regs + " %y(" + @str(!j) + " ) "
next

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

Re: Lag selection

Postby EViews Gareth » Mon Apr 20, 2009 3:31 pm

Change:

%regs=%regs + " %y(" + @str(!j) + " ) "

to:

%regs=%regs + %y + "(" + @str(!j) + " ) "




Although I'll point out that you probably don't want to include up to zero lags. Regressing Y on Y(0) isn't going to turn out very well. (Well I guess that depends on your point of view - you'll get a great R-Squared!)
Follow us on Twitter @IHSEViews

huseyinky
Posts: 18
Joined: Mon Apr 20, 2009 1:59 pm

Re: Lag selection

Postby huseyinky » Mon Apr 20, 2009 4:06 pm

Thanks Gareth it works. You are totally right I get very high R2. Actually instead of using lag values of dependent variable I need to model lag values of explanatory variable. I change my code

for %y ainf21 ainf31 ainf32
equation eq_{%y}
!mininfocrit=999999
for !lag=-12 to -1
%regs=""
for !j=-12 to !lag
%regs=%regs + "ainf(" + @str(!j) + " ) "
next
eq_{%y}.ls {%y} c pc1 pc2 {%regs}
if eq_{%y}.@aic<!mininfocrit then
!bestlag=!lag
!mininfocrit=eq_{%y}.@aic
endif
Next
eq_{%y}.ls {%y} c PC1 PC2 ainf(!bestlag)
Next

It works but did not give the result I need. At the and It chooses model which include only one lagged explanatory variable something like that
best model is ainf21 c pc1 pc2 ainf(-10) or ainf32 c pc1 pc2 ainf(-8). It does not compare ainf21 c pc1 pc2 ainf(-1) to ainf21 c pc1 pc2 ainf(-1) ainf(-2). I know I am bothering you but please foregive me I am a rookie.

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

Re: Lag selection

Postby EViews Gareth » Mon Apr 20, 2009 4:23 pm

It looks to me as though your comparison code is ok, and it is choosing the correct number of lags. However when you re-estimate the equation at the end of the loop, you are not re-constructing the string containing all of the lags up to that point, rather you are only including the final lag.

Thus if the best model was:

ainf21 c pc1 pc2 ainf(-1) ainf(-2) ainf(-3) ainf(-4)

then !bestlag = 4, and then you estimate:

ainf21 c pc1 pc2 ainf(-4)
Follow us on Twitter @IHSEViews

huseyinky
Posts: 18
Joined: Mon Apr 20, 2009 1:59 pm

Re: Lag selection

Postby huseyinky » Mon Apr 20, 2009 4:35 pm

How can I fix it? Have you any suggestion? Thanks a lot.

huseyinky
Posts: 18
Joined: Mon Apr 20, 2009 1:59 pm

Re: Lag selection

Postby huseyinky » Mon Apr 20, 2009 4:40 pm

I have fixed it. It is like that
eq_{%y}.ls {%y} c PC1 PC2 ainf(0 to !bestlag)

Thank you very vey much Gareth.

wxhdiy01
Posts: 21
Joined: Wed Apr 13, 2016 7:07 am

Re: Lag selection

Postby wxhdiy01 » Sun Jul 17, 2016 9:45 pm

Hi Gareth,

I was trying to replicate this lag selection code, but most of the time the best lag length selected is 1, only y(-1) and c are included on the right hand side, while the best should actually be y(-1 to -7) or something like that according to the AIC values. The code looks right to me and I've been having trouble figuring out the problem. I essentially used the same code, only changed the max lag from 12 to 8:
for %y ainf21 ainf31 ainf32
equation ols_{%y}
!mininfocrit=999999
for !lag=-8 to -1
%regs=""
for !j=-8 to !lag
%regs=%regs + %y+"(" + @str(!j) + " ) "
next
ols_{%y}.ls {%y} c {%regs}
if ols_{%y}.@aic<!mininfocrit then
!bestlag=-!lag
!mininfocrit=ols_{%y}.@aic
endif
Next
ols_{%y}.ls {%y} c {%y}(-1 to -!bestlag)
Next

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

Lag selection

Postby EViews Gareth » Sun Jul 17, 2016 11:06 pm

I think your loops are wrong. Think carefully about which regresses will be used at each stage of the loop
Follow us on Twitter @IHSEViews

wxhdiy01
Posts: 21
Joined: Wed Apr 13, 2016 7:07 am

Re: Lag selection

Postby wxhdiy01 » Mon Jul 18, 2016 7:46 am

Thanks, I figured it out. It's easier to write the loop in a normal way like "1 to 8", than "-8 to -1", at least easier to understand when you put several loops together. And for those who were confused in a way as I did, the %regs in the inner loop will help you go through estimations using "y(-1)", "y(-1) y(-2)",..., "y(-1) y(-2)... y(-8)", so you are comparing the AIC of all the relevant specifications in the end. Thanks for sharing the code, it's very useful for beginners like me!

for %y y1 y2 y3
equation ols_{%y}
!mininfocrit=10
for !lag=1 to 8
%regs=""
for !j=1 to !lag
%regs=%regs +" "+ %y+"(" + @str(!j) + " ) "
next
ols_{%y}.ls {%y} c {%regs}
if ols_{%y}.@aic<!mininfocrit then
!bestlag=!lag
!mininfocrit=ols_{%y}.@aic
endif
Next
ols_{%y}.ls {%y} c {%y}(-1 to -!bestlag)
Next

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

Re: Lag selection

Postby EViews Gareth » Mon Jul 18, 2016 8:18 am

Nice.

For what it is worth, I think you can probably simplify this code quite a bit:

Code: Select all

for %y y1 y2 y3
equation ols_{%y}
!mininfocrit=10
for !lag=1 to 8
ols_{%y}.ls {%y} c {%y}(-1 to -!lag)
if ols_{%y}.@aic<!mininfocrit then
!bestlag=!lag
!mininfocrit=ols_{%y}.@aic
endif
Next
ols_{%y}.ls {%y} c {%y}(-1 to -!bestlag)
Next
Follow us on Twitter @IHSEViews

wxhdiy01
Posts: 21
Joined: Wed Apr 13, 2016 7:07 am

Re: Lag selection

Postby wxhdiy01 » Mon Jul 18, 2016 8:25 am

Yes you are right, the simplified code is much easier to follow! Still a lot to learn :wink:

Patric9871
Posts: 4
Joined: Fri Mar 08, 2019 4:41 am

Re: Lag selection

Postby Patric9871 » Fri Mar 08, 2019 8:39 am

Hi,
I am using Eviews 9.
I would like to confirm if my programm for optimal lag selection based on AIC in standard LS reg is correct.
Regression: Y(C, Y-1, ... Y-k, X1 ... X1-j, X2... X2-j, ... X5...,X5-j, dummy)
X1... X5 have the same lag (j)
Y has lag k
I want to find optimal (min AIC) combination of j and k.

I would be grateful for feedback.
Last edited by Patric9871 on Mon Apr 01, 2019 12:40 pm, edited 1 time in total.

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

Re: Lag selection

Postby EViews Gareth » Fri Mar 08, 2019 9:01 am

I think the lags should be specified as -1 to -!lag rather than the other way round.
Follow us on Twitter @IHSEViews


Return to “Programming”

Who is online

Users browsing this forum: Google [Bot] and 6 guests