Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How To Perform Dunnís Test In Python?
Dunn's test is a statistical technique for comparing the means of several samples. When it's required to compare the means of numerous samples to identify which ones are noticeably different from one another, Dunn's test is frequently employed in a range of disciplines, including biology, psychology, and education. We shall examine Dunn's test in-depth in this article, along with a Python implementation.
What is Dunn's Test?
Dunn's test is a statistical analysis used to compare the means of numerous samples. It is a form of multiple comparison test used to compare the means of more than two samples to identify which samples differ substantially from one another.
When the assumption of normality is violated, Dunn's test is commonly used as a post-hoc test following the non-parametric Kruskal-Wallis test. The Kruskal-Wallis test first determines if there are any significant differences between the sample means. If significant differences are found, Dunn's test is then used to perform pairwise comparisons to identify exactly which samples are substantially different from one another.
Installing Required Library
First, we need to install the scikit-posthocs library which provides the Dunn's test implementation ?
pip install scikit-posthocs
Syntax
To perform Dunn's test in Python, we use the posthoc_dunn() method from the scikit-posthocs library ?
scikit_posthocs.posthoc_dunn(data, p_adjust='bonferroni')
Parameters
data Array or list of groups to compare
p_adjust P-value adjustment method (default: 'bonferroni')
Example: Plant Growth Study
Let's demonstrate with a practical example. Researchers want to discover if three different fertilizers result in various degrees of plant growth. They choose 30 different plants at random and divide them into three groups of ten, each with a different fertilizer. After one month, they measure the height of each plant.
import scikit_posthocs as sp
import pandas as pd
# Specify the growth of the 10 plants in each group
group1 = [9, 10, 16, 9, 10, 5, 7, 13, 10, 9]
group2 = [16, 19, 15, 17, 19, 11, 6, 17, 11, 9]
group3 = [7, 9, 5, 8, 8, 14, 11, 9, 14, 8]
# Create a DataFrame for the analysis
data = []
groups = []
for i, group in enumerate([group1, group2, group3], 1):
data.extend(group)
groups.extend([f'Group {i}'] * len(group))
df = pd.DataFrame({'value': data, 'group': groups})
# Perform Dunn's test using Bonferroni correction
result = sp.posthoc_dunn(df, val_col='value', group_col='group', p_adjust='bonferroni')
print(result)
Group 1 Group 2 Group 3
Group 1 1.0 0.115458 1.0
Group 2 1.0 1.0 0.274655
Group 3 1.0 1.0 1.0
Interpreting the Results
The output shows a matrix of adjusted p-values for all pairwise comparisons ?
Group 1 vs Group 2: p-value = 0.115458 (not significant at ? = 0.05)
Group 1 vs Group 3: p-value = 1.000000 (not significant)
Group 2 vs Group 3: p-value = 0.274655 (not significant)
Since all p-values are greater than 0.05, we conclude that there are no statistically significant differences between any of the fertilizer groups.
Alternative Data Format
You can also use Dunn's test with data in list format ?
import scikit_posthocs as sp # Alternative approach with list of groups group1 = [9, 10, 16, 9, 10, 5, 7, 13, 10, 9] group2 = [16, 19, 15, 17, 19, 11, 6, 17, 11, 9] group3 = [7, 9, 5, 8, 8, 14, 11, 9, 14, 8] data = [group1, group2, group3] # Perform Dunn's test result = sp.posthoc_dunn(data, p_adjust='bonferroni') print(result)
1 2 3
1 1.0 0.115458 1.0
2 1.0 1.0 0.274655
3 1.0 1.0 1.0
Conclusion
Dunn's test is a powerful non-parametric post-hoc test for comparing multiple groups when the normality assumption is violated. It's particularly useful in biology, psychology, and education for identifying which specific groups differ significantly from each other after a significant Kruskal-Wallis test.
The scikit-posthocs library makes it easy to perform Dunn's test in Python with various p-value adjustment methods like Bonferroni correction to control for multiple comparisons.
