*
*  @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
*     TREND/[NOTREND]
*       Indicates whether a trend was included in the cointegrating relation.
*     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]
*
*   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/05 Written by Tom Doan, Estima.
*
* Include the MacKinnon CV procedure
*
source mackinnoncv.src
*
procedure egtestresids u start end
type series u
type integer start end
*
option switch  trend     0
option integer lags      0
option switch  print     1
option integer nvar      2
*
local integer m
local series u du trnd
local integer i startl endl
local vect cv
*
inquire(series=u) startl<<start endl<<end
set du = u-u{1}
linreg(entries=4+6*(lags>0)) du startl+lags+1 endl
# u{1} du{1 to lags}
*
compute %nobs=%nobs+1
@mackinnoncv(nvar=nvar,determ=2+trend) cv
compute %cdstat=%tstats(1)
if print {
   disp 'Engle-Granger Cointegration Test'
   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

