How can Dunn’s test be performed in R?

Dunn’s test is a non-parametric statistical test used to compare multiple groups of data. It is commonly used when the data does not meet the assumptions of parametric tests, such as ANOVA. In order to perform Dunn’s test in R, the following steps can be followed:

1. Load the necessary packages: The “dunn.test” package must be loaded into the R environment in order to use the Dunn’s test function.

2. Prepare the data: The data must be organized in a suitable format, with each group of data in a separate column or vector.

3. Perform the Dunn’s test: The function “dunn.test()” can be used to perform Dunn’s test. It requires the data as input, along with the appropriate arguments for specifying the groups and desired level of significance.

4. Interpret the results: The output of the Dunn’s test will include the p-value and the statistical significance of the results. The results can be further visualized using plots or tables.

Overall, Dunn’s test can be easily performed in R by following these steps. It is a useful tool for comparing multiple groups of data and can provide valuable insights in statistical analysis.

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).

x