FAQ/kw - CBU statistics Wiki

Revision 6 as of 2006-08-15 15:42:11

Clear message
location: FAQ / kw

No procedure exists in SPSS for performing post-hoc pairwise group comparisons when the Kruskal-Wallis nonparametric test is used. Kruskal-Wallis compares three or more groups and is the nonparametric analogue of the on-way anova.

The below syntax implements one solution to this. To guards against false positive results the authors suggest only specific comparisons should be tested and only if the overall Kruskal-Wallis test is significant. (as in the LSD approach for t-tests).

As a further precaution Bonferroni, Sidak, Holm or Ryan adjustments to outputted p-values could be used. SPSS Syntax to perform these using uncorrected p-values as input is in the pipeline. For further details on these adjustments see the Graduate Statistics Seminar on post-hoc tests.

*
Example data set below
data list free/
 y group.

begin data
9 1
9 1
1 1
2 1
4 2
5 2
7  2
12 2
6 3
7 3
9 3
8 3
6 3
2 3
5 3
end data.

After opening a spreadsheet containing y (response) and group columns you can run the SPSS Macro syntax below: [ CUT AND PASTE INTO A SPSS SYNTAX WINDOW, SELECT ALL AND RUN; SPECIFY TWO GROUPS TO BE COMPARED IN LAST LINE]

 performs Sprent & Smeeton (2001) method of pairwise comparisons for kruskal-wallis test
*
* Data file containing just the two columns called y and group
* inputs are specified groups to being compared 
*
* BACK UP ALL DATA BEFORE USING THIS MACRO
*
* This test is only valid if the overall Kruskal-Wallis test is statistically significant
*
* results are saved to output.sav which appears in My Documents folder and are outputted
* in SPSS output window
* The Y and Group columns are backed up in a file called input.sav located in the My Documents folder
*
* reference Sprent, P. and Smeeton, NC (2001) Applied Nonparametric Statistical Methods.
* Chapman and Hall:London.

* inputs groups to be compared 

set errors=none.
set mprint=off.

define !kwpairs ( !pos !tokens(1)
                       / !pos !tokens(1)).

save file=input.sav /keep=y group.

RANK
  VARIABLES=y  (A)   /RANK /PRINT=YES
  /TIES=MEAN .
compute rysq=ry*ry.
exe.

aggregate outfile=* 
 /break=group
 /sumy sumysq= sum(ry rysq)
 /nsize=n(ry).

compute mrk = sumy/nsize.
compute suma = sumy*sumy/nsize.
compute con=1.
exe.

* input which two groups to be compared

if (group=!1) m1=sumy/nsize.
if (group=!2) m2=sumy/nsize.
if (group=!1) nx=nsize.
if (group=!2) ny=nsize.
exe.

aggregate outfile=* 
 /break=con 
 /con2 ssq= sum(suma sumysq)
 /ng=max(group)
 /mn1 mn2 = first(m1 m2)
 /n1 n2 = first(nx ny)
 /ntot=sum(nsize).

compute #con1=ntot*(ntot+1)*(ntot+1)/4.
compute #con3= (ntot-1)*(con2-#con1).
compute #con4= #con3/(ssq-#con1).
compute #con5=(ntot-1-#con4)*(n1+n2).
compute con6=(ssq-#con1)*(#con5).
compute con7=(n1*n2)*(ntot-ng)*(ntot-1).

* Just use two-tailed uncorrected t
* Given overall K-Wallis is statistically significant ie LSD equivalent

compute twot=2.

compute cd025= mn1-mn2 + idf.t(0.05/twot,ntot-ng)*sqrt(con6/con7).
compute cd975= mn1-mn2 - idf.t(0.05/twot,ntot-ng)*sqrt(con6/con7).
compute pv = cdf.t(-abs(mn1-mn2) / sqrt(con6/con7),ntot-ng).

save outfile=output.sav /keep = mn1 mn2 cd025 cd975 pv.
exe.

get file=output.sav.
compute odiff=abs(mn1 - mn2).
exe.

formats all (f11.8).
variable labels mn1 'Mean Rank Group 1' /mn2 'Mean Rank Group 2' / odiff 'Observed difference' /pv ' Uncorrected Two-tailed P-Value'
         / cd025 '95% CI L ' / cd975 '95% CI U'.

report format=list automatic align(center)
  /variables=mn1 mn2 odiff pv cd025 cd975
  /title "Pairwise comparisons using Bonferroni adjustments for method"
         "of Sprent and Smeeton (2001) for Kruskal-Wallis test"
         " 95% Confidence Interval for difference in means ranks in (L,U)"
         "                                                                                          "
         " NB: This test is only valid if the Kruskal-Wallis test is statistically"
         " significant overall using all the groups".

!enddefine.

!kwpairs 2 3.
  • [:FAQ/kw/means:SPSS syntax to output raw group means corresponding to mean ranks just obtained by !kwpairs]