*
* @STOCKWAT( options )  series   start  end
*
* This is a modification of Stock's procedure to do Stock-Watson and Dickey-Fuller
* unit root and time trend tests.
*
* Options:
*   ARCORR =  Number of autoregressive corrections [1]
*
* Reference:
*   Stock and Watson(1988), "Testing for Common Trends", JASA, vol. 83, 1097-1116.
*
proc stockwat series start end
type series series
type integer start end
*
option integer arcorr 1
*
local integer start0 start01 startl startm endm i j k
local rect cv
local vector qff qtt
local equation eq99
local vect[series] base
*
dim cv(3,6) qff(10) qtt(10)
*
inquire(series=series) startm<<start endm<<end
inquire(series=series) startl
if .not.%defined(start)
   compute startm=startl+arcorr+3

if startm<=startl+arcorr+2
   {
   dis "*** WARNING ***"
   dis "START does not allow for enough lags: START should be chosen"
   dis "so that (START-ARCORR-2) >= second date for which SERIES is defined"
   compute startm=startl+arcorr+3
   }

compute start0 =startm-arcorr-1
compute start01=start0-1

dim base(8)
set base(1) start01 endm = t
set base(2) start01 endm = t*t
compute cv=||.01 , -20.6 ,  -29.2 ,  -35.5 ,  -3.43 , -3.96 | $
             .05 , -14.1 ,  -21.7 ,  -27.9 ,  -2.86 , -3.41 | $
             .10 , -11.2 ,  -18.2 ,  -24.1 ,  -2.57 , -3.12 ||
dis " "
dis "-------------------------------------------------------------------------"
dis " Levels: "
corr(number=2*arcorr) series startm endm
diff series start01 endm base(3)
label base(3)
# "DIFF"+%LABEL(SERIES)
dis " Differences: "
corr(number=2*arcorr) base(3) startm endm
dis "     DICKEY-FULLER / STOCK-WATSON UNIT ROOT / TIME TREND TESTS"
dis "         Number of autoregressive corrections = " ARCORR
do i=0,1
   compute j=5*i
   if i==0
      {
      set base(4) start01 endm = series
      label base(4)
      # %label(series)
      }
   else
      {
      set base(4) start01 endm = base(3)
      label base(4)
      # %label(base(3))
      }
* Stock-Watson statistics
* First detrend the data
    do k=1,3
       if k==1
          {
          linreg(noprint) base(4) start01 endm base(5)
          # constant
          }
       else if k==2
          {
          linreg(noprint) base(4) start01 endm base(5)
          # constant base(1)
          }
       else
          {
          linreg(noprint) base(4) start01 endm base(5)
          # constant base(1) base(2)
          }
* Construct test statistics
       linreg(noprint) base(5) start0 endm base(6)
       # constant base(5){1}
       linreg(noprint,def=eq99) base(6) startm endm
       # constant base(6){1 to arcorr}
       filter(equ=eq99) base(5) startm-1 endm base(7)
       linreg(noprint) base(7) startm endm
       # constant base(7){1}
       compute qff(k+j)=%nobs*(%beta(2)-1)
    end do k
* Dickey-Fuller tests
    diff base(4) start0 endm base(8)
    linreg(noprint) base(8) startm endm
    # constant base(4){1} base(8){1 to arcorr} base(1)
    compute qff(4+j)=%beta(2)*(%seesq*%xx(2,2))**-.5
    linreg(noprint) base(8) startm endm
    # constant base(4){1} base(8){1 to arcorr}
    compute qff(5+j)=%beta(2)*(%seesq*%xx(2,2))**-.5
* Tests for deterministic trends
    linreg(noprint) base(4) startm endm
    # constant base(4){1 to arcorr}
    compute qtt(1+j)=%beta(1)*(%seesq*%xx(1,1))**-.5
    linreg(noprint) base(4) startm endm
    # constant base(1) base(4){1 to arcorr}
    compute qtt(2+j)=%beta(2)*(%seesq*%xx(2,2))**-.5
end do i
dis " "
dis "       TEST     Statistic     |   1% value      5% value      10% value "
dis " "
dis "   qf-tausq(z) " qff(3)  " | " cv(1,4) cv(2,4) cv(3,4)
dis "     qf-tau(z) " qff(2)  " | " cv(1,3) cv(2,3) cv(3,3)
dis "      qf-mu(z) " qff(1)  " | " cv(1,2) cv(2,2) cv(3,2)
dis "    qf-tau(dz) " qff(7)  " | " cv(1,3) cv(2,3) cv(3,3)
dis "     qf-mu(dz) " qff(6)  " | " cv(1,2) cv(2,2) cv(3,2)
dis "  DF: t-tau(z) " qff(4)  " | " cv(1,6) cv(2,6) cv(3,6)
dis "  DF:  t-mu(z) " qff(5)  " | " cv(1,5) cv(2,5) cv(3,5)
dis "  DF:t-tau(dz) " qff(9)  " | " cv(1,6) cv(2,6) cv(3,6)
dis "  DF: t-mu(dz) " qff(10) " | " cv(1,5) cv(2,5) cv(3,5)
dis "       time(z) " qtt(2)  " \ "
dis "   constant(z) " qtt(1)  "  }     Tests for deterministic "
dis "      time(dz) " qtt(7)  "  }  terms in levels and differences "
dis "  constant(dz) " qtt(6)  " / "
dis " "
end



