The Kruskal-Wallis test
The Kruskal-Wallis is the non-parametric equivalent to one-way ANOVA. The Kruskal-Wallis
test allows us to test for differences among more than two samples. Like other rank-based
tests it has some assumptions, but these are less restrictive than those for ANOVA. The
assumptions are essentially the same as those for the Mann-Whitney U-test:
• The variable being tested is measured on ordinal, interval, or ratio scales
• The observations from both groups are independent of one another.
• The distribution of the variable in each group is similar (apart than the fact that they
have a different central tendency)
The third assumption is important, particularly with respect to the skewness of the
distributions. The test is at least reasonably robust to differences in the dispersion, but the
Kruskal-Wallis test should not be used if the skewness of the variable is different among
groups is very different. The reason for this is—just as with Mann-Whitney U-test—that a
significant result is hard to interpret.
When all three of the above assumptions are satisfied the Kruskal-Wallis is used as a way of
looking for differences in the central tendency of two or more groups. A one-way ANOVA
evaluates the statistical significance of differences between means of these groups. The null
hypothesis of the Kruskal-Wallis test (if all three of the above assumptions) is that the
groups have the same median. A significant p value therefore indicates that the medians are
likely to be different.
Learning in cuttlefish
In a study of the ability of cuttlefish to learn, an experiment was conducted to determine how
the length of exposure to a situation influenced the learning process. Cuttlefish feed on
prawns. If they are presented with prawns in a glass tube they strike at them but, obviously,
fail to capture the prey. Not surprisingly, after a period of this fruitless activity, they give up
striking at the prawns.
In the experiment, 50 cuttlefish were divided at random into 5 groups of 10 and cuttlefish
from each group were presented with prawns in glass tubes for different lengths of time: 2
min., 4 min., 8 min., 15 min., and 30 min. for the 5 groups respectively. After 24 hours the
same cuttlefish were presented with prawns again and the number of strikes they made (over
a fixed period) were recorded.
The data are in the file Cuttlefish.csv. There are two variables: Strikes contains the number of
strikes recorded and Time identifies the groups (period of previous exposure):
cuttlefish <- read.csv(file=”Cuttlefish.CSV”)
Thinking of that third assumption (distribution of the variable in each group is similar),
subset each of the 5 treatment groups and examine their distributions using a histogram.
The data are clearly very variable. The combination of the apparent skew and the fact that
the data are generally small whole numbers with several equal values in each sample, means
that we may not be very successful in using a transformation to beat the data into shape. Let’s
use the Kruskal-Wallis test instead.
Predictably, the R function to carry out a Kruskal-Wallis test is called kruskal.test, and it is
used in exactly the same way as every other statistical modelling function we have looked at:
kruskal.test(Strikes ~ Time, data = cuttlefish)
And… one more time… the elements of the output should be easy to work out. These are a
statement of the test used and the data, followed by the results: a test statistic (a type
of χ2 statistic), a degrees of freedom, and the all-important p-value.
We report all of these when writing up results of a Kruskal-Wallis test. However, there is some
disagreement in the literature how to report a Kruskal-Wallis test—some people report the
statistic as a χ2, while others refer to it as an ‘H’ statistic. We will follow the common
convention of reporting it as an ‘H’ value.
The test (as with ANOVA) tells us that there is at least one difference among the groups, but
it doesn’t tell where the difference or differences are. The output does not give the medians
so we cannot judge how the samples are ordered.
Produce a boxplot to examine the medians for the different treatments.
In this case it is fairly clear that longer periods of exposure to the protected prawn do seem
to result in fewer strikes in the later trial. Once we understand what is driving the significant
result we’re in a position to write a summary:
The frequency with which the cuttlefish attacked the prawns was significantly affected by the
length of time for which they had been exposed to protected prawns 24h earlier (KruskalWallis test: H=11.04, d.f.=4, p<0.05), with longer prior exposure resulting in lower attack
If it was important to know exactly which treatments were significantly different, then some
sort of multiple comparison test would be useful. There are no non-parametric multiple
comparison tests available in base R, but they are implemented in the package
You first need to install the package, then run the following code:
strikes<-nparcomp(Strikes ~ Time, data = cuttlefish)
The Kruskal-Wallis test