*
* @DFUNIT  series  start  end
* Computes one of the Dickey-Fuller unit root tests.
*
* Parameters:
*   series     series to analyze
*   start end  range of <<series>> to use (*not* range over which test is run)
*              By default, the defined range of <<series>>
*
* Options:
*   [TTEST]/NOTTEST
*     Computes the regression t test, as opposed to the T(rho-1) test.
*   DET=NONE/[CONSTANT]/TREND
*     Choose what deterministic components to include.
*   [INTERCEPT]/NOINTERCEPT
*   TREND/[NOTREND]
*     These are older alternatives to the DET option.
*   LAGS=number of additional lags [0]
*     LAGS indicates the number of lags on the differences to include in the
*     regressions. (Note - some software will count the number of lags in the
*     expanded autoregression, which will be one more than the number of lags
*     on the differences).
*   [PRINT]/NOPRINT
*     NOPRINT suppresses the output
*
*   Defines:
*    %NOBS   = number of regression observations + 1 (tables are based upon this)
*    %RHO    = the lag coefficient
*    %CDSTAT = test statistic
*
*   Various other statistics (such as %DURBIN and %RSS) will be set to the values
*   from the regression run.
*
* References:
*   Fuller(1976), "Introduction to Statistical Time Series", New York, Wiley.
*   Dickey and Fuller(1979), "Distribution of the Estimators for Time Series
*   Regressions with a Unit Root", J.A.S.A., pp 427-431.
*
*   The (approximate) critical values for t-test form are from
*   MacKinnon(1991), "Critical Values for Cointegration Tests", Long-Run Economic
*   Relationships, R.F. Engle and C.W.J. Granger, eds, London, Oxford, pp 267-276
*
*   Revision Schedule:
*    11/1989 Written by Tom Doan, Estima
*    01/1992 Updated to version 4.0
*    06/2000 Updated to version 5.0
*    01/2003 INTERCEPT option added, output improved
*    10/2003 PRINT/NOPRINT option added
*    11/2006 DET option added as alternative to INTERCEPT and TREND
*    03/2009 Change to use of REPORT
*    01/2010 Make REPORT local
*
procedure dfunit series start end
type series    series
type integer   start end
*
option integer lags  0
option switch  intercept 1
option switch  trend 0
option switch  ttest 1
option switch  print 1
option choice  det   2  none constant trend
*
local integer  startl endl nobs tag
local series   sdiff strend
local real     teststat fiddle
local string   descript
local vect     critvalues
local integer  dettrend detconstant
local report   dfr
*
if .not.%defined(series) {
   disp "Syntax: @dfunit(options)  SERIES start end"
   return
}

inquire(series=series) startl<<start endl<<end
*
set sdiff  startl+1 endl = series-series{1}
set strend startl+1 endl = t

if trend.or.det==3
   compute dettrend=1
else {
   compute dettrend=0
   if .not.intercept.or.det==1
      compute detconstant=0
   else
      compute detconstant=1
}

if dettrend {
   linreg(noprint,entries=6+6*(lags>0)) series startl+lags+1 endl
   # series{1} constant strend sdiff{1 to lags}
   compute descript="With intercept and trend"
   compute nobs=%nobs+1
   compute critvalues=||-3.9638-8.353/nobs-47.44/(nobs**2),-3.4126-4.039/nobs-17.83/(nobs**2),-3.1279-2.418/nobs-7.58/(nobs**2)||
}
else if detconstant {
   linreg(noprint,entries=5+6*(lags>0)) series startl+lags+1 endl
   # series{1} constant sdiff{1 to lags}
   compute descript="With intercept"
   compute nobs=%nobs+1
   compute critvalues=||-3.4335-5.999/nobs-29.25/(nobs**2),-2.8621-2.738/nobs-8.36/(nobs**2),-2.5671-1.438/nobs-4.48/(nobs**2)||
}
else {
   linreg(noprint,entries=4+6*(lags>0)) series startl+lags+1 endl
    # series{1} sdiff{1 to lags}
   compute descript="Without intercept or trend"
   compute nobs=%nobs+1
   compute critvalues=||-2.5658-1.960/nobs-10.04/(nobs**2),-1.9393-0.398/nobs,-1.6156-0.181/nobs||
}
if lags {
  summarize(noprint)
  # sdiff{1 to lags}
  compute fiddle=1.0/(1.0-%sumlc)
}
else
  compute fiddle=1.0

if ttest
   compute teststat=(%beta(1)-1)/sqrt(%seesq*%xx(1,1))
else
   compute teststat=%nobs*fiddle*(%beta(1)-1)

compute %nobs=nobs,%cdstat=teststat,%rho=%beta(1)

if print==0
   return

report(use=dfr,action=define)
report(use=dfr,atrow=1,atcol=1,span) "Dickey-Fuller Unit Root Test, Series "+%l(series)
report(use=dfr,atrow=2,atcol=1,span) "Regression Run From "+%datelabel(startl+lags+1)+" to "+%datelabel(endl)
report(use=dfr,atrow=3,atcol=1) "Observations" %nobs
report(use=dfr,atrow=4,atcol=1) descript
report(use=dfr,atrow=5,atcol=1,span) "Using "+lags+" lags on the differences"
report(use=dfr,atrow=7,atcol=1) "Sig Level" "Crit Value"
report(use=dfr,atrow=8,atcol=1) "1%(**)"    critvalues(1)
report(use=dfr,atrow=9,atcol=1) "5%(*)"     critvalues(2)
report(use=dfr,atrow=10,atcol=1) "10%"       critvalues(3)
if ttest
   report(use=dfr,atrow=12,atcol=1) "T-Statistic" teststat
else
   report(use=dfr,atrow=12,atcol=1) "T(p-1)" teststat
compute tag=fix(1+(teststat<critvalues(1))+(teststat<critvalues(2)))
report(use=dfr,action=format,atrow=12,atcol=2,special=tag)
report(use=dfr,action=format,atrow=8,atcol=2,width=8,align=decimal)
report(use=dfr,action=show)
end

