How to do a hypothesis test for a mean difference (matched pairs) (in R)
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
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.
Contributed by Elizabeth Czarniak (CZARNIA_ELIZ@bentley.edu)