***********************************************************************************
*
*  HEGY series start end
*
*  Revision schedule:
*    01/94 Written by Jesper Hansson
*
*  Procedure calculates Hylleberg, Engle, Granger and Yoo (HEGY) tests for
*  seasonal integration. Details and critical values are found in HEGY (1990),
*  "Seasonal Integration and Cointegration", JOURNAL OF ECONOMETRICS, 44, 215-238.
*
*  By default (CRITERION=LMTEST), lags of the dependent variable are included
*  until the Lagrange Multiplier test for serial correlation in the residuals
*  can't be rejected at the choosen signinficance level (SIGNIF= ). Alternatively
*  the Ljung-Box test can be used (CRITERION=LBTEST). Another procedure
*  (CRITERION=SIGNLAG) starts with number of lags choosen by the user [5] and
*  removes insignificant lags, one by one. Finally (CRITERION=NOCRIT) the
*  user can supply the number of lags to be included, which will speed up the
*  estimation significantly.
*  The LM-test and the LB-test tests for autocorrelation of order up to [4] and
*  NOBS/4 respectively. The maximum number of lags included are 9.
*  The output presents the HEGY-test statistics, significance level of a
*  final LM-test for serial correlation and the lags included.
*
*  Syntax:
*    @HEGYQNEW series  start  end     {NOTE that 'start' - 'end' denotes the effective
*                                  sample, i.e. allowing for lags and differences}
*  Options:
*    CRITERION = LBTEST/[LMTEST]/SIGNLAG/NOCRIT
*    SIGNIF = [0.10] significance level for the tests above.
*    NLAGS = [5] number of lags of the dependent variable included as regressors.
*            Used only when CRITERION=SIGNLAG or NOCRIT.
*    LMLAGS = [4] maximum order of autocorrelation considered in the LM-test.
*
***********************************************************************************

procedure HEGYQNEW series start end
type series series
type integer start end
option integer nlags 5
option choice criterion 2 lbtest lmtest signlag nocrit
option real signif 0.10
option integer lmlags 4
*
local series season time y1 y2 y3 y4 resid lagsign lagvec
local vector stat
local integer startl endl i j nlag maxlag
local real lmsignif
local string lagstring
local index laglist
local vector[string] auxreg
local vector[index] auxlist
dimension stat(5) auxlist(5) auxreg(5)
set lagsign 1 nlags = 1.0
set lagvec 1 nlags = 1
*
inquire(series=series) startl endl>>end
if startl+nlags+4>start
  compute startl = startl+nlags+4
else
  compute startl = start

compute nlag = nlags

compute maxlag = 9
compute auxreg(1)='-      '
compute auxreg(2)='I      '
compute auxreg(3)='I,SD   '
compute auxreg(4)='I,Tr   '
compute auxreg(5)='I,SD,Tr'
*
enter(varying) auxlist(1)
#
enter(varying) auxlist(2)
# constant
enter(varying) auxlist(3)
# constant season{-2 to 0}
enter(varying) auxlist(4)
# constant time
enter(varying) auxlist(5)
# constant time season{-2 to 0}
*
seasonal season startl endl+4
set time = t
set y1 = series + series{1} + series{2} + series{3}
set y2 = - (series - series{1} + series{2} - series{3})
set y3 = - (series - series{2})
set y4 = series - series{4}
*
disp
disp '******************************* HEGY-tests **************************************'
disp 'Testing for seasonal integration in series:' %label(series)
disp 'Effective sample:' %datelabel(startl) 'to' %datelabel(endl)
disp 'Aux.regr.    ´t1´      ´t2´      ´t3´      ´t4´     ´F3&4´   LM-sign    Lags'
*
do j=1,5
  if criterion==1 {
    compute nlag = 0
    linreg(noprint) y4 startl endl
    # y1{1} y2{1} y3{2 1} auxlist(j)
    while %qsignif<signif.and.nlag<maxlag {
      compute nlag = nlag + 1
      linreg(noprint) y4 startl endl
      # y1{1} y2{1} y3{2 1} y4{1 to nlag} auxlist(j)
      }
    if nlag>0
      enter(varying) laglist
      # y4{1 to nlag}
    else
      enter(varying) laglist
      #
    }
  else if criterion==2 {
    compute nlag = 0
    linreg(noprint) y4 startl endl resid
    # y1{1} y2{1} y3{2 1} auxlist(j)
    linreg(noprint) resid startl endl
    # resid{1 to lmlags} y1{1} y2{1} y3{2 1} auxlist(j)
    compute lmsignif=%chisqr(%trsq,lmlags)
    while lmsignif<signif.and.nlag<maxlag {
      compute nlag = nlag + 1
      linreg(noprint) y4 startl endl resid
      # y1{1} y2{1} y3{2 1} y4{1 to nlag} auxlist(j)
      linreg(noprint) resid startl endl
      # resid{1 to lmlags} y1{1} y2{1} y3{2 1} y4{1 to nlag} auxlist(j)
      compute lmsignif=%chisqr(%trsq,lmlags)
      }
      if nlag>0
        enter(varying) laglist
        # y4{1 to nlag}
      else
        enter(varying) laglist
        #
    }
  else if criterion==4 {
    if nlag>0
      enter(varying) laglist
      # y4{1 to nlag}
    else
      enter(varying) laglist
      #
    }
  else if criterion==3 {
    enter(varying) laglist
    # y4{1 to nlag}
    do i=1,nlag
      compute lagvec(i)=i
    end do i
    linreg(noprint) y4 startl endl
    # laglist y1{1} y2{1} y3{2 1} auxlist(j)
    do i=1,nlag
      test(noprint,zeros)
      # i
      compute lagsign(i)=%signif
    end do i
    order lagsign 1 nlag lagvec
    while lagsign(nlag)>signif {
      enter(varying) laglist
      #
      compute nlag=nlag-1
      do i=1,nlag
        enter(varying) laglist
        # laglist y4{fix(lagvec(i))}
      end do i
      linreg(noprint) y4 startl endl
      # laglist y1{1} y2{1} y3{2 1} auxlist(j)
      do i=1,nlag
        test(noprint,zeros)
        # i
        compute lagsign(i)=%signif
      end do i
      order lagsign 1 nlag lagvec
      }
    }
  *
  linreg(noprint) y4 startl endl resid
  # y1{1} y2{1} y3{2 1} laglist auxlist(j)
  do i=1,4
    test(noprint, zeros)
    # i
    compute stat(i) = sqrt(%cdstat)*abs(%beta(i))/%beta(i)
  end do i
  test(noprint, zeros)
  # 3 4
  compute stat(5) = %cdstat
  *
  linreg(noprint) resid startl endl
  # resid{1 to lmlags} y1{1} y2{1} y3{2 1} laglist auxlist(j)
  compute lmsignif=%chisqr(%trsq,lmlags)
  *
  compute lagstring = ''
  if criterion==3 {
    do i=1,nlag
      compute lagstring = lagstring + %string(fix(lagvec(i)))
    end do i
    }
  else {
    do i=1,nlag
      compute lagstring = lagstring + %string(i)
    end do i
    }
  disp auxreg(j) ######.### stat(1) #####.### stat(2) #####.### stat(3) $
       #####.### stat(4) #####.### stat(5) #####.### lmsignif @+3 lagstring
  compute nlag = nlags
end do j
*
disp '*********************************************************************************'
disp

end
