Lag selection
Moderators: EViews Gareth, EViews Jason, EViews Moderator, EViews Matt
Lag selection
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.
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.

 Fe ddaethom, fe welon, fe amcangyfrifon
 Posts: 12388
 Joined: Tue Sep 16, 2008 5:38 pm
Re: Lag selection
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
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
Re: Lag selection
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
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

 Fe ddaethom, fe welon, fe amcangyfrifon
 Posts: 12388
 Joined: Tue Sep 16, 2008 5:38 pm
Re: Lag selection
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 RSquared!)
%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 RSquared!)
Follow us on Twitter @IHSEViews
Re: Lag selection
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.
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.

 Fe ddaethom, fe welon, fe amcangyfrifon
 Posts: 12388
 Joined: Tue Sep 16, 2008 5:38 pm
Re: Lag selection
It looks to me as though your comparison code is ok, and it is choosing the correct number of lags. However when you reestimate the equation at the end of the loop, you are not reconstructing 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)
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
Re: Lag selection
How can I fix it? Have you any suggestion? Thanks a lot.
Re: Lag selection
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.
eq_{%y}.ls {%y} c PC1 PC2 ainf(0 to !bestlag)
Thank you very vey much Gareth.
Re: Lag selection
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
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

 Fe ddaethom, fe welon, fe amcangyfrifon
 Posts: 12388
 Joined: Tue Sep 16, 2008 5:38 pm
Lag selection
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
Re: Lag selection
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
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

 Fe ddaethom, fe welon, fe amcangyfrifon
 Posts: 12388
 Joined: Tue Sep 16, 2008 5:38 pm
Re: Lag selection
Nice.
For what it is worth, I think you can probably simplify this code quite a bit:
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
Re: Lag selection
Yes you are right, the simplified code is much easier to follow! Still a lot to learn

 Posts: 4
 Joined: Fri Mar 08, 2019 4:41 am
Re: Lag selection
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, Y1, ... Yk, X1 ... X1j, X2... X2j, ... X5...,X5j, 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.
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, Y1, ... Yk, X1 ... X1j, X2... X2j, ... X5...,X5j, 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.

 Fe ddaethom, fe welon, fe amcangyfrifon
 Posts: 12388
 Joined: Tue Sep 16, 2008 5:38 pm
Re: Lag selection
I think the lags should be specified as 1 to !lag rather than the other way round.
Follow us on Twitter @IHSEViews
Who is online
Users browsing this forum: No registered users and 1 guest