*
* @PerronRodriguez( options )  series start end
* does the Perron-Rodriguez(2003) test for unit roots using GLS
* detrending, allowing for a break point at an unknown date.
*
* Parameters:
*   series      Series to test for unit roots
*   start end   Range of series to use
*
* Options:
*   MAXLAGS=maximum number of lags in the auxiliary regression (depends on obs)
*   MINLAGS=minimum number of lags in the auxiliary regression [0]
*   BREAK=TREND/[BOTH]
*     Determines whether the break is just to the trend rate, or to the
*     trend rate and level.
*   PI=fraction of entries on each end of data to exclude as break points [.10]
*   SELECTBY=[TEST]/TSTAT
*     Determines whether to select the break point by the most extreme unit
*     root test [SELECTBY=TEST], or the largest absolute value of the
*     t-statistic for the trend break dummy [SELECTBY=TSTAT]. The authors
*     prefer the default.
*   [PRINT]/NOPRINT
*   TEST=[ADF]/MZT/PT
*
*  Variables Defined:
*    %CDSTAT=test statistic (minimum t-stat)
*    %MINENTRY=entry of the (lower) break giving the minimum t
*
*  Reference:
*     Perron and Rodriguez(2003), "GLS Detrending, Efficient Unit Root Tests
*     and Structural Change",  Journal of Econometrics, vol 115, pp 1-27.
*
*  Revision Schedule:
*   09/2009 Written by Tom Doan, Estima
*
source adfautoselect.src
source perronngmtests.src
*
procedure PerronRodriguez series start end
type series series
type integer start end
*
option choice  break    2  trend  both
option integer maxlag
option integer minlag   0
option real    pi       .10
option switch  print    1
option choice  selectby 1  test   tstat
option choice  test     1  adf    mzt    pt
*
local integer  startl endl pinobs lower upper
local integer  bestlag nobs lags tb glsbreak bestbreak
local series   dy ya
local real     selectstat beststat
local real     adf mzt pt
*
if .not.%defined(series) {
   disp "Syntax: @PerronRodriguez(options)  Y start end"
   return
}
*
inquire(series=series) startl<<start endl<<end
stats(noprint) series startl endl
if %nmiss>0 {
   disp "@PerronRodriguez can't be used on data with missing values"
   return
}
compute nobs=%nobs
*
if .not.%defined(maxlag)
   compute lags=fix(10*(.01*nobs)^.25)
else
   compute lags=maxlag
*
set dy    startl+1 endl = series-series{1}
*
* lower and upper bounds of regressions, allowing for lags
*
compute lower=startl+lags+1
compute upper=endl
*
* Shortest interval between the ends and the breaks
*
compute pinobs=fix(pi*(upper-lower+1))
*
* Remap option values to match those used by GLSDetrend
*
if break==1
   compute glsbreak=3
else
   compute glsbreak=4

compute beststat=%na
do tb=lower+pinobs,upper-pinobs
   @GLSDetrend(tb=tb,break=glsbreak) series startl endl ya
   compute selectstat=abs(%tstats(%nreg))
   @ADFAutoSelect(minlag=minlag,maxlag=lags,det=none,crit=maic,noprint) ya startl endl
   @PerronNgMTests(lags=%%lags,mzt=mzt,adf=adf,pt=pt) ya startl endl
   compute %cdstat=%if(test==1,adf,%if(test==2,mzt,pt))
   if selectby==1
      compute selectstat=-%cdstat
   if .not.%valid(selectstat).or.selectstat>beststat
      compute beststat=selectstat,bestbreak=tb,bestlag=%%lags
end do tb

end


