How can I perform Dunn’s test using Python?

Dunn’s test is a statistical method used to compare multiple groups and determine if there are significant differences between them. To perform Dunn’s test using Python, the following steps can be followed:

1. Import the necessary libraries such as scipy, statsmodels, and pandas.
2. Load the data into a pandas DataFrame.
3. Use the statsmodels library to perform a Kruskal-Wallis test to determine if there are significant differences between the groups.
4. If the Kruskal-Wallis test results in a significant p-value, indicating that there are significant differences, then proceed to perform Dunn’s test.
5. Use the Dunn’s test function from the scikit-posthocs library to perform pairwise comparisons between the groups.
6. The results will include the adjusted p-values and confidence intervals for each comparison.
7. Interpret the results to determine which groups have significant differences.

In summary, Dunn’s test can be performed using Python by importing the necessary libraries, loading the data, and using the appropriate functions from the statsmodels and scikit-posthocs libraries. This method allows for a quick and efficient way to analyze and compare multiple groups in a statistical manner.

Perform Dunn’s Test in Python


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

Example: Dunn’s Test in Python

Researchers want to know if three different fertilizers lead to different levels of plant growth. They randomly select 30 different plants and split them into three groups of 10, applying a different fertilizer to each group. At the end of one month they measure the height of each plant.

Upon performing a Kruskal-Wallis Test, they find that the overall p-value is statistically significant, which means the median growth is the not same across the three groups. Next, they perform Dunn’s test to determine exactly which groups are different.

To perform Dunn’s test in Python, we can use the posthoc_dunn() function from the scikit-posthocs library.

The following code shows how to use this function:

Step 1: Install scikit-posthocs.

First we need to install the scikit-posthocs library:

pip install scikit-posthocs

Step 2: Perform Dunn’s test.

Next, we can create the data and perform Dunn’s test:

#specify the growth of the 10 plants in each group
group1 = [7, 14, 14, 13, 12, 9, 6, 14, 12, 8]
group2 = [15, 17, 13, 15, 15, 13, 9, 12, 10, 8]
group3 = [6, 8, 8, 9, 5, 14, 13, 8, 10, 9]
data = [group1, group2, group3]

#perform Dunn's test using a Bonferonni correction for the p-values
import scikit_posthocs as spsp.posthoc_dunn(data, p_adjust = 'bonferroni')

               1	       2	       3
1	1.000000	0.550846	0.718451
2	0.550846	1.000000	0.036633
3	0.718451	0.036633	1.000000

Note that we chose to use a Bonferroni correction for the p-values to control the family-wise error rate, but other potential choices for the p_adjust argument include:

  •  sidak
  • holm-sidak
  • simes-hochberg
  • hommel
  • fdr_bh
  • fdr_by
  • fdr_tsbh

Refer to the documentation for more details on each of these p-value adjustment methods.

From the results of Dunn’s test we can observe the following:

  • The adjusted p-value for the difference between group 1 and group 2 is 0.550846.
  • The adjusted p-value for the difference between group 1 and group 3 is 0.718451.
  • The adjusted p-value for the difference between group 2 and group 3 is 0.036633.

Thus, the only two groups that are statistically significantly different at α = .05 are groups 2 and 3.

Additional Resources

An Introduction to Dunn’s Test for Multiple Comparisons
How to Perform Dunn’s Test in R

x