*
* @HEGY series  start  end
*
* Computes Hylleberg et al (1990) seasonal unit root tests for
* quarterly time series, with and without intercept, seasonal
* dummies, and/or trend from the regression:
*
*     Y4(t) = DET + PI1 Y1(t-1) + PI2 Y2(t-1) + PI3 Y3(t-2) + PI4 Y3(t-1)
*            + PIi Y4(t-i) + e(t)
*
* Please note: This procedure is written for use with quarterly
* time series. Modifications would be required for use with monthly
* data.
*
* The following OPTIONS are available:
*
*
* DET=NONE/CONS/SD/TREND/STREND/[ALL]
*  Select the deterministic component. NONE sets the deterministic
*  component to zero; CONS includes intercept only; SD includes CONS
*  and (3) seasonal dummies; TREND includes CONS and trend; STREND
*  includes CONS, SD, and Trend; and ALL reports all 5 regressions.
*
* LAGS=lag length [4]
*  Controls default lag length. Can be overridden for particular
*  models using the other "lags" options listed below (e.g.,
*  "@HEGY(DET=ALL,LAGS=4,STLAGS=8) X").
*
* NLAGS = [LAGS]     Number of lags when DET=NONE.
* ILAGS = [LAGS]     Number of lags when DET=CONS.
* SLAGS = [LAGS]     Number of lags when DET=SD.
* TLAGS = [LAGS]     Number of lags when DET=TREND.
* STLAGS= [LAGS]     Number of lags when DET=STREND.
*  Note that for these 5 options, the default setting is controlled
*  by the LAGS option described above (its default value is 4).
*
* The tests reported by @HEGY are t-statistics on PI1, PI2, PI3,
* PI4 and an F-statistic on PI3=PI4=0.  Critical values for these
* tests are in Hylleberg et al (1990). Also reported are the
* extensions of HEGY method by Ghysels et al (1994).  These
* extensions are denoted F234 for PI2=PI3=PI4=0 and F1234 for
* PI1=PI2=PI3=PI4=0. Critical values for F234 and F1234 are in
* Ghysels et al (1994).
*
* The procedure reports a Lagrange Multiplier test for
* autocorrelation in the error term of order 1-4 and the
* significance of the last lag when i>0.
*
*REFERENCES:
* Hylleberg, S., R. Engle, C.W.J. Granger, and B. Yoo (1990),
* "Seasonal Integration and Cointegration", Journal of
* Econometrics, vol 44, pp 215-238
*
* Ghysels, E., H. Lee, and J. Noh (1994), "Testing for Unit Roots
* is Seasonal Time Series", Journal of Econometrics, vol 62, pp.
* 415-442.
*
* Revision Schedule:
*  Originally written by Suliman Al-Turki, IMF
*  09/2018 Rewritten by Tom Doan, Estima to use REPORT and consolidate
*          code by looping over the choices for deterministics.
*
procedure hegy series start end
type series series
type integer start end
*
option integer lags     4
option integer nlags    lags
option integer ilags    lags
option integer slags    lags
option integer tlags    lags
option integer stlags   lags
option choice  det	6  none cons sd trend strend all
option string  title
*
local integer startl endl
local integer testdet uselags
local series  y1 y2 y3 y4 ttrend sdum rs
local real    pi1 pi2 pi3 pi4 f234 f1234 f34 lagn sglagn har0 hsg0
local vect[int] detlist
local report  hreport
local string  detdesc ltitle
*
inquire(series=series) startl<<start endl<<end
*
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}
set ttrend startl endl = t
sea sdum
*
if %defined(title)
   compute ltitle=title
else
   compute ltitle="HEGY Seasonal Unit Root Test, Series "+%l(series)
*
report(use=hreport,action=define,title=ltitle)
report(use=hreport,atrow=1,atcol=1,span) ltitle
report(use=hreport,atrow=2,atcol=1,span) "Sample from "+%datelabel(startl)+" to "+%datelabel(endl)
report(use=hreport,atrow=3,atcol=1) "Observations" endl-startl+1
report(use=hreport,atrow=4,atcol=1) "" "PI1" "PI2" "PI3" "PI4" "F34" "F234" "F1234" "Lags" "AR(1-4)" "Signif" "Last Lag t" "Signif"
*
do testdet=1,5
   if det<>testdet.and.det<>6
      next
   if testdet==1 {
      compute detlist=%rlempty()
      compute detdesc="None"
      compute uselags=nlags
   }
   else
   if testdet==2 {
      compute detlist=||constant||
      compute detdesc="I Only"
      compute uselags=ilags
   }
   else
   if testdet==3 {
      compute detlist=||constant||
      compute detlist=%rladdlaglist(detlist,sdum,||-2,-1,0||)
      compute detdesc="I,SD"
      compute uselags=slags
   }
   else
   if testdet==4 {
      compute detlist=||constant,ttrend||
      compute detdesc="I,Tr"
      compute uselags=tlags
   }
   else {
      compute detlist=||constant,ttrend||
      compute detlist=%rladdlaglist(detlist,sdum,||-2,-1,0||)
      compute detdesc="I,SD,Tr"
      compute uselags=stlags
   }
   linreg(noprint) y4 startl endl rs
   # y1{1} y2{1} y3{2} y3{1} detlist y4{1 to uselags}
   *
   * Pull out/compute the standard HEGY statistics
   *
   compute pi1=%tstats(1)
   compute pi2=%tstats(2)
   compute pi3=%tstats(3)
   compute pi4=%tstats(4)
   exclude(noprint)
   # y1{1} y2{1} y3{1 to 2}
   compute f1234 = %cdstat
   exclude(noprint)
   # y2{1} y3{1 to 2}
   compute f234 = %cdstat
   exclude(noprint)
   # y3{1 to 2}
   compute f34 = %cdstat
   *
   * If we had non-zero lags on the differenced dependent variable,
   * pull out the t-stat on the final lag (which will be the final
   * coefficient in the regression)
   *
   if uselags>0 {
      compute lagn=%tstats(%nreg)
      cdf(noprint) ttest lagn %ndf
      compute sglagn=%signif
   }
   else {
      compute lagn=sglagn=%na
   }
   *
   * Compute the LM test for serial correlation
   *
   linreg(noprint) rs startl+4 endl
   # rs{1 to 4} %rlfromeqn(0)
   exclude(noprint)
   # rs{1 to 4}
   compute har0 = %cdstat
   compute hsg0 = %signif
   report(use=hreport,row=new,atcol=1) detdesc pi1 pi2 pi3 pi4 f34 f234 f1234 uselags har0 hsg0 lagn sglagn
end do testdet
*   disp 'none' @+3 ##.### pi1 @+1 ##.### pi2 @+1 ##.### pi3 @+1 ##.### pi4 $
*   @+1 ##.### f34 @+1 ##.### f234 @+1 ##.### f1234 @+2 nlags @+1 ##.### har0 $
*   @-1 '[' @-1 #.### hsg0 @-1 ']' @+1 ##.### lagn @-1 '[' @-1 #.### sglagn @-1 ']'
*
report(use=hreport,action=format,picture="##.###",atcol=2,tocol=13)
report(use=hreport,action=format,picture="##.###",atcol=2,tocol=8)
report(use=hreport,action=show)
end
