*
*  RECRESID computes recursive residuals for a linear model.  It is
*  somewhat fancier than the similarly named procedure in the RATS
*  manual, allowing start and end to be set by default.
*
*  Syntax:
*
*    RECRESID depvar start end resids
*    # explanatory variables
*
*    depvar = dependent variable
*    start  end = range of estimation
*    resids = (output) series of recursive residuals
*
*  Written, Sept. 1990 by Tom Doan
*  Updated, January , 1992
*
PROCEDURE RECRESID DEPVAR START END RESIDS
TYPE SERIES DEPVAR
TYPE INTEGER START END
TYPE SERIES *RESIDS
*
LOCAL EQUATION REGEQN
LOCAL INTEGER TIME STARTL ENDL
LOCAL INDEX REGLIST
*
*  Get explanatory variables
*

ENTER(VARYING) REGLIST

*
*  Check that RESIDS is defined
*

IF .NOT.%DEFINED(RESIDS) {
   DISP 'Syntax: RECRESID  depvar  start  end  resids'
   RETURN
}

*
*  Determine range if START and END aren't set.
*

INQUIRE(REGRESSORLIST) STARTL>>START  ENDL>>END
# REGLIST DEPVAR

*
*  Run the regression, defining REGEQN
*

LINREG(DEFINE=REGEQN) DEPVAR STARTL ENDL
# REGLIST

SYSTEM(KALMAN) REGEQN
END(SYSTEM)
*
*  Estimate (perfect fit) over first NREG entries.  Kalman filter
*  over remainder.
*
ESTIMATE(NOPRINT) STARTL STARTL+%NREG-1
CLEAR RESIDS
DO TIME=STARTL+%NREG,ENDL
   KALMAN(RESIDS=RECURSIVE) RESIDS
END DO TIME
END

