Page **1** of **2**

### Lag selection

Posted: **Mon Apr 20, 2009 2:14 pm**

by **huseyinky**

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.

### Re: Lag selection

Posted: **Mon Apr 20, 2009 2:38 pm**

by **EViews Gareth**

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#p955viewtopic.php?f=5&t=220&p=633&hilit=aic#p633

### Re: Lag selection

Posted: **Mon Apr 20, 2009 3:25 pm**

by **huseyinky**

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

### Re: Lag selection

Posted: **Mon Apr 20, 2009 3:31 pm**

by **EViews Gareth**

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!)

### Re: Lag selection

Posted: **Mon Apr 20, 2009 4:06 pm**

by **huseyinky**

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.

### Re: Lag selection

Posted: **Mon Apr 20, 2009 4:23 pm**

by **EViews Gareth**

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)

### Re: Lag selection

Posted: **Mon Apr 20, 2009 4:35 pm**

by **huseyinky**

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

### Re: Lag selection

Posted: **Mon Apr 20, 2009 4:40 pm**

by **huseyinky**

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.

### Re: Lag selection

Posted: **Sun Jul 17, 2016 9:45 pm**

by **wxhdiy01**

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

### Lag selection

Posted: **Sun Jul 17, 2016 11:06 pm**

by **EViews Gareth**

I think your loops are wrong. Think carefully about which regresses will be used at each stage of the loop

### Re: Lag selection

Posted: **Mon Jul 18, 2016 7:46 am**

by **wxhdiy01**

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

### Re: Lag selection

Posted: **Mon Jul 18, 2016 8:18 am**

by **EViews Gareth**

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

### Re: Lag selection

Posted: **Mon Jul 18, 2016 8:25 am**

by **wxhdiy01**

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

### Re: Lag selection

Posted: **Fri Mar 08, 2019 8:39 am**

by **Patric9871**

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.

### Re: Lag selection

Posted: **Fri Mar 08, 2019 9:01 am**

by **EViews Gareth**

I think the lags should be specified as -1 to -!lag rather than the other way round.