How to Perform Bartlett’s Test in Python (Step-by-Step)

Bartlett’s test is a statistical test that is used to determine whether or not the variances between several groups are equal.

Many statistical tests (like a one-way ANOVA) assume that variances are equal across samples. Bartlett’s test can be used to verify that assumption.

This test uses the following null and alternative hypotheses:

H0: The variance among each group is equal.

HA: At least one group has a variance that is not equal to the rest.

The test statistic follows a Chi-Square distribution with k-1 degrees of freedom where k is the number of groups.

If the corresponding p-value of the test statistic is less than some significance level (like α = .05) then we can reject the null hypothesis and conclude that not all groups have the same variance.

The following step-by-step example explains how to perform Bartlett’s test in Python.

Step 1: Create the Data

To determine if three different studying techniques lead to different exam scores, a professor randomly assigns 10 students to use each technique (Technique A, B, or C) for one week and then makes each student take an exam of equal difficulty. 

The exam scores of the 30 students are shown below:

#create data
A = [85, 86, 88, 75, 78, 94, 98, 79, 71, 80]
B = [91, 92, 93, 85, 87, 84, 82, 88, 95, 96]
C = [79, 78, 88, 94, 92, 85, 83, 85, 82, 81]

Step 2: Perform Bartlett’s Test

To perform Bartlett’s test, we can use the scipy.stats.bartlett() function.

Here’s how to use this function in our example:

import scipy.stats as stats

#perform Bartlett's test 
stats.bartlett(A, B, C)

BartlettResult(statistic=3.30243757, pvalue=0.191815983)

The test returns the following results:

  • Test statistic B: 3.3024
  • P-value: 0.1918

Since the p-value is not less than 0.05, the professor will fail to reject the null hypothesis. In other words, she doesn’t have sufficient evidence to say that the three groups have different variances.

Thus, she can proceed to perform the one-way ANOVA.

Additional Resources

Bartlett’s Test Calculator
How to Check ANOVA Assumptions
How to Perform a One-Way ANOVA in Python

Leave a Reply

Your email address will not be published. Required fields are marked *