*****************************************************************************
* @RESET  depvar start end
* #< supp. card > list of regressors for the linear specification
*
*  Parameters
*     depvar     is the series for the dependent variable
*     start end  are the start and end points for the depvar;
*                defaults to the entire length of the series
*
*  Options
*     H         = Maximum power of the dependent variable tested
*     TEST      = [FTest]/LMTest
*     [PRINT]/NOPRINT the results of the initial regression.
*     PC/[NOPC]   Use principal components of the powers of the
*                 in sample predictions in the auxiliary regression.
*     NUMBER    = the number of the largest principal component to use
*                 in the auxiliary regression, excluding the largest. Must
*                 be no larger than H-1. Default is [H-2].
*
*  The RESET test (Regression Error Specification Test) tests for neglected
*  nonlinearities in one's linear specification.  One regresses the in-sample
*  residuals on the SAME regressors as in the original linear regression and
*  on powers of the in-sample forecasts (yhat**2,..., yhat**h) and performs
*  an F-test for the joint significance of the coefficients or a Chi-Squared
*  test, see Ramsey (1969) or Granger and Terasvirta (1993).
*
*  The convergence of the test statistics is asymptotic.
*
*  Due to the large degree of multicollinearity possible in these powers,
*  Lin, White, and Granger (1993) suggest using reconstructed series of
*  powers based on principal components (excluding the first and largest one).
*
*  The test statistics and significance levels are saved as global variables;
*  if you don't like this, then place them in the LOCAL REAL section.
*
*  Sources
*     Ramsey, J. (1969): "Tests for specification errors in classical
*         Least-Squares Regression Analysis,"  Journal of the Royal
*         Statistical Society  B, 31:  350-371.
*     Granger, C.W.J, and T. Terasvirta (1993):  MODELLING NONLINEAR
*         ECONOMIC RELATIONSHIPS, Oxford University Press.
*     Lee, T.H., H. White, and C.W.J. Granger (1993): "Testing for
*         Neglected Non-linearities in Time Series Models,"  Journal
*         of Econometrics  56, 269-290.
*
*  Written by Norm Morin, March 1994, during Booknotes on C-SPAN
*
*  Revision History:
*   6/7/2000:  By Estima:
*              - Moved check on number of prin. components, so that is
*                 only performed if PC option is selected.
*              - Fixed description of F-statistics to reflect
*                 proper d.o.f: (N-# of params), not (N - H).
*
*
*  I'd like to thank Scott Spear at UCSD and the Tech Gurus at Estima
*  for helpful comments.  Any remaining problems are mine or the Gremlins'
*  which inhabit all computers.
*
******************************************************************************

********************************************************************
PROCEDURE PRCOMPT start end prcmp

TYPE    rect        *prcmp
OPTION  switch	    print 	1

LOCAL   index       xlist
LOCAL   integer	    p nobs hpc
LOCAL   symmetric   S
LOCAL   vect        egnval
LOCAL   rect	    egnvec evec pc x M
LOCAL   integer	    startl endl

*
*  Bring in explanatory variables
*

ENTER(varying) xlist

IF %defined(start).and.%defined(end) {
   COMPUTE startl = start ; COMPUTE endl = end
   }
ELSE {
   INQUIRE(regressorlist) startl endl
   # xlist
   }
COMPUTE nobs = endl - startl + 1

MAKE X startl endl
# xlist

CMOMENT(center) startl endl
# xlist

COMPUTE S = (1./%nobs)*%CMOM

EIGEN(explain=frvar) S egnval egnvec

COMPUTE prcmp = egnvec*tr(x)

END PRCOMPT

******************************************************************************
******************************************************************************

PROCEDURE RESET depvar start end

TYPE SERIES      depvar
TYPE INTEGER     start end

OPTION INTEGER 	 h        4
OPTION SWITCH    pc       0
OPTION INTEGER   number
OPTION SWITCH    print    0
OPTION CHOICE	 test     1  FTest  LMTest

LOCAL INDEX 	 reglist
LOCAL SERIES 	 depvar depres depvarhat
LOCAL INTEGER	 startl endl num
LOCAL REAL       ssr0 ssr
LOCAL RECT       pp
LOCAL VECT[SER]  powers pdex

*
*  Bring in the regressor list
*
ENTER(varying) reglist
*
*  Check for option errors
*

IF h < 2 {
   DISPLAY 'RESET error: H must be >= 2'
   return
}

IF %DEFINED(number)
   compute num = number
ELSE
   compute num = h-2


INQUIRE(regressorlist)  startl>>start  endl>>end
# reglist depvar


DISPLAY
DISPLAY '**************************************************************************'
DISPLAY '*  The RESET test for departures from linearity in the conditional mean  *'
DISPLAY '**************************************************************************'
DISPLAY
DISPLAY '  If the regressors contain lagged dependent variables,'
DISPLAY "  the test statistic's distibution is asymptotically attained."
DISPLAY


LINREG(print=print) depvar startl endl depres
# reglist

SET depvarhat startl endl  = depvar - depres
compute ssr0 = %rss


DIMENSION powers(h-1)

DO i = 2,h
   SET powers(i-1) = depvarhat**i
END DO i

IF (pc.eq.1)  {

   IF num > h-1 .or. num <= 0 {
    DISPLAY 'RESET error: NUMBER must be > 0 and <= H-1'
    return
   }

   DISPLAY '  This test is using powers of the in-sample predictions based on their'
   DISPLAY '  ' num ' largest principal components, excluding the first one.'
   DISPLAY

   @PRCOMPT(noprint) startl endl pp
   # powers

   DIMENSION pdex(num)

   DO i = 1,num
      SET pdex(i) startl endl = pp(i+1,t-startl+1)
   END DO

   IF (Test.eq.1) {
      LINREG(noprint) depvar startl endl
      # reglist pdex

      EXCLUDE(noprint)
      # pdex
      * COMPUTE %CDSTAT = ((ssr0-ssr)/(num-1))/(ssr/(%nobs-h))
      * COMPUTE %SIGNIF = %FTEST(%CDSTAT,num,%nobs-num)

      DISPLAY '  RESET Test, under the null hypothesis following an F(' @-1 num @-1 $
					',' @-1 %nobs-%nreg @-1 ')'
      DISPLAY '  Test statistic:' ##.### %CDSTAT   '    Significance Level:' #.##### %SIGNIF

   }

   ELSE {

      LINREG(noprint) depres startl endl
      # reglist pdex

      COMPUTE %CDSTAT = %TRSQ
      COMPUTE %SIGNIF = %CHISQR(%CDSTAT,num)

      DISPLAY '  RESET Test, under the null hypothesis following a ChiSquare(' @-1 h-1 @-1 ')'
      DISPLAY '  Test Statistic:' ##.### %CDSTAT   '   Significance Level:' #.##### %SIGNIF

   }

}


ELSE {


   IF (test.eq.1) {
      LINREG(noprint) depvar startl endl
      # reglist powers

      EXCLUDE(noprint)
      # powers

      DISPLAY '  RESET Test, under the null hypothesis following an F(' @-1 h-1 @-1 $
                                                                    ',' @-1 %nobs-%nreg @-1 ')'
      DISPLAY '  Test Statistic:' ##.### %CDSTAT   '    Significance Level:' #.##### %SIGNIF

   }

   ELSE {
      LINREG(noprint) depres startl endl
      # reglist powers

      COMPUTE %CDSTAT = %TRSQ
      COMPUTE %SIGNIF = %CHISQR(%CDSTAT,h-1)

      DISPLAY '  RESET Test, under the null hypothesis following a ChiSquare(' @-1 h-1 @-1 ')'
      DISPLAY '  Test Statistic:' ##.### %CDSTAT   '    Significance Level:' #.##### %SIGNIF

   }
}

DISPLAY

END

