
* @ROBREG DEPVAR NB NE
* # list of dummy independent variables including a series
*   equal to 1.0 for the intercept (the "constant")
* # list of continuous-valued independent variables
*
* DEPVAR = the name of the dependent variable
* NB = the first observation in the data set
* NE = the last observation in the data set
*
* OPTION:
*
* ROBREG.SRC has no options.
*
* ROBREG.SRC estimates a linear regression robustly and rapidly.
* ROBREG.SRC should be especially useful for models whose
* independent variables include both dummy and continuous-
* valued regressors. It is known that these models lead to
* computational and conceptual difficulties for "high-breakdown"
* robust regression based on resampling schemes. (An example is
* REDI.SRC in this archive, which is effective when there are no
* dummy variables.)
* On the assumption that the dummy variables are uncontaminated,
* ROBREG.SRC screens the continuous-valued independent variables
* for leverage points, then uses a Huber-type M regression
* to detect outliers in the dependent variable.
* Finally, ordinary least squares is applied to those observations
* which are presumably free of outliers and leverage points.
*
* The first supplementary card should list the dummy variables
* and any other "designed" variables --such as linear or polynomial
* trends-- that cannot contain bad data. This card MUST include a
* series equal to 1.0 for the intercept (the "constant"). The
* series, which is in lieu of the Rats CONSTANT series, must be
* provided by the program that invokes ROBREG.SRC.
*
* The second supplementary card lists the continuous-valued
* regressors that might contain leverage points. The leverage
* points must be removed because they can seriously distort
* OLS (and many other regression methods in common use).

PROCEDURE ROBREG DEPVAR NB NE

TYPE SERIES DEPVAR
TYPE INTEGER NB NE
LOCAL VEC[INTEGER] NDUM NCON
LOCAL VEC MED SCALE CS975 CS50 B0 B1
LOCAL SYMMETRIC C D
LOCAL REC BVEC
LOCAL SERIES XVAR YVAR XRANK YRANK
LOCAL SERIES GOODOBS TEMPO
LOCAL REAL CUTOFF MYMAD CRIT
LOCAL INTEGER I J K ND NC COUNT


ENTER(VARYING) NDUM
INQUIRE(MATRIX=NDUM) ND

ENTER(VARYING) NCON
INQUIRE(MATRIX=NCON) NC

DIM MED(NC) SCALE(NC) CS975(20) CS50(20) B0(ND+NC)
DIM C(NC,NC) D(NC,NC) NDUM(ND) NCON(NC) B1(ND+NC)
COMPUTE C = %IDENTITY(NC)

COMPUTE [VECTOR[REAL]] CS975 =  $
|| 5.02,7.38,9.35,11.14,12.83,14.45,16.01,17.54,19.02,20.48, $
21.92,23.34,24.74,26.12,27.49,28.85,30.19,31.53,32.85,34.17 ||

COMPUTE [VECTOR[REAL]] CS50 =  $
|| 0.455,1.39,2.37,3.36,4.35,5.35,6.35,7.34,8.34,9.34,  $
10.3,11.3,12.3,13.3,14.3,15.3,16.3,17.3,18.3, 19.3 ||

* COMPUTE PAIRWISE SPEARMAN RANK CORRELATIONS FOR NCON

COMPUTE I = 0
DOFOR XVAR = NCON
COMPUTE J = 0
COMPUTE I = I+1
DOFOR YVAR = NCON
COMPUTE J = J+1
IF J .LE. I
BEGIN
BRANCH 10
END
ORDER(RANK=XRANK) XVAR NB NE
ORDER(RANK=YRANK) YVAR NB NE
LINREG(NOPRINT) YRANK NB NE
# XRANK CONSTANT
COMPUTE C(I,J) = %BETA(1)
COMPUTE C(J,I) = C(I,J)
:10 END DOFOR YVAR
END DOFOR XVAR

* DO A ROBUST STANDARDIZATION OF THE NCON VARIABLES

COMPUTE I = 0
DOFOR XVAR = NCON
COMPUTE I = I+1
STATISTICS(NOPRINT,NOMOMENTS,FRACTILES) XVAR NB NE
COMPUTE MED(I) = %MEDIAN
SET XVAR NB NE = XVAR-MED(I)
SET YVAR NB NE = ABS(XVAR)
STATISTICS(NOPRINT,NOMOMENTS,FRACTILES) YVAR NB NE
COMPUTE SCALE(I) = 1.4826*%MEDIAN
SET XVAR NB NE = XVAR/SCALE(I)
END DOFOR

* COMPUTE ROBUST DISTANCES AND IDENTIFY PROBABLE
* LEVERAGE POINTS

COMPUTE D = INV(C)
SET XVAR NB NE = 0.0
DO T = NB,NE
MAKE BVEC T T
# NCON
COMPUTE XVAR(T) = %QFORM(D,BVEC)
END DO T
STATISTICS(NOPRINT,NOMOMENTS,FRACTILES) XVAR NB NE
COMPUTE CUTOFF = CS975(NC)*%MEDIAN/CS50(NC)
SET GOODOBS NB NE = XVAR .LT. CUTOFF

* RETRANSFORM THE NCON VARIABLES

COMPUTE I = 0
DOFOR XVAR = NCON
COMPUTE I = I+1
SET XVAR NB NE = XVAR*SCALE(I) + MED(I)
END DOFOR
 
* COMPUTE AN M REGRESSION OMITTING LEVERAGE POINTS IN NCON

COMPUTE CRIT = 1000000.0
SET TEMPO NB NE = 0.0
DO J = 1,50
SET XRANK NB NE = %UNIFORM(0.1,1.0)
COMPUTE B0 = %CONST(1000.0)
COMPUTE B1 = %CONST(-1000.0)
COMPUTE COUNT = 0
UNTIL %TESTDIFF(B0,B1) .LT. 0.01 .OR. COUNT .GT. 20 {
COMPUTE COUNT = COUNT+1
COMPUTE B0 = B1
LINREG(NOPRINT,SPREAD=XRANK,SMPL=GOODOBS) DEPVAR
# NDUM NCON
COMPUTE B1 = %BETA
PRJ YRANK NB NE
SET XVAR NB NE = ABS(DEPVAR-YRANK)
STATISTICS(NOPRINT,NOMOMENTS,FRACTILES) XVAR NB NE
COMPUTE MYMAD = 1.4826*%MEDIAN
SET XVAR NB NE = XVAR/MYMAD
SET XRANK NB NE = 10000.0
DO T = NB,NE
IF XVAR(T) .LT. 3.0
BEGIN
COMPUTE XRANK(T) = 1.0/(1.0 - (XVAR(T)/3.0)**2)**2
END
END DO T
}

IF CRIT .GT. MYMAD
BEGIN
COMPUTE CRIT = MYMAD
SET TEMPO NB NE = XVAR
END
END DO J

* COMPUTE AN OLS REGRESSION OMITTING LEVERAGE POINTS
* AND OUTLIERS

SET XRANK NB NE = TEMPO .LT. 2.5

LINREG(SMPL=XRANK) DEPVAR
# NDUM NCON

* DISPLAY ROBUST STANDARDIZED RESIDUALS THAT EXCEED 2.5 IN
* MAGNITUDE

DISPLAY '   '
DISPLAY 'ROBUST STANDARDIZED RESIDUALS'

PRJ XRANK NB NE
SET XVAR NB NE = (DEPVAR-XRANK)/SQRT(%SEESQ)

DO T = NB,NE
IF ABS(XVAR(T)) .GT. 2.5
BEGIN
DISPLAY T XVAR(T)
END
END DO T

END ROBREG










































