How to Perform Dunn’s Test in R


A Kruskal-Wallis test is used to determine whether or not there is a statistically significant difference between the medians of three or more independent groups. It is considered to be the non-parametric equivalent of the One-Way ANOVA.

If the results of a Kruskal-Wallis test are statistically significant, then it’s appropriate to conduct Dunn’s Test to determine exactly which groups are different.

This tutorial explains how to perform Dunn’s Test in R.

Example: Dunn’s Test in R

A researcher wants to know whether or not three drugs have different effects on back pain, so he recruits 30 individuals who all experience similar back pain and randomly splits them up into three groups to receive either Drug A, Drug B, or Drug C. After one month of taking the drug, the researcher asks each individual to rate their back pain on a scale of 1 to 100, with 100 indicating the most severe pain.

The researcher conducts a Kruskal-Wallis test using a .05 significance level to determine if there is a statistically significant difference between the median back pain ratings across these three groups.

The following code shows how to create the data frame in R and perform a Kruskal-Wallis test:

#make this example reproducible
set.seed(0)

#create data frame
data <- data.frame(drug = rep(c("A", "B", "C"), each = 10),
                   pain = c(runif(10, 40, 60),
                            runif(10, 45, 65),
                            runif(10, 55, 70)))

#view first six rows of data frame
head(data)

#  drug     pain
#1    A 57.93394
#2    A 45.31017
#3    A 47.44248
#4    A 51.45707
#5    A 58.16416
#6    A 44.03364

#perform Kruskal-Wallis Test
kruskal.test(pain ~ drug, data = data)

	Kruskal-Wallis rank sum test

data:  pain by drug
Kruskal-Wallis chi-squared = 11.105, df = 2, p-value = 0.003879

Since the overall p-value (0.003879) is less than .05, this means there is a statistically significant difference between the reported pain levels among the three drugs. Thus, we can perform Dunn’s test to determine exactly which drugs are different.

The following code shows how to perform Dunn’s Test in R by using the dunnTest() function from the FSA() library:

#load library
library(FSA)

#perform Dunn's Test with Bonferroni correction for p-values
dunnTest(pain ~ drug,
         data=data,
         method="bonferroni")

Dunn (1964) Kruskal-Wallis multiple comparison
  p-values adjusted with the Bonferroni method.

  Comparison          Z     P.unadj       P.adj
1      A - B -0.8890009 0.374002602 1.000000000
2      A - C -3.2258032 0.001256197 0.003768591
3      B - C -2.3368023 0.019449464 0.058348393

Note that we chose to use a Bonferroni correction for the p-values of the multiple comparisons, but other possible options include:

  • “sidak” (Sidak adjustment)
  • “holm” (Holm Adjustment)
  • “hs’ (Holm-Sidak Adjustment)
  • “bs” (Bonferroni-Sidak Adjustment)
  • “by” (Benjamini-Yekuteili Adjustment)
  • “bh” (Benjamini-Hochberg procedure)

At α = .05, drugs A and C are the only two drugs that are statistically significantly different from each other (adjusted p-value = .003768).

Leave a Reply

Your email address will not be published.