# Re: st: Select minimum AIC from a matrix

 From smerryman@kc.rr.com To statalist@hsphsun2.harvard.edu Subject Re: st: Select minimum AIC from a matrix Date Wed, 09 Aug 2006 08:54:10 -0500

```Here is one way and an example of the output.

Scott

. webuse lutkepohl,clear
(Quarterly SA West German macro data, Bil DM, from Lutkepohl 1993
Table E.1)

. adftest dlinv, lag(10)

Optimal lag by AIC criteria = 3

Augmented Dickey-Fuller test for unit root         Number of obs
=        87

---------- Interpolated Dickey-Fuller --
-------
Test         1% Critical       5% Critical      10%
Critical
Statistic           Value             Value
Value
-----------------------------------------------------------------------
-------
Z(t)             -3.290            -3.528            -
2.900            -2.585
-----------------------------------------------------------------------
-------
MacKinnon approximate p-value for Z(t) = 0.0153

. adftest dlinv, lag(10) bic

Optimal lag by BIC criteria = 1

Augmented Dickey-Fuller test for unit root         Number of obs
=        89

---------- Interpolated Dickey-Fuller --
-------
Test         1% Critical       5% Critical      10%
Critical
Statistic           Value             Value
Value
-----------------------------------------------------------------------
-------
Z(t)             -7.510            -3.525            -
2.899            -2.584
-----------------------------------------------------------------------
-------
MacKinnon approximate p-value for Z(t) = 0.0000

. estimates stat

-----------------------------------------------------------------------
-------
Model |    Obs    ll(null)   ll(model)     df
AIC         BIC
-------------+---------------------------------------------------------
-------
adf_1_dlin~t |     89    112.0825    151.2462      3    -296.4923   -
289.0264
adf_2_dlin~t |     88    110.3282    149.7886      4    -291.5772   -
281.6679
adf_3_dlin~t |     87    108.9272    154.1228      5    -298.2457   -
285.9162
adf_4_dlin~t |     86    109.1804    152.3199      6    -292.6398   -
277.9137
adf_5_dlin~t |     85    107.9103    150.0973      7    -286.1947   -
269.0961
adf_6_dlin~t |     84    106.1527    147.9145      8     -279.829   -
260.3825
adf_7_dlin~t |     83    104.5957    147.4534      9    -276.9067   -
255.1372
adf_8_dlin~t |     82    103.5983    146.9816     10    -273.9633   -
249.8961
adf_9_dlin~t |     81    101.9356    144.9244     11    -267.8488   -
241.5098
adf_10_dli~t |     80     100.188     142.758     12    -261.5159   -
232.9316
-----------------------------------------------------------------------
-------

---------------------cut here ---------------------------
program define adftest, rclass
version 9.2
syntax varlist(max=1) [if] [in] [, Lags(int-1) bic ]
estimates clear
tokenize `varlist'
foreach var of local varlist{
forvalues i=1/`lags'{
//display "ADF(`i') of `var'"
qui dfuller `var', constant lags(`i')
local list storage(`i')
}
qui estimates stats `storage(`i')'
matrix s=r(S)
matrix criteria=J(`lags',2,.)
forvalues i=1/`lags' {
matrix criteria[`i',1]=s[`i',5]
matrix criteria[`i',2]=s[`i',6]
}
if "`bic'" != "" {
matrix criteria = criteria[1...,2]
local crit  "BIC"
}
else {
matrix criteria = criteria[1...,1]
local crit  "AIC"
}
}

mata: min_row(st_matrix("criteria"))
local min = min[1,1]
display " "
display in gr "Optimal lag by `crit' criteria = " `min'
dfuller `varlist', lag(`min') constant

end

mata:
matrix function min_row(matrix A)
{
B = colmin(A)
C = B:==A
A2 = (0, 0)
maxindex(C, 1, C, A2)
st_matrix("min",C)

}
end
---------------------cut here ---------------------------

----- Original Message -----
From: Rodrigo Martell <rodrigo.martell@frontier-economics.com.au>
Date: Tuesday, August 8, 2006 10:45 pm
Subject: st: Select minimum AIC from a matrix
To: statalist@hsphsun2.harvard.edu

> This might seem like a silly question but it stems from my
> inexperience with Stata. I've created (with a bit of help from
> StataCorp) a program that runs an ADF test for a series for a
> number of lags specified by the user. I get to a point where the
> AIC and BIC are reported in a matrix called "criteria". Here's the
> code I've written:
> ************************************************
> program define adftest263, rclass
> version 9
> syntax varlist(max=1) [if] [in] [, /*
> 	*/Lags(int-1)]
> tokenize `varlist'
> foreach var of local varlist{
> forvalues i=1/`lags'{
> display "ADF(`i') of `var'"
> dfuller `var', constant lags(`i') regress
> local list storage(`i')
> estimates store adf_`i'_`var'
> }
> estimates stats `storage(`i')'
> matrix s=r(S)
> matrix list s
> matrix criteria=J(`lags',2,.)
> forvalues i=1/`lags' {
> 	matrix criteria[`i',1]=s[`i',5]
> 	matrix criteria[`i',2]=s[`i',6]
> }
> matrix list criteria
> ***** Up to here it works fine *********
> }
> end
> ***********************************************************
> I want to now select the first column (I presume a command with
> j=1 and any `i'?) of this matrix and calculate the minimum. This
> minimum should
> correspond to the "best fitting (by AIC) ADF lag length". I want
> to select that lag length and run it for the series to report the
> result.Does anyone know how to do this? I'd greatly appreciate a
> hand. An example of what I'm trying to do:
>
> The result of running the program with an ADF(2) would be a matrix
> displayed like this. The first column corresponds to the AIC and
> the second to the BIC, while the rows correspond to the number of
> lags in the ADF:
>
> 	c1	c2
> r1   1.5    1.6
>
> r2    0.5    0.5
>
> From the output, I want to calculate the minimum of column c1
> (minimum AIC), which would be 0.5, which corresponds to the second
> row, which implies an ADF(2) is the best fitting model. I would
> then want to run -dfuller [series], constant lags(2)-
>
> I would love a hand if anyone can give me one. Thanks!
>
> Rodrigo
>
> Rodrigo Martell
>
> <http://www.frontier-economics.com>
> Frontier Economics Pty. Ltd.
> 395 Collins Street
> Melbourne VIC 3000
> Australia
> www.frontier-economics.com
> switch:
> direct:
> fax:
> mobile:
> email:
>
>
> +61 (0)3 9620 4488
> +61 (0)3 9613 1518
> +61 (0)3 8614 2711
> +61 (0)407 909 811
> rodrigo.martell@frontier-economics.com
> <mailto:rodrigo.martell@frontier-economics.com>
>
>
> This e-mail, including any attachments, may contain confidential
> and privileged information for the sole use of the intended
> recipient(s). Any review, use, disclosure or distribution by
> others is strictly prohibited. If you are not the intended
> recipient (or authorised to receive information for the
> all copies of this message. Thank you.
>
>
>
> *
> *   For searches and help try:
> *   http://www.stata.com/support/faqs/res/findit.html
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/
>
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```