*
* @hadri( options ) series start end
*
* Implements tests from Hadri(2000), "Testing for stationarity in
* heterogeneous panel data", The Econometrics Journal, vol 3, no 2,
* 148-161. This has stationarity as the null, and rejects if the
* detrended or de-meaned data are too persistent.
*
* Requires version 7.30 or later of RATS due to the TITLE option on
* REPORT(ACTION=DEFINE). That just affects the title of the window if
* you reload it with the Window--Restore Report operation. If you remove
* that, it will work with 7.1 or later.
*
* Parameters:
*
*   series     = series to analyze
*   start end  = range to use in performing tests [by default, maximum range of series]
*
* Options:
*   SMPL=Dummy series with 0's in entries to skip [include all]
*   DET=NONE/[CONSTANT]/TREND (NONE is an error)
*   VARIANCE=[HOMOGENEOUS]/HETEROGENEOUS/ROBUST
*   LWINDOW=NEWEY/BARTLETT/FLAT/PARZEN/QUADRATIC
*   LAGS=# of lags (or bandwidth for LWINDOW=QUADRATIC) [Schwert's]
*     If VARIANCE=HOMOGENEOUS, the variances are assumed to be the same in
*     each cross section. HETEROGENEOUS is for no serial correlation, but
*     differing variances. ROBUST allows for heterogeneous serial
*     correlation, with the long-run variance computed according to the
*     LWINDOW and LAGS options.
*
*   TITLE=title for output ["Hadri Unit Root Test Series: ..."]
*   [PRINT]/NOPRINT
*
* Variables Defined:
*   %CDSTAT - the "zbar" (standardized Normal) statistic
*   %SIGNIF - one tailed significance level
*   %NGROUP - number of individuals used
*
* Revision Schedule:
*  07/2010 Written by Tom Doan, Estima
*  03/2012 Replace ROBUST option with choice in VARIANCE option.
*
procedure hadri series start end
type series 	series
type integer 	start end
*
option choice  det      2 none constant trend
option choice  variance 1 homogeneous heterogeneous robust
option string  title
option series  smpl
option switch  print  	1
option choice  lwindow  2 newey bartlett flat parzen quadratic
option real    lags
*
local real        sigsqeps lastsum lm xi zeta pt nt lagsl
local vect 			sigsqepsi
local vect[int] 	detvars
local integer		startl endl starti endi i
local series      itrend icount psum
local vect[ser]	resids
local string      ltitle descript wstyle
local report      hreport
*
fixed vect[string] lwindowtext(5)
Bartlett(Newey-West)
Bartlett(Newey-West)
Flat
Parzen
Quadratic Spectral
*
if .not.%defined(series) {
   disp "###Syntax: @HADRI( options ) >>>>>SERIES<<<<<"
   return
}
if det==1 {
   disp "###@HADRI doesn't support DET=NONE"
   return
}

inquire(series=series) startl<<start endl<<end
compute starti=%indiv(startl),endi=%indiv(endl)
panel(icount=1,smpl=smpl,compress) series startl endl icount
*
* Figure out the biggest subsample, and the number of individuals with
* data.
*
sstats(max) starti endi icount>>pt
sstats      starti endi (icount>0)>>nt
*
dim sigsqepsi(endi)
*
* Create the individual trend series in case we need it.
*
set itrend = %period(t)
*
* Create the set of regressors and calculate the adjustment parameters.
* Note that the "zeta" are variances, not standard deviations. This is
* wrong in Baltagi's "Econometric Analysis of Panel Data".
*
if det==2 {
   compute detvars=||constant||
   compute descript="Constant"
   compute xi=1.0/6.0
   compute zeta=1.0/45.0
}
else {
   compute detvars=||constant,itrend||
   compute descript="Constant + Trend"
   compute xi=1.0/15.0
   compute zeta=11.0/6300.0
}
*
* Do individual specific regressions
*
sweep(group=%indiv(t),smpl=smpl,series=resids) startl endl
# series
# detvars
*
* Copy the residuals out of the VECT[SERIES] into %resids.
*
set %resids = resids(1)
*
* Compute the individual-specific partial sums. This re-initializes
* lastsum everytime we hit a new individual.
*
set(start=(lastsum=0.0)) psum startl endl = lastsum=%if(%period(t)==1,0.0,lastsum),$
    %if(%valid(%resids),lastsum=lastsum+%resids,%na)
*
* Compute the residual variances, either individual specific or full
* sample, depending upon the choice for the VARIANCES option.
*
if %defined(lags)
   compute lagsl=lags
else
   compute lagsl=fix(4*(.01*pt)^.25)

if variance==1 {
   *
   * Homogeneous simple variance
   *
   compute wstyle="Homogeneous Across Individuals"
   sstats(mean,smpl=%valid(%resids)) startl endl %resids^2>>sigsqeps
   *
   * Copy the one value to the entire vector
   *
   compute sigsqepsi=%const(sigsqeps)
}
else
if variance==2 {
   *
   * Heterogeneous simple variance
   *
   compute wstyle="Heterogeneous Across Individuals"
   do i=starti,endi
      if icount(i)
         sstats(mean,smpl=%valid(%resids)) i//1 i//%panelobs() %resids^2>>sigsqepsi(i)
   end do i
}
else {
   *
   * Heterogeneous long-run variance
   *
   compute wstyle="Robust to Serial Correlation, "+lwindowtext(lwindow)+"("+fix(lagsl)+")"
   do i=starti,endi
      if icount(i) {
         mcov(lwindow=lwindow,lags=lagsl) i//1 i//%panelobs()
         # %resids
         compute sigsqepsi(i)=%cmom(1,1)/%nobs
      }
   end do i
}
*
sstats(smpl=%valid(%resids),mean) startl endl psum^2/sigsqepsi(%indiv(t))>>lm
compute lm=lm/nt
*
* Compute one-tailed (for positive) significance level
*
compute %cdstat=sqrt(nt)*(lm-xi)/sqrt(zeta)
compute %signif=1-%cdf(%cdstat)
compute %ngroup=fix(nt)
*
if %defined(title)
   compute ltitle=title
else
   compute ltitle="Hadri LM 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) "Test has large N, large T asymptotics"
report(use=hreport,atrow=3,atcol=1,span) "Null is Stationary. Alternative is Some Unit Roots"
report(use=hreport,atrow=4,atcol=1,span) "Individual Specific Components: "+descript
report(use=hreport,atrow=5,atcol=1,span) wstyle
report(use=hreport,atrow=7,atcol=1) "N"      fix(nt)
report(use=hreport,atrow=8,atcol=1) "T"      fix(pt)
report(use=hreport,atrow=9,atcol=1) "Z" 	 	 %cdstat
report(use=hreport,atrow=10,atcol=1) "Signif" %signif
report(use=hreport,action=format,width=8,atcol=2)
if print
   report(use=hreport,action=show)
end

