3.1 C
London
Friday, December 20, 2024
HomePythonHypothesis Tests in PythonHow to Perform Welch’s t-test in Python

How to Perform Welch’s t-test in Python

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

The most common way to compare the means between two independent groups is to use a two-sample t-test. However, this test assumes that the variances between the two groups is equal.

If you suspect that the variance between the two groups is not equal, then you can instead use Welch’s t-test, which is the non-parametric equivalent of the two-sample t-test.

To perform Welch’s t-test in Python, we can use the ttest_ind() function from the SciPy library, which uses the following syntax:

ttest_ind(a, b, equal_var=False)

where:

  • a: First array of data values
  • b: Second array of data values
  • equal_var: Specifies no assumption of equal variances between the two arrays

This tutorial explains how to use this function to perform Welch’s t-test in Python.

Example: Welch’s t-test in Python

Suppose we want to compare the exam scores of 12 students who used an exam prep booklet to prepare for some exam vs. 12 students who did not.

The following code shows how to perform Welch’s t-test in Python to determine if the mean exam scores are equal between the two groups:

#import ttest_ind() function
from scipy import stats

#define two arrays of data
booklet = [90, 85, 88, 89, 94, 91, 79, 83, 87, 88, 91, 90]
no_booklet = [67, 90, 71, 95, 88, 83, 72, 66, 75, 86, 93, 84]

#perform Welch's t-test 
stats.ttest_ind(booklet, no_booklet, equal_var = False)

Ttest_indResult(statistic=2.23606797749, pvalue=0.04170979503207)

The test statistic turns out to be 2.2361 and the corresponding p-value is 0.0417.

Since this p-value is less than .05, we can reject the null hypothesis of the test and conclude that there is a statistically significant difference in mean exam scores between the two groups.

Note that the two sample sizes in this example were equal, but Welch’s t-test still works even if the two sample sizes are not equal.

Additional Resources

An Introduction to Welch’s t-test
Welch’s t-test Calculator
How to Perform Welch’s t-test in Excel

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories