*
* ADFAutoSelect
*
* @ADFAutoSelect( options )  series   start  end
*
* Parameters:
*
*    series     series being tested for unit roots
*
*    start end  range to use in calculations (this is NOT the range
*               used in the actual regressions). By default, the defined
*               range of series.
*
* Options:
*
*   MAXLAG=maximum number of lags to consider [Schwert's 12(n/100)**.25]
*   DET=NONE/[CONSTANT]/TREND  deterministic components
*   CRIT=[AIC]/BIC/HQ/MAIC  criterion to use in selecting lag length
*   YD=GLS detrended series [not used]
*   PRINT/[NOPRINT]  Prints a table for the various criteria
*
* Variables Defined:
*   %%LAGS = chosen number of lags
*
* Revision Schedule
*  07/2004 Written by Tom Doan, Estima. Based loosely on Norman Morin's SELECTUNI
*          procedure.
*
procedure ADFAutoSelect series start end
type series series
type integer start end
*
option switch  print    0
option integer maxlag
option choice  det      2  none  constant trend
option choice  crit     1  aic bic hq maic
option series  yd
*
declare integer %%lags
*
local integer maxlag lagnum nobs
local series trend delseries ylag
local real sigsq aic bic hq maic
local real test besttest
local integer startl endl lags lag
*
inquire(series=series) startl<<start endl<<end
compute  nobs = endl-startl+1
*
set delseries startl endl = series-series{1}
if .not.%defined(maxlag)
   compute lags=fix(12*(.01*nobs)**.25)
else
   compute lags=maxlag
*
if %defined(yd)
   set ylag startl endl = yd{1}
else
   set ylag startl endl = series{1}

set trend startl endl = t
cmom startl+maxlag+1 endl
# series ylag constant trend delseries{0 to maxlag}
*
if print {
  report(action=define)
  report(atrow=1,atcol=1,span) 'Information Criteria for ADF Lag Lengths, Series '+%l(series)
  report(atrow=2,atcol=1) 'Lags' 'AIC' 'BIC' 'HQ' 'MAIC'
  report(action=format,atcol=2,tocol=5,width=10)
}
do lag=0,lags
   if lag==0 {
      linreg(noprint,entries=det) delseries
      # ylag constant trend
   }
   else {
      linreg(noprint,entries=6+det) delseries
      # ylag delseries{1 to lag} constant trend
   }
   compute sigsq = %rss/%nobs
   compute aic  = log(sigsq)+2.0*%nreg/%nobs
   compute bic  = log(sigsq)+log(%nobs)*%nreg/%nobs
   compute hq   = log(sigsq)+2.0*log(log(%nobs))*%nreg/%nobs
   compute maic = log(sigsq)+2.0*(lag+%beta(1)**2*%cmom(2,2)/sigsq)/%nobs
   if print ; report(atrow=lag+3,atcol=1,align=decimal) lag aic bic hq maic
   if crit==1
      compute test=aic
   else
   if crit==2
      compute test=bic
   else
   if crit==3
      compute test=hq
   else
   if crit==4
      compute test=maic
   if lag==0.or.test<besttest
      compute besttest=test,%%lags=lag
end do lag
*
if print {
   report(action=format,tag=minimum,special=onestar,align=decimal,atrow=3,atcol=2,tocol=2)
   report(action=format,tag=minimum,special=onestar,align=decimal,atrow=3,atcol=3,tocol=3)
   report(action=format,tag=minimum,special=onestar,align=decimal,atrow=3,atcol=4,tocol=4)
   report(action=format,tag=minimum,special=onestar,align=decimal,atrow=3,atcol=5,tocol=5)
   report(action=format,atcol=2,tocol=5,picture='###.###')
   report(action=show)
}
end


