*
* @EndersSiklos( options)  u  start  end
* does various types of the unit root regressions with threshold breaks
* on the residuals from an Engle-Granger cointegrating regression
* (assumed to be done already---the input u are the residuals).
*
* The regression run is
*
*  du = (rho1 D1 x u{1} + rho2 D2 x u{1}) + lags of du
*
* where D1 is the dummy for threshold series < tau and D2 is
* 1-D1.
*
* It produces three test statistics:
*
*   t-max is the larger of the two t-statistics on rho1=0 and rho2=0.
*   phi is the joint F-test for rho1=rho2=0
*   equal is the test for rho1=rho2.
*
* Parameters:
*   u   = series of residuals from Engle-Granger regression
*   start end  = range to use [defined range of u]
*
* You can either input the threshold value or request a search.
*
* Options:
*   LAGS=# of lags on the differences [1]
*   THRESHOLD=series which determines the threshold (required!)
*
*   TAU=threshold value [not used, chosen by search]
*   PI=fraction of high and low empirical values for the threshold series
*    which are omitted in the search.
*
* If you provide a value of TAU, the regressions are done with that
* fixed value for tau. If you don't, the values of the threshold series
* (excluding the PI fraction at each end) are searched for the one that
* minimizes the sum of squared residuals
*
* Variables Defined:
*  %%BREAKVALUE    Threshold value chosen or input
*  %%EQUALSTAT     F-test for rho1=rho2
*  %%PHISTAT       joint F-test for rho1=0 and rho2=0
*  %%TMAX          maximum t-statistic between rho1=0 and rho2=0
*
* Reference: Enders and Siklos(2001),  "Cointegration and Threshold
* Adjustment," JBES, vol. 19, no. 2, 166-76.
*
* Revision Schedule:
*  05/2011 Written by Tom Doan, Estima
*  10/2014 Add check for THRESHOLD empty
*
procedure EndersSiklos u start end
type series  u
type integer start end
*
option integer lags 1
option series  threshold
option real    tau
option real    pi   .15
option string  title
*
local series 	du
local vec[series] xsplit
local integer  i ttau
local vector   testvalues
local integer  startl endl pistart piend
local real     rssbest ltau taubest
local report   esreport
local string   ltitle
*
if .not.%defined(threshold).or..not.%defined(u) {
   disp "###Usage: @EndersSiklos(THRESHOLD=threshold series,other options) U ..."
   return
}
*
set du = u-u{1}
*
inquire(reglist) startl<<start endl<<end
# du{0 to lags}
*
dim xsplit(2)
*
if %defined(tau)
   compute ltau=tau
else {
   @UniqueValues(values=testvalues) threshold startl endl
   compute pistart=fix(pi*%rows(testvalues)),piend=%rows(testvalues)-pistart+1
   do ttau=pistart,piend
      compute ltau=testvalues(ttau)
      set xsplit(1) = u{1}*(threshold>=ltau)
      set xsplit(2) = u{1}*(threshold<ltau)
      *
      linreg(noprint) du startl endl
      # xsplit du{1 to lags}
      if ttau==pistart.or.%rss<rssbest
         compute taubest=ltau,rssbest=%rss
   end do tau
   compute ltau=taubest
}
compute %%breakvalue=ltau
set xsplit(1) = u{1}*(threshold>=ltau)
set xsplit(2) = u{1}*(threshold<ltau)
*
linreg(noprint) du startl endl
# xsplit du{1 to lags}
*
compute %%tmax=%max(%tstats(1),%tstats(2))
test(zeros,noprint)
# 1 2
compute %%phistat=%cdstat
restrict(noprint) 1
# 1 2
# 1.0 -1.0 0.0
compute %%equalstat=%cdstat
*
if %defined(title)
   compute ltitle=title
else
   compute ltitle="Enders-Siklos Test"

report(use=esreport,action=define,title=ltitle)
report(use=esreport,atrow=1,atcol=1,span) ltitle
report(use=esreport,row=new,atcol=1) "Lags" lags
report(use=esreport,row=new,atcol=1) "Threshold" ltau
report(use=esreport,row=new,atcol=1) "T-Max" %%tmax
report(use=esreport,row=new,atcol=1) "Phi"   %%phistat
report(use=esreport,row=new,atcol=1) "Equality" %%equalstat
report(use=esreport,row=new,atcol=2,align=center) "Coeff" "Std Error"
report(use=esreport,row=new,atcol=1) "Above" %beta(1) %stderrs(1)
report(use=esreport,row=new,atcol=1) "Below" %beta(2) %stderrs(2)
do i=1,lags
   report(use=esreport,row=new,atcol=1) "DU{"+(i)+"}" %beta(2+i) %stderrs(2+i)
end do i
report(use=esreport,action=format,picture="*.####")
report(use=esreport,action=show)
end
