Open In App

How to Perform Welch’s ANOVA in Python

Last Updated : 21 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

When the assumption of equal variances is violated, Welch’s ANOVA is used as an alternative to the standard one-way ANOVA. A one-way ANOVA (“analysis of variance”) is used to see if there is a statistically significant difference in the means of three or more independent groups.

Steps to perform Welch’s ANOVA :

Step 1: Create a Pandas Data frame to hold the data for performing ANCOVA.

Example:

To see if three different studying strategies lead to different test scores, an instructor assigns ten students at random to use each strategy (strat1, strat2, strat3) for one week and then demands each student to take an exam of similar complexity. The following are the exam results of the 30 students:

strat1 = [64, 66, 68, 75, 78, 94, 98, 79, 71, 80]

strat2 = [91, 92, 93, 90, 97, 94, 82, 88, 95, 96]

strat3 = [79, 78, 88, 94, 92, 85, 83, 85, 82, 81]

Step 2: Check whether there is equal variance between scores of those three strategies.

We can use Bartlett’s test to see if the variances in each group are equal. If the test statistic’s p-value is less than a certain level of significance (such as =.05), we can reject the null hypothesis and conclude that not all groups have the same variance. To run Bartlett’s test in Python, use the following code:

Python




# Import library
import scipy.stats as stats
  
# Create Data
strat1 = [64, 66, 68, 75, 78, 94, 98, 79, 71, 80]
strat2 = [91, 92, 93, 90, 97, 94, 82, 88, 95, 96]
strat3 = [79, 78, 88, 94, 92, 85, 83, 85, 82, 81]
  
# perform Bartlett's test
stats.bartlett(strat1, strat2, strat3)


The p-value (.01089) from Bartlett’s test is less than α = .05, which means we can reject the null hypothesis that each group has the same variance. Thus, the assumption of equal variances is violated and we can proceed to perform Welch’s ANOVA.

Step 3: Now apply Welch’s ANOVA to the data.

In Python, we can use the Pingouin package’s welch_anova() function to perform Welch’s ANOVA. Make sure we have installed ‘pingouin’ library before applying Welch’s ANOVA.

Syntax to install pingouin library:

pip install pingouin

Python




import pingouin as pg
import pandas as pd
import numpy as np
  
# create DataFrame
df = pd.DataFrame({'score': [64, 66, 68, 75, 78, 94, 98, 79, 71, 80,
                             91, 92, 93, 90, 97, 94, 82, 88, 95, 96,
                             79, 78, 88, 94, 92, 85, 83, 85, 82, 81],
                   'group': np.repeat(['strat1', 'strat2', 'strat3'],
                                      repeats=10)})
  
# perform Welch's ANOVA
pg.welch_anova(dv='score', between='group', data=df)


Step 4:  Analyze the results obtained after performing welch_anova().

The overall p-value (0.001598) from the ANOVA table is less than α = .05, which means we can reject the null hypothesis that the exam scores are equal between the three studying techniques.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads