set printback on /mxloop=120000. /* ------------------------------------------------ */ /* MEDIAN (version 1.1) */ /* */ /* The syntax of MEDIAN is */ /* */ /* MEDIAN VAR=variable */ /* [/GROUPS=variable] */ /* [/METHOD={1} */ /* {2} */ /* {3} */ /* {4**} */ /* {5} */ /* {6}] */ /* [/OUTFILE='file name']. */ /* */ /* where the number of METHOD refers to the methods */ /* described in the documention file MEDIAN.TXT. */ /* */ /* WARNING: */ /* */ /* The macro MEDIAN will erase the files */ /* DUMMYTMP.VAR and DUMMYTMP.GRP */ /* if they exist already in the current directory. */ /* The macro MEDIAN will also delete the variables */ /* DUMMYTMP and DUMMYGRP */ /* if they exist in the current working file of */ /* SPSS. */ /* */ /* A documentation of MEDIAN.SPS (description etc.) */ /* (MEDIAN.TXT) can be found at: */ /* http://www2.jura.uni-hamburg.de/instkrim/kriminologie/Mitarbeiter/Enzmann/Software/Enzmann_Software.html */ /* ------------------------------------------------ */. PRESERVE. set printback off. define MEDIAN ( var !CharEnd('/') /Groups !default('') !CharEnd('/') /Method !default(4) !CharEnd('/') /outfile !default('') !CMDEnd). compute dummytmp=1. !IF (!Length(!groups)=0) !THEN. !LET !temp=!var. AGGREGATE /OUTFILE='dummytmp.var' /BREAK=!var /freq = N(dummytmp). compute dummytmp=1. compute dummygrp=1. temporary. select if $casenum=1. AGGREGATE /OUTFILE='dummytmp.grp' /BREAK=!temp dummygrp /freq = N(dummytmp). !LET !subgrp=0. !ELSE. AGGREGATE /OUTFILE='dummytmp.var' /BREAK=!var !groups /freq = N(dummytmp). compute dummytmp=1. compute dummygrp=1. AGGREGATE /OUTFILE='dummytmp.grp' /BREAK=!groups dummygrp /freq = N(dummytmp). !LET !subgrp=1. !IFEND. !IF (!Length(!outfile)=0) !THEN !LET !outf='none'. !LET !file=0. !ELSE !let !outf=!outfile. !LET !file=1. !IFEND. compute dummytmp=1. match files /file=* /drop dummytmp dummygrp. matrix. get miss /file='dummytmp.var' /missing=accept /sysmis=999. do if ncol(miss)=2. + get freqtab /file='dummytmp.var' /variables=!var freq /names=name /missing omit. + compute groups=0. else. + get freqtab /file='dummytmp.var' /variables=!var freq !groups /names=name /missing omit. + compute groups=1. end if. compute n = csum(freqtab)(2). do if ncol(miss)=2. + compute nmiss=csum(miss)(2)-n. else. + compute nmiss=csum(miss)(3)-n. end if. release miss. * print {ncol(freqtab)}. do if groups. get grptab /file='dummytmp.grp' /variables=!groups dummygrp /missing omit. compute grptab=grptab(:,1). compute ngrp=nrow(grptab). compute group_n=make(ngrp,2,0). compute ngroups=1. loop #j=1 to ngrp. + compute flag=0. + loop #i=1 to nrow(freqtab). - do if freqtab(#i,3)=grptab(#j). + compute flag=1. + compute group_n(ngroups,2)=group_n(ngroups,2)+freqtab(#i,2). - end if. + end loop. + do if flag=1. - compute group_n(ngroups,1)=grptab(#j). - compute ngroups=ngroups+1. + end if. end loop. release grptab,ngrp,flag. compute ngroups=ngroups-1. compute group_n=group_n(1:ngroups,:). * print group_n. compute result1=make(1,5,0). compute result2=make(1,4,0). loop #j=1 to ngroups. do if (!method=1) or (!method=3) or (!method=4). + compute j25 = trunc(group_n(#j,2)*0.25). + compute g25 = group_n(#j,2)*0.25-j25. + compute j50 = trunc(group_n(#j,2)*0.50). + compute g50 = group_n(#j,2)*0.50-j50. + compute j75 = trunc(group_n(#j,2)*0.75). + compute g75 = group_n(#j,2)*0.75-j75. else if !method=2. + compute j25 = trunc((group_n(#j,2)+1)*0.25). + compute g25 = (group_n(#j,2)+1)*0.25-j25. + compute j50 = trunc((group_n(#j,2)+1)*0.50). + compute g50 = (group_n(#j,2)+1)*0.50-j50. + compute j75 = trunc((group_n(#j,2)+1)*0.75). + compute g75 = (group_n(#j,2)+1)*0.75-j75. else if !method=5. + compute j25 = trunc((group_n(#j,2)-1)*0.25). + compute g25 = (group_n(#j,2)-1)*0.25-j25. + compute j50 = trunc((group_n(#j,2)-1)*0.50). + compute g50 = (group_n(#j,2)-1)*0.50-j50. + compute j75 = trunc((group_n(#j,2)-1)*0.75). + compute g75 = (group_n(#j,2)-1)*0.75-j75. else if !method=6. + compute j25 = trunc(group_n(#j,2)*0.25+0.5). + compute j50 = trunc(group_n(#j,2)*0.50+0.5). + compute j75 = trunc(group_n(#j,2)*0.75+0.5). end if. compute cumfreq=0. compute cumproz=0. compute q1=0. compute md=0. compute q3=0. compute gfreqtab=make(1,2,0). loop #i=1 to nrow(freqtab). + do if freqtab(#i,3)=group_n(#j,1). - compute gfreqtab={gfreqtab;freqtab(#i,1:2)}. + end if. end loop. compute gfreqtab=gfreqtab(2:nrow(gfreqtab),:). * print gfreqtab. loop #i=1 to nrow(gfreqtab). + compute cumfreq=cumfreq+gfreqtab(#i,2). + compute cumproz=cumfreq/group_n(#j,2). do if (!method=1 or !method=2). + do if (cumfreq >= j25) and not q1. - do if (#i < nrow(gfreqtab) and j25+1 > cumfreq). + compute quart1 = (1-g25)*gfreqtab(#i,1)+g25*gfreqtab(#i+1,1). - else. + compute quart1 = (1-g25)*gfreqtab(#i,1)+g25*gfreqtab(#i,1). - end if. - compute q1=1. + end if. + do if (cumfreq >= j50) and not md. - do if (#i < nrow(gfreqtab) and j50+1 > cumfreq). + compute med = (1-g50)*gfreqtab(#i,1)+g50*gfreqtab(#i+1,1). - else. + compute med = (1-g50)*gfreqtab(#i,1)+g50*gfreqtab(#i,1). - end if. - compute md=1. + end if. + do if (cumfreq >= j75) and not q3. - do if (#i < nrow(gfreqtab) and j75+1 > cumfreq). + compute quart3 = (1-g75)*gfreqtab(#i,1)+g75*gfreqtab(#i+1,1). - else. + compute quart3 = (1-g75)*gfreqtab(#i,1)+g75*gfreqtab(#i,1). - end if. - compute q3=1. + end if. else if (!method=3). + do if (cumfreq >= j25) and not q1. - do if (#i < nrow(gfreqtab) and j25+1 > cumfreq and g25 > 0). + compute quart1 = gfreqtab(#i+1,1). - else. + compute quart1 = gfreqtab(#i,1). - end if. - compute q1=1. + end if. + do if (cumfreq >= j50) and not md. - do if (#i < nrow(gfreqtab) and j50+1 > cumfreq and g50 > 0). + compute med = gfreqtab(#i+1,1). - else. + compute med = gfreqtab(#i,1). - end if. - compute md=1. + end if. + do if (cumfreq >= j75) and not q3. - do if (#i < nrow(gfreqtab) and j75+1 > cumfreq and g75 > 0). + compute quart3 = gfreqtab(#i+1,1). - else. + compute quart3 = gfreqtab(#i,1). - end if. - compute q3=1. + end if. else if (!method=4). + do if (cumfreq >= j25) and not q1. - do if (#i < nrow(gfreqtab) and j25+1 > cumfreq and g25 > 0). + compute quart1 = gfreqtab(#i+1,1). - else if (#i < nrow(gfreqtab) and j25+1 > cumfreq). + compute quart1 = (gfreqtab(#i,1)+gfreqtab(#i+1,1))/2. - else. + compute quart1 = gfreqtab(#i,1). - end if. - compute q1=1. + end if. + do if (cumfreq >= j50) and not md. - do if (#i < nrow(gfreqtab) and j50+1 > cumfreq and g50 > 0). + compute med = gfreqtab(#i+1,1). - else if (#i < nrow(gfreqtab) and j50+1 > cumfreq). + compute med = (gfreqtab(#i,1)+gfreqtab(#i+1,1))/2. - else. + compute med = gfreqtab(#i,1). - end if. - compute md=1. + end if. + do if (cumfreq >= j75) and not q3. - do if (#i < nrow(gfreqtab) and j75+1 > cumfreq and g75 > 0). + compute quart3 = gfreqtab(#i+1,1). - else if (#i < nrow(gfreqtab) and j75+1 > cumfreq). + compute quart3 = (gfreqtab(#i,1)+gfreqtab(#i+1,1))/2. - else. + compute quart3 = gfreqtab(#i,1). - end if. - compute q3=1. + end if. else if (!method=5). do if (nrow(gfreqtab)=1 and not(q1 and md and q3)). + compute quart1=gfreqtab(1,1). + compute q1=1. + compute med=quart1. + compute md=1. + compute quart3=quart1. + compute q3=1. else. + do if (cumfreq >= j25) and not q1. - do if (#i+1 < nrow(gfreqtab) and j25+2 > cumfreq+gfreqtab(#i+1,2)). + compute quart1 = gfreqtab(#i+1,1)+ g25*(gfreqtab(#i+2,1)-gfreqtab(#i+1,1)). - else if (#i < nrow(gfreqtab) and j25+1 <= cumfreq and j25+2 > cumfreq). + compute quart1 = gfreqtab(#i,1)+ g25*(gfreqtab(#i+1,1)-gfreqtab(#i,1)). - else if (#i < nrow(gfreqtab) and j25+1 > cumfreq). + compute quart1 = gfreqtab(#i+1,1). - else. + compute quart1 = gfreqtab(#i,1). - end if. - compute q1=1. + end if. + do if (cumfreq >= j50) and not md. - do if (#i+1 < nrow(gfreqtab) and j50+2 > cumfreq+gfreqtab(#i+1,2)). + compute med = gfreqtab(#i+1,1)+ g50*(gfreqtab(#i+2,1)-gfreqtab(#i+1,1)). - else if (#i < nrow(gfreqtab) and j50+1 <= cumfreq and j50+2 > cumfreq). + compute med = gfreqtab(#i,1)+ g50*(gfreqtab(#i+1,1)-gfreqtab(#i,1)). - else if (#i < nrow(gfreqtab) and j50+1 > cumfreq). + compute med = gfreqtab(#i+1,1). - else. + compute med = gfreqtab(#i,1). - end if. - compute md=1. + end if. + do if (cumfreq >= j75) and not q3. - do if (#i+1 < nrow(gfreqtab) and j75+2 > cumfreq+gfreqtab(#i+1,2)). + compute quart3 = gfreqtab(#i+1,1)+ g75*(gfreqtab(#i+2,1)-gfreqtab(#i+1,1)). - else if (#i < nrow(gfreqtab) and j75+1 <= cumfreq and j75+2 > cumfreq). + compute quart3 = gfreqtab(#i,1)+ g75*(gfreqtab(#i+1,1)-gfreqtab(#i,1)). - else if (#i < nrow(gfreqtab) and j75+1 > cumfreq). + compute quart3 = gfreqtab(#i+1,1). - else. + compute quart3 = gfreqtab(#i,1). - end if. - compute q3=1. + end if. end if. else if (!method=6). + do if (cumfreq >= j25) and not q1. - compute quart1 = gfreqtab(#i,1). - compute q1=1. + end if. + do if (cumfreq >= j50) and not md. - compute med = gfreqtab(#i,1). - compute md=1. + end if. + do if (cumfreq >= j75) and not q3. - compute quart3 = gfreqtab(#i,1). - compute q3=1. + end if. end if. end loop if cumproz >= 0.75. compute result1={result1; gfreqtab(1,1),quart1,med,quart3, gfreqtab(nrow(gfreqtab),1)}. compute result2={result2;gfreqtab(nrow(gfreqtab),1)-gfreqtab(1,1), quart3-quart1,(quart3-quart1)/2,group_n(#j,2)}. end loop. compute result1=result1(2:nrow(result1),:). compute result2=result2(2:nrow(result2),:). end if. do if (!method=1) or (!method=3) or (!method=4). + compute j25 = trunc(n*0.25). + compute g25 = n*0.25-j25. + compute j50 = trunc(n*0.50). + compute g50 = n*0.50-j50. + compute j75 = trunc(n*0.75). + compute g75 = n*0.75-j75. else if !method=2. + compute j25 = trunc((n+1)*0.25). + compute g25 = (n+1)*0.25-j25. + compute j50 = trunc((n+1)*0.50). + compute g50 = (n+1)*0.50-j50. + compute j75 = trunc((n+1)*0.75). + compute g75 = (n+1)*0.75-j75. else if !method=5. + compute j25 = trunc((n-1)*0.25). + compute g25 = (n-1)*0.25-j25. + compute j50 = trunc((n-1)*0.50). + compute g50 = (n-1)*0.50-j50. + compute j75 = trunc((n-1)*0.75). + compute g75 = (n-1)*0.75-j75. else if !method=6. + compute j25 = trunc(n*0.25+0.5). + compute j50 = trunc(n*0.50+0.5). + compute j75 = trunc(n*0.75+0.5). end if. compute cumfreq=0. compute q1=0. compute md=0. compute q3=0. loop #i=1 to nrow(freqtab). + compute cumfreq=cumfreq+freqtab(#i,2). + compute cumproz=cumfreq/n. do if (!method=1 or !method=2). + do if (cumfreq >= j25) and not q1. - do if (#i < nrow(freqtab) and j25+1 > cumfreq). + compute quart1 = (1-g25)*freqtab(#i,1)+g25*freqtab(#i+1,1). - else. + compute quart1 = (1-g25)*freqtab(#i,1)+g25*freqtab(#i,1). - end if. - compute q1=1. + end if. + do if (cumfreq >= j50) and not md. - do if (#i < nrow(freqtab) and j50+1 > cumfreq). + compute med = (1-g50)*freqtab(#i,1)+g50*freqtab(#i+1,1). - else. + compute med = (1-g50)*freqtab(#i,1)+g50*freqtab(#i,1). - end if. - compute md=1. + end if. + do if (cumfreq >= j75) and not q3. - do if (#i < nrow(freqtab) and j75+1 > cumfreq). + compute quart3 = (1-g75)*freqtab(#i,1)+g75*freqtab(#i+1,1). - else. + compute quart3 = (1-g75)*freqtab(#i,1)+g75*freqtab(#i,1). - end if. - compute q3=1. + end if. else if (!method=3). + do if (cumfreq >= j25) and not q1. - do if (#i < nrow(freqtab) and j25+1 > cumfreq and g25 > 0). + compute quart1 = freqtab(#i+1,1). - else. + compute quart1 = freqtab(#i,1). - end if. - compute q1=1. + end if. + do if (cumfreq >= j50) and not md. - do if (#i < nrow(freqtab) and j50+1 > cumfreq and g50 > 0). + compute med = freqtab(#i+1,1). - else. + compute med = freqtab(#i,1). - end if. - compute md=1. + end if. + do if (cumfreq >= j75) and not q3. - do if (#i < nrow(freqtab) and j75+1 > cumfreq and g75 > 0). + compute quart3 = freqtab(#i+1,1). - else. + compute quart3 = freqtab(#i,1). - end if. - compute q3=1. + end if. else if (!method=4). + do if (cumfreq >= j25) and not q1. - do if (#i < nrow(freqtab) and j25+1 > cumfreq and g25 > 0). + compute quart1 = freqtab(#i+1,1). - else if (#i < nrow(freqtab) and j25+1 > cumfreq). + compute quart1 = (freqtab(#i,1)+freqtab(#i+1,1))/2. - else. + compute quart1 = freqtab(#i,1). - end if. - compute q1=1. + end if. + do if (cumfreq >= j50) and not md. - do if (#i < nrow(freqtab) and j50+1 > cumfreq and g50 > 0). + compute med = freqtab(#i+1,1). - else if (#i < nrow(freqtab) and j50+1 > cumfreq). + compute med = (freqtab(#i,1)+freqtab(#i+1,1))/2. - else. + compute med = freqtab(#i,1). - end if. - compute md=1. + end if. + do if (cumfreq >= j75) and not q3. - do if (#i < nrow(freqtab) and j75+1 > cumfreq and g75 > 0). + compute quart3 = freqtab(#i+1,1). - else if (#i < nrow(freqtab) and j75+1 > cumfreq). + compute quart3 = (freqtab(#i,1)+freqtab(#i+1,1))/2. - else. + compute quart3 = freqtab(#i,1). - end if. - compute q3=1. + end if. else if (!method=5). do if (nrow(freqtab)=1 and not(q1 and md and q3)). + compute quart1=freqtab(1,1). + compute q1=1. + compute med=quart1. + compute md=1. + compute quart3=quart1. + compute q3=1. else. + do if (cumfreq >= j25) and not q1. - do if (#i+1 < nrow(freqtab) and j25+2 > cumfreq+freqtab(#i+1,2)). + compute quart1 = freqtab(#i+1,1)+ g25*(freqtab(#i+2,1)-freqtab(#i+1,1)). - else if (#i < nrow(freqtab) and j25+1 <= cumfreq and j25+2 > cumfreq). + compute quart1 = freqtab(#i,1)+ g25*(freqtab(#i+1,1)-freqtab(#i,1)). - else if (#i < nrow(freqtab) and j25+1 > cumfreq). + compute quart1 = freqtab(#i+1,1). - else. + compute quart1 = freqtab(#i,1). - end if. - compute q1=1. + end if. + do if (cumfreq >= j50) and not md. - do if (#i+1 < nrow(freqtab) and j50+2 > cumfreq+freqtab(#i+1,2)). + compute med = freqtab(#i+1,1)+ g50*(freqtab(#i+2,1)-freqtab(#i+1,1)). - else if (#i < nrow(freqtab) and j50+1 <= cumfreq and j50+2 > cumfreq). + compute med = freqtab(#i,1)+ g50*(freqtab(#i+1,1)-freqtab(#i,1)). - else if (#i < nrow(freqtab) and j50+1 > cumfreq). + compute med = freqtab(#i+1,1). - else. + compute med = freqtab(#i,1). - end if. - compute md=1. + end if. + do if (cumfreq >= j75) and not q3. - do if (#i+1 < nrow(freqtab) and j75+2 > cumfreq+freqtab(#i+1,2)). + compute quart3 = freqtab(#i+1,1)+ g75*(freqtab(#i+2,1)-freqtab(#i+1,1)). - else if (#i < nrow(freqtab) and j75+1 <= cumfreq and j75+2 > cumfreq). + compute quart3 = freqtab(#i,1)+ g75*(freqtab(#i+1,1)-freqtab(#i,1)). - else if (#i < nrow(freqtab) and j75+1 > cumfreq). + compute quart3 = freqtab(#i+1,1). - else. + compute quart3 = freqtab(#i,1). - end if. - compute q3=1. + end if. end if. else if (!method=6). + do if (cumfreq >= j25) and not q1. - compute quart1 = freqtab(#i,1). - compute q1=1. + end if. + do if (cumfreq >= j50) and not md. - compute med = freqtab(#i,1). - compute md=1. + end if. + do if (cumfreq >= j75) and not q3. - compute quart3 = freqtab(#i,1). - compute q3=1. + end if. end if. end loop if cumproz >= 0.75. do if groups. + print {name(1);name(3)} /format A8 /title 'Median and Dispersion Measures:' /rlabels ' Var =' 'Group ='. else. + print {name(1)} /format A8 /title 'Median and Dispersion Measures:' /rlabels 'Var = '. end if. do if !method=1. + print /title 'Method: Weighted average centered at x_np'. else if !method=2. + print /title 'Method: Weighted average centered at x_(n+1)p'. else if !method=3. + print /title 'Method: Empirical distribution function'. else if !method=4. + print /title 'Method: Empirical distribution function with averaging'. else if !method=5. + print /title 'Method: Empirical distribution function with interpolation'. else if !method=6. + print /title 'Method: Closest observation'. end if. do if groups. print {group_n(:,1),result1} /title ' ' /format F12.3 /clabels 'GROUP' 'MIN' 'Q1' 'MEDIAN' 'Q3' 'MAX'. print {group_n(:,1),result2} /title ' ' /format F12.3 /clabels 'GROUP' 'RANGE' 'IQR' 'SEMI-IQR' 'VALID N'. print {freqtab(1,1),quart1,med,quart3,freqtab(nrow(freqtab),1)} /title 'TOTAL:' /clabels 'MIN' 'Q1' 'MEDIAN' 'Q3' 'MAX' 'N' /format F12.3. do if !file. + save {group_n(:,1),result1,result2} /outfile=!outf /variables=group min Q1 MED Q3 max range IQR semi_IQR valid_N. end if. else. print {freqtab(1,1),quart1,med,quart3,freqtab(nrow(freqtab),1)} /title ' ' /clabels 'MIN' 'Q1' 'MEDIAN' 'Q3' 'MAX' /format F12.3. end if. print {freqtab(nrow(freqtab),1)-freqtab(1,1), quart3-quart1,(quart3-quart1)/2,n,nmiss} /title ' ' /clabels 'RANGE' 'IQR' 'SEMI-IQR' 'VALID N' 'MISSING' /format F12.3. do if !file and not groups. + save {name(1),freqtab(1,1),quart1,med,quart3,freqtab(nrow(freqtab),1),freqtab(nrow(freqtab),1)-freqtab(1,1),quart3-quart1,(quart3-quart1)/2,n,nmiss} /outfile=!outf /variables=var min Q1 MED Q3 max range IQR semi_IQR valid_N n_miss /strings=var. end if. do if groups. + print /title 'Note: The next table will ignore a temporary selection of cases.'. end if. end matrix. !IF (!subgrp=1) !THEN. temporary. select if not missing(!var). freq !groups. !IFEND. erase file='dummytmp.var'. erase file='dummytmp.grp'. !enddefine. restore.