Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: Select minimum AIC from a matrix


From   "Rodrigo Martell" <[email protected]>
To   <[email protected]>
Subject   RE: st: Select minimum AIC from a matrix
Date   Thu, 10 Aug 2006 08:41:47 +1000

Thanks Nick and Scott. Nick, I am aware of the difference between criterion and criteria but usually don't pay much attention to it when writing them. You're right, it's pedantic but I should be more careful. Thanks. Thanks also for the comments on the code. I started learning programming with Stata last week by reading the codes in the Stata folder and know very little about it so getting feedback is a great way to learn and improve. The postings here on statalist I also find useful.

Thanks again,

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
[email protected] <mailto:[email protected]> 


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 recipient), please contact the sender by reply e-mail and delete all copies of this message. Thank you.

	



-----Original Message-----
From: [email protected]
[mailto:[email protected]]On Behalf Of Nick Cox
Sent: Thursday, 10 August 2006 7:51 AM
To: [email protected]
Subject: RE: st: Select minimum AIC from a matrix


Comments on two side issues: 

1. Yes, it's very pedantic, but clearly the 
"C" in AIC stands for "criterion". Output 
should thus be labelled in terms of "AI 
criterion" or "AIC". No doubt you can find plenty
of uses of "AIC criterion", but that doesn't make
them good labelling. Note also that "criteria" is 
the plural of "criterion" and not a singular 
or an alternative spelling.  

2. The code 

syntax varlist(max=1) 
tokenize `varlist'
foreach var of local varlist {
	...
} 

can be slimmed down. As there is only one 
variable, you can just go

syntax varname 

although the name in question will
be returned in `varlist'. 

The statement 

tokenize `varlist' 

does no harm, but is just incantation here
as its results are never used. 

Finally, the -foreach- loop is over a single 
name, and thus again does no harm, but is
nevertheless unnecessary. Either you can 
just define 

local var `varlist'

or you can substitute `varlist' for `var'
wherever it occurs. These are points in 
Rodrigo's original version: I guess Scott was 
concentrating on getting it working, not 
pedantry or polish. 

Nick 
[email protected] 

[email protected]
 
> Here is one way and an example of the output.
 
> . 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')
> 		estimates store adf_`i'_`var'
> 	}
> 	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 <[email protected]>
> Date: Tuesday, August 8, 2006 10:45 pm
> Subject: st: Select minimum AIC from a matrix
> To: [email protected]
> 
> > 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!

*
*   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/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index