Kruskal-Wallis test
Compare three or more separate groups on an ordinal or quantitative variable without assuming normality – the nonparametric alternative to one-way analysis of variance: all the scores are ranked together, and the test checks whether the ranks in at least one group are systematically higher or lower. The output: the table of ranks and medians, the test table as in SPSS (H, df, p), the table of pairwise comparisons with Bonferroni correction and rrb for each pair, a boxplot and a report sentence in English.
Loading the data
Paste columns directly from the sheet (including the header row) – select them in Excel, copy, and paste here:
Variable selection
When to use?
When there are three or more groups of different participants and one variable to compare across them – and one-way analysis of variance is not appropriate. Gravetter and Wallnau (2017, Appendix E.4) present Kruskal-Wallis as an alternative to the one-way analysis of variance of chapter 12: "analysis of variance requires numerical scores from which means and variances are computed; Kruskal-Wallis requires only that the participants can be ranked" – and it is the extension of Mann-Whitney from two groups to three or more. The groups can be treatment conditions (children who draw alone, in a group, or with supervision – and all the drawings are ranked together for creativity) or existing populations (5-, 6- and 7-year-olds). Field (2024, section 7.6) presents it as the test for "several independent groups" alongside the Friedman test for repeated measures, in the example of soya meals and sperm counts: four groups of 20 men, and checking the data (section 7.6.3) shows in every group a Q-Q plot that deviates from the diagonal, a significant Shapiro-Wilk (except for one group), and a significant Levene – "a sad story: probably not normal and probably unequal variances". Pallant (2020, chapter 16): "the nonparametric alternative to a one-way between-groups analysis of variance… the scores are converted to ranks, and the mean rank of each group is compared" – "does the level of optimism differ across three age groups?".
On this site it is the natural place for two situations: a single rating item (Likert 1-5), which by the site's convention is an ordinal variable and has no mean (Variables and measurement scales) – a comparison across several groups is done here, or in Frequencies and chi-square; and a scale score or quantitative variable in small groups or with outliers, when the normality check (Normality check, by group) or the boxplot shows a distribution that is not reasonable for analysis of variance. When all groups are large (30 or more) and the data are quantitative, one-way analysis of variance is the default – Gravetter and Wallnau: "if the data are appropriate for an ANOVA or one of the t tests, the standard test is preferable to the alternative for ordinal data" (Appendix E.1) – and Kruskal-Wallis supports it or replaces it at your discretion; the "Reading the result" box tells you which situation you are in. Only two groups – Mann-Whitney test (the same p, plus U, an exact p and two effect-size measures); the same participants in several measurements – the Friedman test (Field, Section 7.7), not on this page.
The logic: ranks and H
As in Mann-Whitney, all the scores are ranked together from lowest (rank 1) to highest "ignoring the group to which the score belongs" (Field, 2024, Section 7.6.1), and tied values receive the mean of their ranks (Section 7.4.1; Gravetter & Wallnau, 2017, Appendix E.1). Then "the scores are collected back into their groups" and the ranks in each group are summed – Ri in Field, T in Gravetter and Wallnau. If there is no difference, the high and low ranks are scattered across the groups and the mean ranks are similar to the overall mean rank (N + 1)/2; if one group is higher, the high ranks concentrate in it. The statistic measures the spread of the rank sums: H = 12/(N(N + 1)) · Σ(Ri²/ni) − 3(N + 1) (equation 7.26; Appendix E.4 – the same formula with T). In the soya example the rank sums are 927, 883, 883 and 547 (the group that ate soya seven times a week – the lowest), and H = 251.66 − 243 = 8.659 (equation 7.27); in example E.2 of Gravetter and Wallnau (three groups of five, T = 39.5, 26.5, 54) – H = 3.785. The data of both examples are among the page's tests.
Significance. H is approximately chi-square distributed with df = k − 1 – the number of groups minus one (Field, Section 7.6.1; Gravetter & Wallnau, Appendix E.4). For 4 groups df = 3 and H = 8.659 gives p = .034; for 3 groups the critical value at .05 is 5.99, and 3.785 does not exceed it – "the data do not provide sufficient evidence of a difference among the three treatments". The approximation requires at least five observations in each group (Gravetter & Wallnau). SPSS corrects H for ties – the footnote "The test statistic is adjusted for ties" in Pallant's output – by dividing by C = 1 − Σ(t³ − t)/(N³ − N); the textbook formula does not correct, and the difference in example E.2 (two repeated values) is 3.785 versus 3.819. The page prints the corrected value, and in the table note also the uncorrected one.
How to read the output?
Table 1 – ranks and medians. The SPSS Ranks table (N, Mean Rank) plus the rank sum, the median, the quartiles and the range of each group. The first thing to read: the mean rank – Pallant (2020, chapter 16): "check the mean rank of the groups… this tells you which group had the highest overall ranking, which corresponds to the highest score"; in her example the oldest age group (45+) has the highest mean rank – "the highest optimism", and the youngest – the lowest. In the soya example: 46.35, 44.15, 44.15 and 27.35 – three similar groups and one low one. The median is what is reported instead of the mean (Field, 2024, section 7.6.8; Pallant produces it in a separate step with Means: 22, 22 and 23).
Table 2 – the test. In the new SPSS output: Total N, Test Statistic (this is H; Pallant: "chi-square, shown as Test Statistic"), Degree of Freedom and Asymptotic Sig. (2-sided test); in the old output – Chi-Square, df, Asymp. Sig.. The rule (Field, Cramming Sam's tips for chapter 7): "look at the Asymptotic Sig. row – a value of .05 or less is usually taken as a significant difference between the groups". In the soya example H(3) = 8.66, p = .034 – "people would usually conclude that the number of soya meals significantly affects sperm counts"; in Pallant χ²(2, n = 435) = 8.57, p = .014. But the overall test "tells us that sperm counts differed in some of the groups, but not which groups" (Field, Section 7.6.5) – which is what the next table is for.
The boxplot is what the new SPSS output attaches to the test (output 7.6): Field reads from it that the box of the seven-meal group is smaller ("the scores are more compressed") and that its median "looks slightly lower than the others" – "perhaps the difference lies between the seven-meal group and the rest?" – and the outliers (the circles above the whiskers) are "a few men who produced a particularly wild amount".
Pairwise comparisons – Table 3
"The simplest way to break down the overall effect is to compare all pairs of groups" (Field, 2024, Section 7.6.2) – with 4 groups, six pairs. Six Mann-Whitney tests could be run, but "doing many tests on the same data inflates the familywise error rate" (section 2.10.8) – so the p of each comparison is corrected so that across all the tests together the risk of a Type I error stays at 5%. The table the page prints is the SPSS Pairwise Comparisons table (output 7.7; in Pallant – Pairwise Comparisons of age 3 groups): for each pair the statistic is the difference in mean ranks – "for 7 versus 1 meals 44.15 − 27.35 = 16.80" – its standard error, the z (Std. Test Statistic: "the differences are converted to z-scores by dividing by the standard error, and from these the p"; for 7 versus 1: z = 2.286, p = .022), and two p values: Sig. and Adj. Sig. – the p multiplied by the number of comparisons (Bonferroni correction, the footnote in the table), up to 1. Field: "the Adj. Sig. column contains the adjusted p-values, and these are the ones we interpret (no matter how tempting it is to interpret the Sig. column)". In the soya example no adjusted value is below .05 – "although the comparison between seven meals and none comes close, p = .058, which reminds us that significance tests encourage black-and-white thinking, and effect sizes may help": "despite the significant overall effect, no specific comparison indicates a significant difference". Pallant, by contrast, scans the Sig. column ("look for values below .05") and finds one significant comparison, the youngest versus the oldest – although in her table Sig. = .004 and Adj. Sig. = .011 (both significant; her text prints .002). The site's convention: read the adjusted p, like Field and like SPSS; the table also prints the unadjusted one. These comparisons are the test known as Dunn's test – that is what it is called in R (dunn_test), and SPSS does not name it.
SPSS also offers a stepwise procedure (Stepwise step-down, output 7.8): the groups are ordered by rank sum, and at each step the next group is tested for being "equivalent" to the previous ones; the result is homogeneous subsets, with a less severe correction – in the soya example the seven-meal group alone, and the other three together (p = .943). Field: "these procedures are quite complicated, don't worry if you don't fully understand them". The page does not compute it (see "What the page does not compute").
Effect size – rrb for each pair
"There is no easy way to convert a Kruskal-Wallis statistic with more than one degree of freedom into an effect size r. But such an effect size would not be very useful anyway (because it summarizes a general effect). In most cases it is more interesting to know the effect size of a focused comparison" (Field, 2024, Section 7.6.7) – so Field computes an effect size for each pair: the U of a Mann-Whitney test on the two groups, and from it rrb = 1 − 2U/(n1n2) (equation 7.11; table 7.4). In the soya example the effects are "substantial (and similar in size) for seven meals versus all the other groups, around .5, but trivial (rrb < .1) for the comparisons among the other groups" – .48, .39 and .44 versus .04, .06 and .02. To obtain the six U values in SPSS, Field runs six NPAR TESTS /M-W in syntax ("until we died of boredom – luckily, syntax is your friend"); the page computes them directly and prints them in Table 3. The sign indicates direction: positive – the first group in the pair is higher (in Field's table 7.4 the values are printed without a sign); and the "Reading the result" box translates it into the percentage of supporting pairs (Kerby, as cited in Field) and into a word on Cohen's yardstick (1988, pp. 79-83: .1 small, .3 medium, .5 large – as on the Mann-Whitney page). Pallant gives no effect size for this test – in her report the medians are the measure.
Assumptions and warnings
Two general assumptions (Pallant, 2020, Chapter 16): random sampling, and independent observations – "a between-groups analysis, so different people in each group"; the same participants in several conditions – the Friedman test. Five observations per group. "One assumption… to justify the use of the chi-square distribution: each condition must contain at least five scores" (Gravetter & Wallnau, 2017, Appendix E.4); the page warns, and SPSS offers an exact p in the Exact Tests module. Power. Field (box 7.1): ranks lose information about the size of the differences, so when the assumptions of the parametric test hold it is more powerful; and when they do not hold, the claim "nonparametric tests have less power" is not defined. Preliminary check. Field (section 7.6.3) checks normality by group before the test (Q-Q plots, and in small groups also Shapiro-Wilk – "if the test detected a deviation in such a small sample, it is probably substantial") – the Normality check page, by group.
Missing values. A participant is included when they have a valid value on the dependent variable and on the grouping variable – analysis-by-analysis deletion (/MISSING ANALYSIS). Multiple tests. Several variables in one click are several overall tests, each with a 5% risk of a Type I error; the correction in Table 3 is within each test only. What the page does not compute. The stepwise procedure (Stepwise step-down), the median test (Field's tip 7.3), the Jonckheere-Terpstra trend test (section 7.6.6: "when you expect the medians to rise or fall in the order of the groups" – in the soya example J = 912, z = −2.48, p = .013), exact and Monte Carlo p, an overall effect size (eta squared of H – not in any of the sources), and the Friedman test for repeated measures.
And how do you do it in SPSS?
The new way (Pallant, 2020, Chapter 16; Field, 2024, Section 7.6.4): Analyze › Nonparametric Tests › Independent Samples… – on the Objective tab select Customize analysis; on Fields – the dependent variable to Test Fields and the categorical variable to Groups; on Settings – Customize tests, Kruskal-Wallis 1-way ANOVA, and under Multiple comparisons – All pairwise (or Stepwise step-down); before that, Pallant: in Edit › Options › Output select Pivot tables and charts. The output: Hypothesis Test Summary (the decision only), and a double click opens the Independent-Samples Kruskal-Wallis Test Summary with the boxplot and the Pairwise Comparisons table. The medians are not in the output – Pallant produces them with Analyze › Compare Means › Means with Median under Cell Statistics.
The syntax the page generates includes both ways: the old one (Analyze › Nonparametric Tests › Legacy Dialogs › K Independent Samples…, with Define Range for the codes), which prints Ranks and Test Statistics as in Tables 1-2 and also runs in PSPP; and Pallant's new one, which adds the pairwise comparisons (Table 3):
NPAR TESTS /K-W=attitude_score BY education(1 4) /MISSING ANALYSIS. NPTESTS /INDEPENDENT TEST (attitude_score) GROUP (education) KRUSKAL_WALLIS(COMPARE=PAIRWISE) /MISSING SCOPE=ANALYSIS USERMISSING=EXCLUDE /CRITERIA ALPHA=0.05 CILEVEL=95. MEANS TABLES=attitude_score BY education /CELLS=COUNT MEDIAN.
Reading the output. Ranks = Table 1 (N, Mean Rank); Test Statistics = Table 2 (Chi-Square = H corrected for ties, df, Asymp. Sig.); Pairwise Comparisons = Table 3 (Test Statistic, Std. Error, Std. Test Statistic, Sig., Adj. Sig. – note that SPSS orders the pairs by mean rank, and the sign of the statistic depends on the version: positive in Field's output, negative in Pallant's; the page prints first minus second in the order of the levels); MEANS – the medians. The syntax was checked against PSPP (the free implementation): mean ranks, H, df and p identical to Tables 1-2; NPTESTS does not exist in PSPP.
And how do you do it in R?
In base R, kruskal.test(y, g) gives H ("Kruskal-Wallis chi-squared", corrected for ties like SPSS), df and p. The SPSS pairwise comparisons are given by dunn_test from the rstatix package with p.adjust.method = "bonferroni" – the same z and p (the sign of z there is second minus first); and rrb is computed from wilcox.test for each pair (R's W is n1n2 − U1). The script was run as is on the sample data file and gave the table values (in the comments). The site rule for R: a UTF-8 file, every comment line starts with an English word, and a UTF-8 environment for Hebrew names.
# Kruskal-Wallis test – Professor Stat. This file is saved as UTF-8; every comment line starts with an English word
library(readxl) # reading Excel
library(rstatix) # Dunn pairwise comparisons with Bonferroni correction (like NPTESTS in SPSS)
# Loading: the standard template – row = participant, column = variable; the sheet "data"
d <- read_excel("C:/Rdata/DATA_kw_example_eng.xlsx", sheet = "data")
ok <- !is.na(d[["attitude_score"]]) & !is.na(d[["education"]])
y <- d[["attitude_score"]][ok]; g <- factor(d[["education"]][ok])
# Median and quartiles (type = 6 = SPSS) and n for each group – Table 1
do.call(rbind, tapply(y, g, function(v) c(n = length(v), Mdn = median(v), quantile(v, c(.25, .75), type = 6))))
# n 17/18/50/32; Mdn 3.38, 3.31, 3.06, 3.50
# Ranks: all values ranked together (mean rank for ties); sum and mean rank for each group
r <- rank(y); cbind(sumRank = tapply(r, g, sum), meanRank = tapply(r, g, mean)) # 68.94, 57.94, 49.16, 69.69
# The test: H with tie correction (like SPSS), df = k − 1, p from chi-square – Table 2
kruskal.test(y, g) # chi-squared = 8.884, df = 3, p = .031
# Pairwise comparisons: z, p and Bonferroni-adjusted p – Table 3
dunn_test(data.frame(y = y, g = g), y ~ g, p.adjust.method = "bonferroni") # 3 vs 4: z = 2.68, p = .007, adjusted .045
# Effect size for each pair – r_rb = 1 − 2U1/(n1n2) (Field, 2024, table 7.4); W of wilcox.test = n1n2 − U1
a <- y[g == "3"]; b <- y[g == "4"]
U1 <- length(a) * length(b) - wilcox.test(a, b, exact = FALSE)$statistic
1 - 2 * U1 / (length(a) * length(b)) # −.345 (group 4 higher)
# Field's example (2024, table 7.3): four groups of 20 – H(3) = 8.66, p = .034; 7 vs 1 meals: z = 2.29, p = .022, adjusted .133
# Example E.2 of Gravetter and Wallnau: H = 3.785 without tie correction (the book); kruskal.test corrects for ties – 3.819
kruskal.test(list(c(14, 3, 21, 5, 16), c(2, 14, 9, 12, 5), c(26, 8, 14, 19, 20)))
Reporting the results
Field (2024, section 7.6.8): report the statistic H, the degrees of freedom and the significance – "sperm counts were significantly affected by eating soya meals, H(3) = 8.66, p = .034" – followed by the follow-up tests with effect sizes: "pairwise comparisons with adjusted p-values showed no significant differences… between seven meals and four (p = .133, rrb = .44), one (p = .133, rrb = .39) or none (p = .058, rrb = .48)…". Pallant (2020, chapter 16) adds n for each group and the medians: "a Kruskal-Wallis test revealed a significant difference in optimism levels across three age groups (group 1, n = 147: 18-29…), χ²(2, n = 435) = 8.57, p = .014. The older age group (45+) recorded a significantly higher median (Md = 23) than the youngest… (Md = 22)". Gravetter and Wallnau (Appendix E.4): "there is no standard format… the report should include a summary of the data, the chi-square value, df, N and p" – "H = 3.785 (2, N = 15), p > .05". The report sentences in the output combine all three ("both-and"): n and median for each group, H(df, N) = …, p, and after a significant overall test – the significant pairs with z, the adjusted p and rrb. The formatting rules (American Psychological Association [APA], 2020, Section 6.36): p and rrb without a zero before the decimal point; H and z with one.
Common mistakes
Stopping at the overall test. A significant H says there is a difference, not between which groups (Field, 2024, Section 7.6.2) – report the pairwise comparisons.
Reading Sig. instead of Adj. Sig. Six tests on the same data – without correction, "at least one Type I error in more than 5% of cases" (Field; Section 2.10.8). Pallant reads Sig.; the site's convention – the adjusted value.
Interpreting "no pair is significant" as "no effect". In the soya example the overall test is significant, no pair is, and rrb ≈ .5 for seven meals versus all the rest (Field, Section 7.6.7) – report the effect.
Reporting means. The test is on ranks; report medians (Field, Section 7.6.8; Pallant, 2020, Chapter 16).
Comparing the page's z with that of R or SPSS without checking the sign. The same absolute value; the sign depends on the order of the pair and on the software.
Groups of two or three participants. The chi-square approximation requires five per group (Gravetter & Wallnau, 2017, Appendix E.4).
Escaping to Kruskal-Wallis every time a normality test is significant. In large groups the central limit theorem covers analysis of variance (Field, 2024, section 6.2.7; the Normality check page) – and ranks lose the size of the differences (box 7.1).
References
American Psychological Association. (2020). Publication manual of the American Psychological Association (7th ed.). https://doi.org/10.1037/0000165-000
Cohen, J. (1988). Statistical power analysis for the behavioral sciences (2nd ed.). Lawrence Erlbaum Associates.
Field, A. (2024). Discovering statistics using IBM SPSS statistics (6th ed.). SAGE.
Gravetter, F. J., & Wallnau, L. B. (2017). Statistics for the behavioral sciences (10th ed.). Cengage Learning.
Pallant, J. (2020). SPSS survival manual: A step by step guide to data analysis using IBM SPSS (7th ed.). Open University Press.
Kruskal and Wallis (1952), Jonckheere (1954), Terpstra (1952) and Kerby (2014) – as cited in Field (2024). By the site rule, the reference list includes only sources read in the original; Gravetter and Wallnau (2017, Appendix E, pp. 687-697), Cohen (1988, pp. 79-83 – r conventions), Pallant (2020) and APA (2020) – by page, chapter and section of the print edition; Field (2024) in a reflowable digital copy – by section, equation, table and output (his table 7.3, 80 participants, is the page's test file). The syntax – verified against PSPP; R was run in 4.3.3 with rstatix 0.7.2.