How to do a hypothesis test for a mean difference (matched pairs)
Description
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
Using SciPy, in Python
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.
Solution, in R
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
sample.1 <- c(15, 10, 7, 22, 17, 14)
sample.2 <- c( 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
3
alpha = 0.05
t.test(sample.1, sample.2, alternative = "two.sided",
mu = 0, paired = TRUE, conf.level = 1-alpha)
Paired t-test
data: sample.1 and sample.2
t = 2.8577, df = 5, p-value = 0.0355
alternative hypothesis: true mean difference is not equal to 0
95 percent confidence interval:
0.7033862 13.2966138
sample estimates:
mean difference
7
Our
If we want instead to test whether it is some other value mu
parameter to the t.test
function instead of zero.
Right-tailed test
If instead we want to test whether the mean difference is less than or equal to
zero,
1
2
t.test(sample.1, sample.2, alternative = "greater",
mu = 0, paired = TRUE, conf.level = 1-alpha)
Paired t-test
data: sample.1 and sample.2
t = 2.8577, df = 5, p-value = 0.01775
alternative hypothesis: true mean difference is greater than 0
95 percent confidence interval:
2.06416 Inf
sample estimates:
mean difference
7
Our
Again, you can use another value mu = 0
in the code.
Left-tailed test
If instead we want to test whether the mean difference is greater than or equal to
zero,
1
2
t.test(sample.1, sample.2, alternative = "less",
mu = 0, paired = TRUE, conf.level = 1-alpha)
Paired t-test
data: sample.1 and sample.2
t = 2.8577, df = 5, p-value = 0.9822
alternative hypothesis: true mean difference is less than 0
95 percent confidence interval:
-Inf 11.93584
sample estimates:
mean difference
7
Our
Again, you can use another value mu = 0
in the code.
Content last modified on 24 July 2023.
See a problem? Tell us or edit the source.
Topics that include this task
Opportunities
This website does not yet contain a solution for this task in any of the following software packages.
- Excel
- Julia
If you can contribute a solution using any of these pieces of software, see our Contributing page for how to help extend this website.