|
Size: 2199
Comment:
|
Size: 2636
Comment:
|
| Deletions are marked like this. | Additions are marked like this. |
| Line 1: | Line 1: |
| Line 4: | Line 3: |
| The syntax below uses dummy data of one binary variable (y) and a continuous predictor, x and is an adaptation of code from Adam W taken from [[http://stackoverflow.com/questions/24216574/how-to-do-leave-one-out-cross-validation-in-spss ! here]] for cross validating using the leaving-one-out approach for a linear regression model. | The syntax below uses dummy data of one binary variable (y) and a continuous predictor, x and is an adaptation of code from Andy W taken from [[http://stackoverflow.com/questions/24216574/how-to-do-leave-one-out-cross-validation-in-spss | here]] for cross validating using the leaving-one-out approach for a linear regression model. |
| Line 23: | Line 22: |
| The macro then produces the classification table of predicted by true group for the single omitted data point. | We can save this file into a file dummy.sav in U:/My Documents folder. The macro below can then be used to produce the classification table of predicted by true group for the single omitted data point. The inputs are number of rows in data file (assumed sample size), outcome variable name, name of datafile sandwiched between % (percent) signs and the names of the predictors in the logistic regression. |
| Line 28: | Line 27: |
| /!POS !CMDEND) | /!POS !ENCLOSE('%','%') /!POS !CMDEND). |
| Line 47: | Line 47: |
| /TABLE = 'Sim' | /TABLE = !QUOTE(!3) |
| Line 49: | Line 49: |
#MATCH FILES FILE = * # /TABLE = 'Sim' # /BY Id. |
|
| Line 58: | Line 62: |
| /METHOD=ENTER !3 | /METHOD=ENTER !4 |
| Line 82: | Line 86: |
| Line 85: | Line 90: |
| !LOOE 100 Y X. | !LOOE 40 y %U:/My Documents/dummy.sav% x1 x2. |
The leaving one out error rate in SPSS for logistic regression
The syntax below uses dummy data of one binary variable (y) and a continuous predictor, x and is an adaptation of code from Andy W taken from here for cross validating using the leaving-one-out approach for a linear regression model.
INPUT PROGRAM. LOOP Id = 1 TO 100. END CASE. END LOOP. END FILE. END INPUT PROGRAM. DATASET NAME Sim. COMPUTE X = RV.NORMAL(10,5). COMPUTE YS = 3 + 0.2*(X) + RV.NORMAL(0,0.2). RECODE YS (Lowest thru 4.5=0) (4.5 thru Highest=1) INTO Y. COMPUTE YCOPY=Y. FORMATS Id (F2.0) X Y YCOPY (F4.2). EXECUTE.
We can save this file into a file dummy.sav in U:/My Documents folder. The macro below can then be used to produce the classification table of predicted by true group for the single omitted data point. The inputs are number of rows in data file (assumed sample size), outcome variable name, name of datafile sandwiched between % (percent) signs and the names of the predictors in the logistic regression.
DEFINE !LOOE (!POS !TOKENS(1)
/!POS !TOKENS(1)
/!POS !ENCLOSE('%','%')
/!POS !CMDEND).
COMPUTE YCOPY=!2.
INPUT PROGRAM.
COMPUTE #Cases = !1.
LOOP #Id = 1 TO #Cases.
LOOP #Iter = 1 TO #Cases.
COMPUTE L1O = #Iter.
COMPUTE Id = #Id.
END CASE.
END LOOP.
END LOOP.
END FILE.
END INPUT PROGRAM.
DATASET NAME LeaveOneOut.
*Merging in original data.
MATCH FILES FILE = *
/TABLE = !QUOTE(!3)
/BY Id.
#MATCH FILES FILE = *
# /TABLE = 'Sim'
# /BY Id.
*Set missing to
IF L1O = Id !2 = $SYSMIS.
SORT CASES BY L1O.
SPLIT FILE BY L1O.
*You can replace the logistic regression with whatever procedure you are.
*interested in.
LOGISTIC REGRESSION VARIABLES !2
/METHOD=ENTER !4
/SAVE=PRED (pred) PGROUP (pgp)
/CRITERIA=PIN(.05) POUT(.10) ITERATE(20) CUT(.5).
SPLIT FILE OFF.
*This shows the original leave one out stats.
*And new stats are the same besides some floating.
*point differences (tested for linear regresison)
.
*COMPUTE Test = (CVFit2 - (PredAll-CVFit)).
TEMPORARY.
SELECT IF (L1O = Id).
*FREQ Y pgp.
CROSSTABS
/TABLES=YCOPY BY pgp
/FORMAT=AVALUE TABLES
/CELLS=COUNT
/COUNT ROUND CELL.
EXECUTE.
!ENDDEFINE.We can now run the macro
* ARGUMENTS ARE THE NUMBER OF ROWS OF DATA (SAMPLE SIZE), THE NAME OF THE OUTCOME VARIABLE AND A LIST OF PREDICTORS !LOOE 40 y %U:/My Documents/dummy.sav% x1 x2.
