The Kruskal-Wallis test answers one question: when you have three or more groups of people, do they all tend to score alike, or does at least one group score higher or lower? This guide explains how it works in plain language, how it is calculated, and how to run it in AnalyZ Solutions.
The Kruskal-Wallis test answers a simple question: when you have three or more groups of people, do they all tend to score alike, or does at least one group tend to score higher or lower than the others?
Suppose delivery times in hours are recorded for 25 deliveries from each of four depots. One depot looks faster and another slower, but every depot also has a few very long deliveries. Are the depots truly different, or is the gap down to which deliveries happened to be sampled? If they differ, which ones? The test gives you a way to tell.
The usual way to compare groups is to compare their averages, and the standard test for that is one-way ANOVA. It works well when the values spread evenly around each group's average in a roughly bell-shaped pattern. It works badly in three common situations.
Instead of the exact values, the test uses their order. It lines everyone from all the groups up from lowest to highest and gives each person a position number, called a rank. If the groups were really the same, each group's members would be spread evenly along the line, so every group would have about the same average position. The test checks whether the groups' average positions differ by more than luck would explain. A very large value is only the last in line, so it cannot drag the result around. For background on this whole family of tests, see What are non-parametric tests?
Suppose delivery times in hours are recorded for 25 deliveries from each of four depots. The times are heavily skewed, because a few deliveries take very long. You want to know three things. Do the depots differ? Which ones? And how large is the difference? The Kruskal-Wallis test answers all three.
Use it to compare three or more separate groups, meaning each person belongs to only one of them, when at least one of these is true:
It is a common choice for satisfaction ratings, income bands and count-like outcomes such as days absent.
When the values spread evenly around each group's average and the groups are moderate in size, one-way ANOVA is slightly better, because it uses the exact values. For two groups only, use the Mann-Whitney U test. For repeated measurements on the same people, use the Friedman test. For help choosing between this test and ANOVA, see What are non-parametric tests?
Three ideas make the test work: giving everyone a position number, comparing the groups' average positions, and following a significant result with comparisons between pairs of groups.
A rank is a position number. Put the values from every group into one list and number them from smallest to largest. If two or more values are equal, they share the average of the positions they occupy. Only the order matters, so the largest value gets the last rank, however large it is.
A group's mean rank is the average position number of its members. If the groups were equivalent, every group would have about the same mean rank, near the middle of the line. The test asks whether the mean ranks differ by more than luck would allow.
A significant result says that at least one group tends to differ from the others. It does not say which. Comparing pairs of groups finds out. Each comparison is adjusted for the number of pairs, because the more comparisons you run, the more likely it is that one of them looks significant by luck alone.
The calculation has four steps. The formulas are there for readers who want to check the arithmetic, and the worked example below follows every step with real numbers.
In symbols, with N people in k groups, where group i has nᵢ people and rank sum Rᵢ:
If the groups were equivalent, each group's average rank would be close to the overall average of (N + 1) ÷ 2, and H would be small. Software adds a correction when there are many tied values. The effect size eta² (said "eta squared") is the share of the variation in the ranks that is linked to group membership. It runs from 0 to 1, or 0% to 100%.
When H is significant, the follow-up uses Dunn's procedure, a way of comparing each pair of groups using their mean ranks. For two groups i and j:
The z value says how many standard errors apart the two mean ranks are. The standard error measures how much the difference would vary by luck alone. The adjusted p multiplies each pair's raw p-value by the number of pairs. This is called a Bonferroni adjustment, and it protects against false findings.
Five delivery times from each of three depots.
| Group | Times (hours) | Ranks | Rank sum | Mean rank |
|---|---|---|---|---|
| Depot A | 12.1, 14.3, 13.2, 10.8, 15 | 2, 4, 3, 1, 5 | 15 | 3.0 |
| Depot B | 16.4, 18.2, 17.1, 19.5, 15.7 | 7, 9, 8, 11, 6 | 41 | 8.2 |
| Depot C | 20.3, 22.8, 19.9, 21.4, 18.6 | 13, 15, 12, 14, 10 | 64 | 12.8 |
| Group 1 | Group 2 | Difference in mean ranks | z | p (raw) | p (adjusted) |
|---|---|---|---|---|---|
| Depot A | Depot B | -5.2 | -1.838 | 0.0660 | 0.1980 |
| Depot A | Depot C | -9.8 | -3.465 | < 0.001 | 0.0016 |
| Depot B | Depot C | -4.6 | -1.626 | 0.1039 | 0.3116 |
Only Depot A and Depot C differ clearly after the adjustment, even though the overall test is significant. Depot B sits between them and cannot be separated from either with groups this small.
One-way ANOVA is the standard test for comparing the averages of three or more separate groups.
| Feature | One-way ANOVA | Kruskal-Wallis test |
|---|---|---|
| What it uses | The exact values | The position of each value in the line |
| What it compares | The group averages | Whether groups tend to sit higher or lower in the line |
| The test statistic | F, compared with its own reference curve | H, compared with a chi-square reference curve |
| What it needs from the data | Values that spread evenly around each group's average in a bell shape | No particular shape |
| Measure of how big the differences are | Eta-squared, from the spread of the values | Eta-squared from H, the share of variation in ranks linked to the groups |
| Follow-up comparisons | Pairwise t-tests, unadjusted in AnalyZ Solutions | Dunn comparisons with a Bonferroni adjustment |
Every test gives trustworthy results only when some conditions hold. These are called assumptions. For this test they are modest.
The descriptive table shows each group's number of people, median and mean rank. The median is the middle value, so half the group scores above it and half below. The mean rank is the average position number of the group's members in the combined line, and it is the quantity the test actually compares. A group with a higher mean rank tends to have higher values than the rest.
The significance level tells you how sure you can be that the groups really differ. "Significant at the 5% level" means that if the groups were truly the same, differences this large would appear by luck in fewer than 5 samples out of 100. The effect size tells you how big the differences are, which is a separate question. It is eta-squared, calculated from the test statistic, and it shows the share of the variation in the ranks that is linked to group membership. Under 1% is negligible, 1% to 6% small, 6% to 14% medium and above 14% large.
A significant overall result says that at least one group differs, but not which. When the overall test is significant, the module compares every pair of groups using Dunn's procedure. It reports each pair's significance after a Bonferroni adjustment, which multiplies the raw p-values by the number of comparisons. A pair flagged as significant is still significant after allowing for how many pairs were compared. Follow-up comparisons like these are often called post-hoc comparisons, meaning "after the fact".
Open Inferential Analysis, choose Non-parametric Tests, then Kruskal-Wallis.
The figures below are invented to show the steps, and they follow the results tables in AnalyZ Solutions, with a few columns left out to save space. Suppose delivery times in hours were recorded for 25 deliveries from each of four depots. The times are heavily skewed, so a rank-based test is used.
| Group | N | Median | Mean Rank |
|---|---|---|---|
| Depot A | 25 | 17.4 | 51.52 |
| Depot B | 25 | 23.6 | 59.72 |
| Depot C | 25 | 21.1 | 62.64 |
| Depot D | 25 | 10.6 | 28.12 |
| H | df | p-value | Sig | eta² | N |
|---|---|---|---|---|---|
| 21.8129 | 3 | < 0.001 | *** | 0.1960 | 100 |
| Group 1 | Group 2 | Mean Rank 1 | Mean Rank 2 | z | p (adjusted) | Sig |
|---|---|---|---|---|---|---|
| Depot A | Depot B | 51.52 | 59.72 | -0.9993 | 1.0000 | ns |
| Depot A | Depot C | 51.52 | 62.64 | -1.3552 | 1.0000 | ns |
| Depot A | Depot D | 51.52 | 28.12 | 2.8517 | 0.0261 | * |
| Depot B | Depot C | 59.72 | 62.64 | -0.3559 | 1.0000 | ns |
| Depot B | Depot D | 59.72 | 28.12 | 3.8510 | < 0.001 | *** |
| Depot C | Depot D | 62.64 | 28.12 | 4.2068 | < 0.001 | *** |
Delivery times differ across the four depots, and the difference is large and significant at the 0.1% level. Depot D is clearly faster than the other three, which are similar to each other. The test shows where the differences are and does not explain them. Staffing, routes or the type of goods handled could all matter.
Stopping at the overall test. It says only that some group differs. State which groups differ, using the pairwise results.
Running several Mann-Whitney tests instead. Each extra test raises the chance of finding a difference that is not real.
Describing results as differences in averages. Use medians and mean ranks.
Ignoring unequal spread. If one group is far more spread out than the others, a significant result may reflect the spread as much as the typical value.
Open Non-parametric Tests in AnalyZ Solutions. Free to sign up, free to analyze.
Get started