*
*  @EGTEST( options ) start  end
*  # list of endogenous variables
*
*  Computes an Engle-Granger test for cointegration. This does the first stage regression
*  taking the first endogenous variable on the list as the dependent variable. Use
*  the related procedure EGTESTRESIDS if you already have the residuals.
*
*  Options:
*     [PRINT]/NOPRINT
*       NOPRINT suppresses the output
*     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
*     DET=NONE/[CONSTANT]/TREND
*       Determines what deterministic components are included in the first cointegration relation.
*     TREND/NOTREND (deprecated in favor of DET option)
*       Determines whether trend is included in the cointegrating relation.
*
*   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.
*    06/2007 DET option added
*
* Include the MacKinnon CV procedure
*
source mackinnoncv.src
*
procedure egtest start end
type integer start end
*
option choice  det       2  none constant trend
option integer lags      0
option switch  print     1
option switch  trend     0
*
local vect[int] datavec rhs dlist
local integer m
local series u du trnd
local integer i startl endl
local vect cv
local integer table
*
* Pull in list of series. Set m to the number of variables - 1.
* Copy positions 2 to m+1 into rhs.
*
enter(varying) datavec
compute m=%rows(datavec)-1
dim rhs(m)
ewise rhs(i)=datavec(i+1)
inquire(reglist) startl<<start endl<<end
# datavec
*
if trend==1
   compute table=3
else
   compute table=det
*
if table==3 {
   set trnd startl endl = t
   compute dlist=||constant,trnd||
}
else
if table==2 {
   compute dlist=||constant||
}
else
   dim dlist(0)
*
linreg(print=print) datavec(1) startl endl u
# rhs dlist
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=m+1,det=table) 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


