*
*  @EGTESTRESIDS( options ) resids  start  end
*  Computes an Engle-Granger test for cointegration using the residuals
*  from a previous first-stage regression, producing MacKinnon critical values.
*  Use EGTEST to do the first stage regression as well.
*
*  Options:
*     [PRINT]/NOPRINT
*       NOPRINT suppresses the output
*     DET=NONE/[CONSTANT]/TREND
*       Indicates which deterministic components were included in the first stage regression.
*     LAGS=number of additional lags [0]
*       LAGS indicates the degree of AR polynomial on the differences
*       to include in the unit root test on the residuals
*     NVAR=number of endogenous variables in the cointegrating regression [2]
*     TITLE=descriptive title for test [none]
*     TREND/NOTREND
*       Indicates whether a trend was included in the first stage regression.
*
*   Defines:
*    %NOBS   = number of regression observations + 1 (tables are based upon this)
*    %CDSTAT = test statistic
*
*  The (approximate) critical values for t-test form are from
*    MacKinnon, "Critical Values for Cointegration Tests", Long-Run
*      Economic Relationships, R.F. Engle and C.W.J. Granger, eds,
*       London, Oxford, 1991, pp 267-276
*
*  Revision Schedule:
*    07/2005 Written by Tom Doan, Estima.
*    05/2007 Added TITLE option
*    06/2007 Added DET option
*
* Include the MacKinnon CV procedure
*
source mackinnoncv.src
*
procedure egtestresids u start end
type series u
type integer start end
*
option choice  det       2  none constant trend
option switch  trend
option integer lags      0
option switch  print     1
option integer nvar      2
option string  title
*
local series u du
local integer startl endl
local vect cv
local integer table
*
inquire(series=u) startl<<start endl<<end
set du = u-u{1}
linreg(entries=4+6*(lags>0),noprint) du startl+lags+1 endl
# u{1} du{1 to lags}
*
compute %nobs=%nobs+1
if trend==1
   compute table=2+trend
else
   compute table=det

@mackinnoncv(nvar=nvar,determ=table) cv
compute %cdstat=%tstats(1)
if print {
   disp "Engle-Granger Cointegration Test"
   if %defined(title) ; disp title
   disp "Auxiliary regression run with "+lags+" additional lags"
   disp "Test Statistic" %tstats(1)
   disp "Critical Values" @20 "1%"  @30 "5%" @40 "10%"
   disp  *.## @20 cv(1) @30 cv(2) @40 cv(3)
}
end







