How to Perform Tukey’s Test in Python

Spread the love


Tukey’s Test, also known as Tukey’s Honest Significant Difference (HSD) test, is a post-hoc test that is used to make pairwise comparisons between groups’ means after an ANOVA test has been conducted. In this article, we will discuss in detail what Tukey’s Test is, why it is important, and how to perform it using Python.

Background and Significance of Tukey’s Test


Analysis of Variance (ANOVA) is a statistical method used to compare the means of three or more independent groups. It tests if there is a statistically significant difference among the group means. However, ANOVA itself doesn’t tell which specific groups are different from each other.

b. Post-hoc Tests

To identify which groups are different after ANOVA has determined that there are significant differences, post-hoc tests are used. These tests make multiple pairwise comparisons and control the experiment-wise error rate.

c. Importance of Tukey’s Test

Among post-hoc tests, Tukey’s Test is one of the most common. It compares all possible pairs and is particularly useful when sample sizes are equal, though it can be used for unequal sample sizes too. It controls the family-wise error rate and is conservative in declaring significance, which makes it highly reliable.

Understanding Tukey’s Test

a. Hypotheses

For each pairwise comparison, the hypotheses are:

  • Null Hypothesis (H0): The means of the two groups are equal.
  • Alternative Hypothesis (H1): The means of the two groups are different.

b. Test Statistic

Tukey’s HSD test statistic calculates the difference between each pair of means and divides by the standard error, taking into account the number of comparisons and group sizes.

c. Assumptions

  • Groups are independent.
  • Data is normally distributed.
  • Homogeneity of variances.

d. Applications

  • Comparing means from multiple groups in experiments.
  • Used in quality control, business analytics, and research.

Loading and Preparing Data

Before you can perform Tukey’s HSD test, you need to have some data. Load your data from a CSV file, excel, SQL database, or any other source. The pandas library is useful for loading and managing data.


import pandas as pd

# Load data from a CSV file
data = pd.read_csv('your-data-file.csv')

Performing Tukey’s Test in Python

a. Using statsmodels

The statsmodels library provides the pairwise_tukeyhsd function for performing Tukey’s Test.

import statsmodels.stats.multicomp as multi

# Sample data: scores from students in three different classes
scores = [90, 92, 91, 89, 95, 93, 91, 96, 94, 92, 87, 85, 88, 86, 87]
classes = ['A', 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'B', 'C', 'C', 'C', 'C', 'C']

# Perform Tukey's Test
results = multi.pairwise_tukeyhsd(scores, classes)

# Print the results

b. Interpreting the Results

The output will show the mean differences between groups, the confidence interval, and whether the null hypothesis is rejected for each pair.

Practical Example

Let’s consider a practical example where you have test scores of students from three different schools and you want to know if there is a statistically significant difference in the average test scores among the schools.

import pandas as pd
import statsmodels.stats.multicomp as multi

# Sample data
data = {
    'School': ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'C', 'C', 'C', 'C'],
    'Scores': [85, 87, 88, 89, 91, 92, 93, 95, 90, 89, 88, 92]

# Convert to DataFrame
df = pd.DataFrame(data)

# Perform Tukey's Test
results = multi.pairwise_tukeyhsd(df['Scores'], df['School'])

# Output the results


Tukey’s HSD test is a powerful tool for making pairwise comparisons between groups’ means after an ANOVA test. In Python, the statsmodels library makes it convenient to perform this test. Being a post-hoc test, it is essential to ensure that its assumptions are met before interpreting the results. This test is widely used in various fields including scientific research, business analytics, and quality control, to make informed decisions based on the data.

Leave a Reply