How to do a hypothesis test for a mean difference (matched pairs) (in Python, using SciPy)
Task
Say we have two sets of data that are not independent of each other and come
from a matched-pairs experiment,
Related tasks:
- How to compute a confidence interval for a mean difference (matched pairs)
- How to do a hypothesis test for a mean difference (matched pairs)
- How to do a hypothesis test for a population proportion
- How to do a hypothesis test for population variance
- How to do a hypothesis test for the difference between means when both population variances are known
- How to do a hypothesis test for the difference between two proportions
- How to do a hypothesis test for the mean with known standard deviation
- How to do a hypothesis test for the ratio of two population variances
- How to do a hypothesis test of a coefficient’s significance
- How to do a one-sided hypothesis test for two sample means
- How to do a two-sided hypothesis test for a sample mean
- How to do a two-sided hypothesis test for two sample means
Solution
We choose a value,
We’re going to use fake fata here, but you can replace our fake data with your real data below. Because the data are matched pairs, the samples must be the same size.
1
2
3
# Replace the following example data with your real data
sample1 = [15, 10, 7, 22, 17, 14]
sample2 = [ 9, 1, 11, 13, 3, 6]
Two-tailed test
In a two-sided hypothesis test, the null hypothesis states that the mean
difference is equal to 0 (or some other hypothesized value),
1
2
from scipy import stats
stats.ttest_rel(sample1, sample2, alternative = "two-sided")
TtestResult(statistic=2.8577380332470415, pvalue=0.03550038112896236, df=5)
Our
Note that the function above specifically tests whether the mean of
1
2
d = 6 # as an example
stats.ttest_rel([ x - d for x in sample1 ], sample2, alternative = "two-sided")
TtestResult(statistic=0.4082482904638631, pvalue=0.6999865427788738, df=5)
The above
Right-tailed test
If instead we want to test whether the mean difference is less than or equal to
zero,
1
stats.ttest_rel(sample1, sample2, alternative = "greater")
TtestResult(statistic=2.8577380332470415, pvalue=0.01775019056448118, df=5)
Our
A similar change could be made to the code above to test
Left-tailed test
If instead we want to test whether the mean difference is greater than or equal to
zero,
1
stats.ttest_rel(sample1, sample2, alternative = "less")
TtestResult(statistic=2.8577380332470415, pvalue=0.9822498094355188, df=5)
Our
A similar change could be made to the code above to test
Content last modified on 24 July 2023.
See a problem? Tell us or edit the source.
Contributed by Elizabeth Czarniak (CZARNIA_ELIZ@bentley.edu)