Bootstrap the mean and median
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | SET PRINTBACK ON MPRINT ON . * Bootstrap the mean and the median. * Author: David Marso. * Raynald's note: 474 is the number of cases in the file. It is possible to let the syntax automatically determine this number (see bcnon.sps for instance). GET FILE='C:\\Program Files\\SPSS\\Employee data.sav'. BOOTMED NSAMP=1000 NSIZE = 474 BOOTVAR=SALARY OUTFILE=BOOTME . **GET THE BOOT STRAP FILE AND SMILE *. get file "BOOTME" . DEFINE BOOTMED ( NSAMP !TOKENS(1) / NSIZE !TOKENS(1) / BOOTVAR !TOKENS(1) / OUTFILE !TOKENS(1) ) . * STUFF IT ALL ***SORTED*** INTO ONE CASE * . sort cases by !BOOTVAR . vector data (!NSIZE). compute data($casenum)=!BOOTVAR . compute nobreak=1. aggregate outfile * / break nobreak / data1 to !CONCAT(data,!NSIZE) = max(data1 to !CONCAT(data,!NSIZE)). * DO ON THE FLY RANDOMIZATION AND STAT CALCULATION * . vector data=data1 to !CONCAT(data,!NSIZE) . vector tmp (!NSIZE). loop #P=1 to !NSAMP. * INITIALIZE A STORAGE ARRAY FOR EACH ITERATION * . loop #=1 to !NSIZE . compute tmp(#)=0. end loop. * INCREMENT STORAGE EACH TIME A CASE IS SAMPLED * . loop #I=1 to !NSIZE . compute ID=trunc(uni(!NSIZE )+1). compute tmp(ID)=TMP(ID)+1. end loop. **** IN THIS EXAMPLE I CREATE THE MEDIAN AND MEAN**** . **** YOU MAY WISH TO USE A DIFFERENT FUNCTION ****. **** IN WHICH CASE PLUG AWAY!!! **** * CALCULATE THE CASE COUNT CONTAINING THE MEDIAN * . compute tot=0. compute crit=!NSIZE / 2. * LOOP UNTIL YOU FIND THE CRITICAL CASE * . loop #=1 to !NSIZE . compute tot=tot + tmp(#). end loop if tot >=crit . * CALCULATE THE MEDIAN * . do if mod(!NSIZE,2)=0 . compute median=(data(#) + data(#-1))/2. else. compute median=data(#). end if . *** COMPUTE MEAN USING SAME SAMPLE *** . compute MEAN=0 . loop # = 1 to !NSIZE . compute MEAN=MEAN + data(#) * tmp(#) . end loop. COMPUTE MEAN=MEAN / !NSIZE . * DUMP THE MEAN and MEDIAN TO The OUTFILE * . xsave outfile !QUOTE(!OUTFILE) / keep median MEAN . * DO IT AGAIN AND AGAIN AND AGAIN .... * . end loop . execute . !ENDDEFINE . |
Related pages
...