*
* Implements tests from Im, Pesaran and Shin, "Testing for Unit Roots in
* Heterogeneous Panels", J. of Econometrics, 2003, vol 115, pp 53-74.
*
* Syntax:
*
* @ipshin( options ) series start end
*
* Parameters:
*
* series = series to analyze
* start end = range to use in performing tests [by default, maximum range of series]
*
* Options:
*
* LAGS=# of additional lags of the differenced series to include [0]
* TREND/[NOTREND]  include trend in addition to intercept in tests
* BLOCK=block size [size of panel data records]
* [PRINT]/NOPRINT show test output
*
* Variables Defined:
*   %CDSTAT - the "zbar" (standardized Normal) statistic
*   %TBAR   - the average DF statistic
*
* Revision Schedule:
*  09/2004 Written by Tom Doan, Estima
*  07/2007 Correct significance level for positive z statistics
*
procedure ipshin series start end
type series series
type integer start end
*
option switch  trend  0
option integer lags   0
option integer block
option switch  print  1
*
local integer startl endl tperiods base
local integer i j k nlast nfirst nsecs mint maxt
local series time dy
local vect[integer] detvar
local real tt ttbar ttildebar et etilde vt vtilde
local real adjust ttmu ttvar ttildemu ttildevar ttilde zbar ztildebar
local real cv01 cv05 cv10
*
* Moments for t (standard t's on the lagged y) and ttilde (t with sigma squared
* computed under the null) statistics.
*
fixed vect[int] tmomsizes(15)
 6 7 8 9 10 15 20 25 30 40 50 100 500 1000 1000000
fixed rect ttildemoments(2,15)
 -1.125 -1.178 -1.214 -1.244 -1.274 -1.349 -1.395 -1.423 -1.439 -1.463 -1.477 -1.504 -1.526 -1.526 -1.533
  0.497  0.506  0.506  0.527  0.521  0.565  0.592  0.609  0.623  0.639  0.656  0.683  0.704  0.702  0.706
fixed rect ttmoments(2,15)
 -1.520 -1.514 -1.501 -1.501 -1.504 -1.514 -1.522 -1.520 -1.526 -1.523 -1.527 -1.532 -1.531 -1.529 -1.533
  1.745  1.414  1.228  1.131  1.069  0.923  0.851  0.809  0.789  0.770  0.760  0.735  0.715  0.707  0.706
*
* Lookup tables for critical values of the average t statistics. 1%, 5% and 10% c.v's
* for different combinations of N and T.
*
fixed vect[int] dftsizes(12)
  5 10 15 20 25 30 40 50 60 70 100 1000000
fixed vect[int] dfnsizes(8)
  5  7 10 15 20 25 50 100
fixed vect[rect] dfcv_constant(3)(8,12)
-3.79, -2.66, -2.54, -2.50, -2.46, -2.44, -2.43, -2.42, -2.42, -2.40, -2.40, -2.40
-3.45, -2.47, -2.38, -2.33, -2.32, -2.31, -2.29, -2.28, -2.28, -2.28, -2.27, -2.27
-3.06, -2.32, -2.24, -2.21, -2.19, -2.18, -2.16, -2.16, -2.16, -2.16, -2.15, -2.15
-2.79, -2.14, -2.10, -2.08, -2.07, -2.05, -2.04, -2.05, -2.04, -2.04, -2.04, -2.04
-2.61, -2.06, -2.02, -2.00, -1.99, -1.99, -1.98, -1.98, -1.98, -1.97, -1.97, -1.97
-2.51, -2.01, -1.97, -1.95, -1.94, -1.94, -1.93, -1.93, -1.93, -1.93, -1.92, -1.92
-2.20, -1.85, -1.83, -1.82, -1.82, -1.82, -1.81, -1.81, -1.81, -1.81, -1.81, -1.81
-2.00, -1.75, -1.74, -1.73, -1.73, -1.73, -1.73, -1.73, -1.73, -1.73, -1.73, -1.73

-2.76, -2.28, -2.21, -2.19, -2.18, -2.16, -2.16, -2.15, -2.16, -2.15, -2.15, -2.15
-2.57, -2.17, -2.11, -2.09, -2.08, -2.07, -2.07, -2.06, -2.06, -2.06, -2.05, -2.05
-2.42, -2.06, -2.02, -1.99, -1.99, -1.99, -1.98, -1.98, -1.97, -1.98, -1.97, -1.97
-2.28, -1.95, -1.92, -1.91, -1.90, -1.90, -1.90, -1.89, -1.89, -1.89, -1.89, -1.89
-2.18, -1.89, -1.87, -1.86, -1.85, -1.85, -1.85, -1.85, -1.84, -1.84, -1.84, -1.84
-2.11, -1.85, -1.83, -1.82, -1.82, -1.82, -1.81, -1.81, -1.81, -1.81, -1.81, -1.81
-1.95, -1.75, -1.74, -1.73, -1.73, -1.73, -1.73, -1.73, -1.73, -1.73, -1.73, -1.73
-1.84, -1.68, -1.67, -1.67, -1.67, -1.67, -1.67, -1.67, -1.67, -1.67, -1.67, -1.67

-2.38, -2.10, -2.06, -2.04, -2.04, -2.02, -2.02, -2.02, -2.02, -2.02, -2.01, -2.01
-2.27, -2.01, -1.98, -1.96, -1.95, -1.95, -1.95, -1.95, -1.94, -1.95, -1.94, -1.94
-2.17, -1.93, -1.90, -1.89, -1.88, -1.88, -1.88, -1.88, -1.88, -1.88, -1.88, -1.88
-2.06, -1.85, -1.83, -1.82, -1.82, -1.82, -1.81, -1.81, -1.81, -1.81, -1.81, -1.81
-2.00, -1.80, -1.79, -1.78, -1.78, -1.78, -1.78, -1.78, -1.78, -1.77, -1.77, -1.77
-1.96, -1.77, -1.76, -1.75, -1.75, -1.75, -1.75, -1.75, -1.75, -1.75, -1.75, -1.75
-1.85, -1.70, -1.69, -1.69, -1.69, -1.69, -1.68, -1.68, -1.68, -1.68, -1.69, -1.69
-1.77, -1.64, -1.64, -1.64, -1.64, -1.64, -1.64, -1.64, -1.64, -1.64, -1.64, -1.64

fixed vect[rect] dfcv_trend(3)(8,11)
-8.12, -3.42, -3.21, -3.13, -3.09, -3.05, -3.03, -3.02, -3.00, -3.00, -2.99, -2.99
-7.36, -3.20, -3.03, -2.97, -2.94, -2.93, -2.90, -2.99, -2.88, -2.87, -2.86, -2.86
-6.44, -3.03, -2.88, -2.84, -2.82, -2.79, -2.78, -2.77, -2.76, -2.75, -2.75, -2.75
-5.72, -2.86, -2.74, -2.71, -2.69, -2.68, -2.67, -2.65, -2.66, -2.65, -2.64, -2.64
-5.54, -2.75, -2.67, -2.63, -2.62, -2.61, -2.59, -2.60, -2.59, -2.58, -2.58, -2.58
-5.16, -2.69, -2.61, -2.58, -2.58, -2.56, -2.55, -2.55, -2.55, -2.54, -2.54, -2.54
-4.50, -2.53, -2.48, -2.46, -2.45, -2.45, -2.44, -2.44, -2.44, -2.44, -2.43, -2.43
-4.00, -2.42, -2.39, -2.38, -2.37, -2.37, -2.36, -2.36, -2.36, -2.36, -2.36, -2.36

-4.66, -2.98, -2.87, -2.82, -2.80, -2.79, -2.77, -2.76, -2.75, -2.75, -2.75, -2.75
-4.38, -2.85, -2.76, -2.72, -2.70, -2.69, -2.68, -2.67, -2.67, -2.66, -2.66, -2.66
-4.11, -2.74, -2.66, -2.63, -2.62, -2.60, -2.60, -2.59, -2.59, -2.58, -2.58, -2.58
-3.88, -2.63, -2.57, -2.55, -2.53, -2.53, -2.52, -2.52, -2.52, -2.51, -2.51, -2.51
-3.73, -2.56, -2.52, -2.49, -2.48, -2.48, -2.48, -2.47, -2.47, -2.46, -2.46, -2.46
-3.62, -2.52, -2.48, -2.46, -2.45, -2.45, -2.44, -2.44, -2.44, -2.44, -2.43, -2.43
-3.35, -2.42, -2.38, -2.38, -2.37, -2.37, -2.36, -2.36, -2.36, -2.36, -2.36, -2.36
-3.13, -2.34, -2.32, -2.32, -2.31, -2.31, -2.31, -2.31, -2.31, -2.31, -2.31, -2.31

-3.73, -2.77, -2.70, -2.67, -2.65, -2.64, -2.63, -2.63, -2.62, -2.63, -2.62, -2.62
-3.60, -2.68, -2.62, -2.59, -2.58, -2.57, -2.57, -2.56, -2.56, -2.55, -2.55, -2.55
-3.45, -2.59, -2.54, -2.52, -2.51, -2.51, -2.50, -2.50, -2.50, -2.49, -2.49, -2.49
-3.33, -2.52, -2.47, -2.46, -2.45, -2.45, -2.44, -2.44, -2.44, -2.44, -2.44, -2.44
-3.26, -2.47, -2.44, -2.42, -2.41, -2.41, -2.41, -2.40, -2.40, -2.40, -2.40, -2.40
-3.18, -2.44, -2.40, -2.39, -2.39, -2.38, -2.38, -2.38, -2.38, -2.38, -2.38, -2.38
-3.02, -2.36, -2.33, -2.33, -2.33, -2.32, -2.32, -2.32, -2.32, -2.32, -2.32, -2.32
-2.90, -2.30, -2.29, -2.28, -2.28, -2.28, -2.28, -2.28, -2.28, -2.28, -2.28, -2.28
*
* Lookup tables for the mean and variance of the ADF statistics for combinations
* of ADF lags (down the columns, not across rows as presented in the paper) and
* time periods
*
fixed vect[int] adftsizes(11)
  10 15 20 25 30 40 50 60 70 100 100000
fixed rect adfmu_constant(11,9)
-1.504, -1.488, -1.319, -1.306, -1.171,     NA,     NA,    NA,    NA
-1.514, -1.503, -1.387, -1.366, -1.260, -1.239,     NA,    NA,    NA
-1.522, -1.516, -1.428, -1.413, -1.329, -1.313, -1.238,    NA,    NA
-1.520, -1.514, -1.443, -1.433, -1.363, -1.351, -1.289, -1.273, -1.212
-1.526, -1.519, -1.460, -1.453, -1.394, -1.384, -1.331, -1.319, -1.266
-1.523, -1.520, -1.476, -1.471, -1.428, -1.421, -1.380, -1.371, -1.329
-1.527, -1.524, -1.493, -1.489, -1.454, -1.451, -1.418, -1.411, -1.377
-1.519, -1.519, -1.490, -1.486, -1.458, -1.454, -1.427, -1.423, -1.393
-1.524, -1.522, -1.498, -1.495, -1.470, -1.467, -1.444, -1.441, -1.415
-1.532, -1.530, -1.514, -1.512, -1.495, -1.494, -1.476, -1.474, -1.456
-1.532, -1.530, -1.514, -1.512, -1.495, -1.494, -1.476, -1.474, -1.456

fixed rect adfvar_constant(11,9)
1.069, 1.255, 1.421, 1.759, 2.080,    NA,    NA,    NA,    NA
0.923, 1.011, 1.078, 1.181, 1.279, 1.420,    NA,    NA,    NA
0.851, 0.915, 0.969, 1.037, 1.097, 1.171, 1.237,    NA,    NA
0.809, 0.861, 0.905, 0.952, 1.005, 1.055, 1.114, 1.164, 1.217
0.789, 0.831, 0.865, 0.907, 0.946, 0.980, 1.023, 1.062, 1.105
0.770, 0.803, 0.830, 0.858, 0.886, 0.912, 0.942, 0.968, 0.996
0.760, 0.781, 0.798, 0.819, 0.842, 0.863, 0.886, 0.910, 0.929
0.749, 0.770, 0.789, 0.802, 0.819, 0.839, 0.858, 0.875, 0.896
0.736, 0.753, 0.766, 0.782, 0.801, 0.814, 0.834, 0.851, 0.871
0.735, 0.745, 0.754, 0.761, 0.771, 0.781, 0.795, 0.806, 0.818
0.735, 0.745, 0.754, 0.761, 0.771, 0.781, 0.795, 0.806, 0.818

fixed rect adfmu_trend(11,9)
-2.166, -2.173, -1.914, -1.922, -1.750,     NA,     NA,     NA,     NA
-2.167, -2.169, -1.999, -1.977, -1.823, -1.804,     NA,     NA,     NA
-2.168, -2.172, -2.047, -2.032, -1.911, -1.888, -1.778,     NA,     NA
-2.167, -2.172, -2.074, -2.065, -1.968, -1.955, -1.868, -1.851, -1.761
-2.172, -2.173, -2.095, -2.091, -2.009, -1.998, -1.923, -1.912, -1.835
-2.173, -2.177, -2.120, -2.117, -2.057, -2.051, -1.995, -1.986, -1.925
-2.176, -2.180, -2.137, -2.137, -2.091, -2.087, -2.042, -2.036, -1.987
-2.174, -2.178, -2.143, -2.142, -2.103, -2.101, -2.065, -2.063, -2.024
-2.174, -2.176, -2.146, -2.146, -2.114, -2.111, -2.081, -2.079, -2.046
-2.177, -2.179, -2.158, -2.158, -2.135, -2.135, -2.113, -2.112, -2.088
-2.177, -2.179, -2.158, -2.158, -2.135, -2.135, -2.113, -2.112, -2.088

fixed rect adfvar_trend(11,9)
1.132, 1.453, 1.627, 2.482, 3.947,    NA,    NA,    NA,    NA
0.869, 0.975, 1.036, 1.214, 1.332, 1.590,    NA,    NA,    NA
0.763, 0.845, 0.882, 0.983, 1.052, 1.165, 1.243,    NA,    NA
0.713, 0.769, 0.796, 0.861, 0.913, 0.991, 1.055, 1.145, 1.208
0.690, 0.734, 0.756, 0.808, 0.845, 0.899, 0.945, 1.009, 1.063
0.655, 0.687, 0.702, 0.735, 0.759, 0.792, 0.828, 0.872, 0.902
0.633, 0.654, 0.661, 0.688, 0.705, 0.730, 0.753, 0.786, 0.808
0.621, 0.641, 0.653, 0.674, 0.685, 0.705, 0.725, 0.747, 0.766
0.610, 0.627, 0.634, 0.650, 0.662, 0.673, 0.689, 0.713, 0.728
0.597, 0.605, 0.613, 0.625, 0.629, 0.638, 0.650, 0.661, 0.670
0.597, 0.605, 0.613, 0.625, 0.629, 0.638, 0.650, 0.661, 0.670

inquire(series=series) startl<<start endl<<end
set dy startl+1 endl = series-series{1}
*
if .not.%defined(block)
   compute tperiods=2//1-1//1
else
   compute tperiods=block
*
* Set up the list of deterministic variables
*
if trend {
   set time = t
   compute detvar=||constant,time||
}
else
   compute detvar=||constant||

*
* Figure out which individuals are covered by the range
*

compute base=(startl-1)/tperiods*tperiods+1
compute nlast=(endl-1)/tperiods,nfirst=(startl-1)/tperiods
compute nsecs=nlast-nfirst+1

*
* mint and maxt will keep track of the minimum and maximum number of
* time periods per individual. The other variables are for accumulation
* of means, variances and test statistics.
*

compute mint=maxt=0
compute ttbar=ttildebar=et=etilde=vt=vtilde=0.0
*
do i=nfirst,nlast
*
*     Run regression over data for individual "i". Include series{1}
*     and the detvar list regardless, and include the ADF lags if
*     the lags option is positive.
*
   linreg(noprint,entries=5+(trend>0)+6*(lags>0)) dy i*tperiods+1 (i+1)*tperiods
   # series{1} detvar dy{1 to lags}
*
*      tt is the standard t-statistic. adjust is the adjustment factor
*      for the variance and degrees of freedom for the variance calculated
*      under the null.
*
   compute tt=%tstats(1)
   compute adjust=(tt**2/%ndf+1)*%ndf/(%ndf+1.0)
   compute ttilde=tt/sqrt(adjust)
*
*      Adjust mint and maxt if necessary
*
   if mint==0.or.%nobs<mint
      compute mint=%nobs
   if maxt==0.or.%nobs>maxt
      compute maxt=%nobs
*
*     Look up the closest match (without going over the "T" dimension) for the
*     ADF mean and variance.
*
   do j=1,%rows(adftsizes)
      if %nobs<=adftsizes(j).or.j==%rows(adftsizes) {
         compute ttmu =%if(trend,adfmu_trend(j,lags+1) ,adfmu_constant(j,lags+1))
         compute ttvar=%if(trend,adfvar_trend(j,lags+1),adfvar_constant(j,lags+1))
         compute ttildemu=ttmu,ttildevar=ttvar
         break
      }
   end do j
*
*     For the special case of a simple DF regression without trend, use the
*     small (N dimension) sample lookup table instead.
*
   if lags==0.and.trend==0 {
      do j=1,%rows(tmomsizes)
         if %nobs<=tmomsizes(j).or.j==%rows(tmomsizes) {
            compute ttildemu=ttildemoments(1,j),ttildevar=ttildemoments(2,j),ttmu=ttmoments(1,j),ttvar=ttmoments(2,j)
            break
         }
      end do j
   }

   compute ttbar=ttbar+tt,ttildebar=ttildebar+ttilde
   compute et   =et+ttmu ,etilde   =etilde+ttildemu
   compute vt   =vt+ttvar,vtilde   =vtilde+ttildevar
end do i
*
compute ttbar=ttbar/nsecs
compute ttildebar=ttildebar/nsecs
compute zbar=nsecs*(ttbar-et/nsecs)/sqrt(vt)
compute ztildebar=nsecs*(ttildebar-etilde/nsecs)/sqrt(vtilde)
*
if print {
   disp "Im-Pesaran-Shin Unit Root Tests for Heterogeneous Panels"
   disp "Dependent Variable" %l(series)
   disp "Data for" nsecs "individuals"
   if lags
      disp "Using" lags "additional lags"
   if trend
      disp "Including trend in regressions"

   if mint<>maxt
      disp "Unbalanced Panel, ranging from" mint "to" maxt "data points per individual"
   else
   if lags==0 {
     do j=1,%rows(dftsizes)
        if maxt<=dftsizes(j).or.j==%rows(dftsizes)
           break
     end do j
     do k=1,%rows(dfnsizes)
        if nsecs<=dfnsizes(k).or.k==%rows(dfnsizes)
           break
     end do k
     if trend
        compute cv01=dfcv_trend(1)(k,j),cv05=dfcv_trend(2)(k,j),cv10=dfcv_trend(3)(k,j)
     else
        compute cv01=dfcv_constant(1)(k,j),cv05=dfcv_constant(2)(k,j),cv10=dfcv_constant(3)(k,j)
     disp "Balanced Panel with" mint "data points per individual"
     disp "tbar statistic" ttbar "t-tilde-bar statistic" ttildebar
     disp "Critical Values" @20 "1%" @35 "5%" @50 "10%"
     disp @12 cv01 @27 cv05 @42 cv10
   }
   disp "Z tbar statistic"      @26 ####.#### zbar      @37 "Significance Level" *.##### %cdf(zbar)
   disp "Z ttildebar statistic" @26 ####.#### ztildebar @37 "Significance Level" *.##### %cdf(ztildebar)
}
compute %cdstat=zbar
compute %tbar=ttbar
end

