Two-Sample T-Test with Statsmodels

Statsmodels is a python library that is specifically used to compute different statistical models and for conducting statistical tests. This library makes use of R-style modules and dataframes. 

Firstly, let’s create the sample data. We are creating two arrays and now let’s perform the two-sample T-test. Statsmodels library provides ttest_ind() function to conduct two-sample T-Test whose syntax is given below,

Syntax: ttest_ind(data_group1, data_group2)

Here,

  • data_group1: First data group
  • data_group2: Second data group

Example:

Python3




# Python program to conduct
# two-sample t-test using statsmodels
 
# Importing library
from statsmodels.stats.weightstats import ttest_ind
import numpy as np
import pingouin as pg
 
# Creating data groups
data_group1 = np.array([160, 150, 160, 156.12,
                        163.24,
                        160.56, 168.56, 174.12,
                        167.123, 165.12])
data_group2 = np.array([157.97, 146, 140.2, 170.15,
                        167.34, 176.123, 162.35,
                        159.123, 169.43, 148.123])
 
# Conducting two-sample ttest
ttest_ind(data_group1, data_group2)


Output:

 Two-Sample T-Test with Statsmodels

Interpreting the result:

This is the time to analyze the result. The p-value of the test comes out to be equal to 0.521, which is greater than the significance level alpha (that is, 0.05). This implies that we can say that the average height of students in one class is statistically not different from the average height of students in another class. 



How to Conduct a Two Sample T-Test in Python

In this article, we are going to see how to conduct a two-sample T-test in Python.

This test has another name as the independent samples t-test. It is basically used to check whether the unknown population means of given pair of groups are equal. tt allows one to test the null hypothesis that the means of two groups are equal

Similar Reads

Assumptions

Before conducting the two-sample t-test using Python let us discuss the assumptions of this parametric test. Basically, there are three assumptions that we can make regarding the data groups:...

Two sample T-Test in Python

Let us consider an example, we are given two-sample data, each containing heights of 15 students of a class. We need to check whether two different class students have the same mean height. There are three ways to conduct a two-sample T-Test in Python....

Method 2: Two-Sample T-Test with Pingouin

...

Method 3: Two-Sample T-Test with Statsmodels

...

Contact Us