# How to 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 sp

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.

Step 3: Interpret the results.

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.