Paired t-test

Paired t-test in Python

Paired t-tests are used to test the difference of means for two dependant variables. In the Paired t-test, the difference of the means between the two samples is compared to a given number that represents the null hypothesis. For a Paired t-test, the number of observations in each sample must be equal.

In Python, a Paired t-test can be performed using the scipy.stats.ttest_rel(…) function from the scipy package, which accepts the following parameters:

1.a, b: Sample observations. The arrays must have the same shape.

2.axis: If an int, the axis of the input along which to compute the statistic. The statistic of each axis-slice (e.g. row) of the input will appear in a corresponding element of the output. If None, the input will be raveled before computing the statistic.

3.nan_policy: Defines how to handle input NaNs.

4.alternative (optional): Defines the alternative hypothesis.

5.keepdims: If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

Data Used

import pandas as pd

# Create sample data
data = {
    'SBPbefore': [120, 124, 130, 118, 140, 128, 140, 135, 126, 130, 126, 127],
    'SBPafter': [128, 131, 131, 127, 132, 125, 141, 137, 118, 132, 129, 135]
}

df_pressure = pd.DataFrame(data)

Paired t-test

The following code was used to test the comparison in Python. Note that the baseline null hypothesis goes in the “popmean” parameter.

import pandas as pd
from scipy import stats

# Perform paired t-test
t_stat, p_value = stats.ttest_rel(df_pressure['SBPbefore'], df_pressure['SBPafter'])

# Print results
print("Paired t-test:")
print(f"t = {t_stat}")
print(f"p-value = {p_value}")
Paired t-test:
t = -1.0896479884009451
p-value = 0.299163498777129