Step-by-Step Guide to Perform Dunn’s Test in Python
1. Import Necessary Libraries
Import the required libraries for data manipulation and Dunn’s Test:
# Importing necessary packages and modules
import pandas as pd
import scikit_posthocs as sp
from sklearn.datasets import load_iris
2. Load Your Dataset
Load your dataset into a pandas DataFrame. Ensure your data is structured appropriately for comparison:
# Load the dataset
iris_dataset = load_iris(as_frame=True)
dataset = iris_dataset.frame
print(dataset.head())
Output:
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) \
0 5.1 3.5 1.4 0.2
1 4.9 3.0 1.4 0.2
2 4.7 3.2 1.3 0.2
3 4.6 3.1 1.5 0.2
4 5.0 3.6 1.4 0.2
target
0 0
1 0
2 0
3 0
4 0
3. Prepare Your Data
Extract the data you want to compare. In this example, let’s compare sepal widths among different species:
# Data containing sepal width of the three species
data = [dataset[dataset['target'] == 0]['sepal width (cm)'],
dataset[dataset['target'] == 1]['sepal width (cm)'],
dataset[dataset['target'] == 2]['sepal width (cm)']]
4. Perform Dunn’s Test:
# Using the posthoc_dunn() function
p_values = sp.posthoc_dunn(data, p_adjust='holm')
print(p_values)
Output:
1 2 3
1 1.000000e+00 2.047087e-14 1.536598e-07
2 2.047087e-14 1.000000e+00 1.580934e-02
3 1.536598e-07 1.580934e-02 1.000000e+00
- For the difference between groups 1 and 2, the adjusted p-value is 2.047087e-14
- For the difference between groups 1 and 3, the adjusted p-value is 1.536598e-07
- For the difference between groups 2 and 3, the adjusted p-value is 1.580934e-02
5. Compare with significance level
Let’s assume our significance level is 0.05, So, we will check if p_values less than the chosen significance level indicate statistically significant differences between groups.
print(p_values <0.05)
Output:
1 2 3
1 False True True
2 True False True
3 True True False
This indicates that:
- Group 1 is significantly different from Group 2 and Group 3.
- Group 2 is significantly different from Group 1 and Group 3.
- Group 3 is significantly different from Group 1 and Group 2.
How to Perform Dunn’s Test in Python
Dunn’s test is a statistical procedure used for multiple comparisons following a Kruskal-Wallis test. Here’s a breakdown of what it does and when it’s used:
Table of Content
- Dunn’s Test
- What is the Kruskal-Wallis test?
- Key points about Dunn’s test
- How to Perform Dunn’s Test with Python
- Step-by-Step Guide to Perform Dunn’s Test in Python
- Frequently Asked Questions on Dunn’s Test