* @ROBLGT 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 binary ( 0 / 1) dependent variable
* NB = the first observation in the data set
* NE = the last observation in the data set
*
* OPTION:
*
* ROBLGT.SRC has no options.
*
* ROBLGT.SRC estimates a binary logit regression robustly. It
* screens the continuous-valued independent variables
* for leverage points that can seriously distort the logit
* regression. To guarantee the existence of the maximum-
* likelihood estimate, ROBLGT also applies a transformation
* to the binary dependent variable (see Rousseeuw and Christmann,
* "Robustness against separation and outliers in logistic
* regression," Computational Statistics and Data Analysis 43
* (2003), 315-332).
*
* The first supplementary card should list all 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 ROBLGT.SRC.
*
* The second supplementary card lists the continuous-valued
* regressors that might contain leverage points.

PROCEDURE ROBLGT DEPVAR NB NE

TYPE SERIES DEPVAR
TYPE INTEGER NB NE
LOCAL VEC[INTEGER] NDUM NCON
LOCAL VEC MED SCALE CS975 CS50 B
LOCAL SYMMETRIC C D
LOCAL REC BVEC
LOCAL SERIES XVAR YVAR XRANK YRANK
LOCAL SERIES GOODOBS
LOCAL REAL CUTOFF DELTA DELTA0 DELTA1 PHAT
LOCAL INTEGER I J K ND NC
LOCAL FRML LOGIT LOGLIKE RHS

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

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

DIM MED(NC) SCALE(NC) CS975(20) CS50(20)
DIM C(NC,NC) D(NC,NC) NDUM(ND) NCON(NC) B(ND+NC)
COMPUTE C = %CONST(1.0)

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

* APPLY THE ROUSSEEUW-CHRISTMANN TRANSFORMATION TO THE
* BINARY DEPENDENT VARIABLE

COMPUTE DELTA = 0.01
STATISTICS(NOPRINT) DEPVAR NB NE
COMPUTE PHAT = %MAX(DELTA,%MIN(1.0-DELTA,%MEAN))
COMPUTE DELTA0 = DELTA*PHAT/(1.0+DELTA)
COMPUTE DELTA1 = (1.0+DELTA*PHAT)/(1.0+DELTA)
SET YRANK NB NE = DELTA0*(1.0-DEPVAR) + DELTA1*DEPVAR

* SET UP AND SOLVE THE LOGIT MODEL

LINREG(NOPRINT,SMPL=GOODOBS) YRANK
# NDUM NCON
FRML(LASTREG,ADDPARMS,VECTOR=B) RHS
FRML LOGIT = 1.0/(1.0+EXP(-RHS))
FRML LOGLIKE = YRANK*LOG(LOGIT) + (1.0-YRANK)*LOG(1.0-LOGIT)
MAXIMIZE(METHOD=BFGS,SMPL=GOODOBS,ITERATIONS=200) LOGLIKE


END ROBLGT





