# How to compute a confidence interval for a mean difference (matched pairs) (in R)

See all solutions.

Say we have two sets of data that are not independent of each other and come from a matched-pairs experiment, and we want to construct a confidence interval for the mean difference between these two samples. How do we make this confidence interval? Let’s assume we’ve chosen a confidence level of $\alpha$ = 0.05.

## Solution

We have two samples of data, $x_1, x_2, x_3, \ldots, x_k$ and $x’_1, x’_2, x’_3, \ldots, x’_k$. We’re going to use some fake data below just as an example; replace it with your real data.

1
2
sample.1 <- c(15, 10, 7, 22, 17, 14)
sample.2 <- c(9, 1, 11, 13, 3, 6)


The shortest way to create the confidence interval is with R’s t.test() function. It’s just one line of code (after we choose $\alpha$).

1
2
alpha <- 0.05       # replace with your chosen alpha (here, a 95% confidence level)
t.test(sample.1, sample.2, paired = TRUE, conf.level = 1-alpha)

1
2
3
4
5
6
7
8
9
10
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


If you need the lower and upper bounds later, you can save them as variables as follows.

1
2
3
conf.interval <- t.test(sample.1, sample.2, paired = TRUE, conf.level = 1-alpha)
lower.bound <- conf.interval$conf.int upper.bound <- conf.interval$conf.int


It’s also possible to do the computation manually, using the code below.

1
2
3
4
5
6
7
8
diff.samples <- sample.1 - sample.2                # differences between the samples
n = length(sample.1)                               # number of observations per sample
diff.mean <- mean(diff.samples)                    # mean of the differences
diff.variance <- var( diff.samples )               # variance of the differences
critical.val <- qt(p = alpha/2, df = n - 1,
lower.tail=FALSE)                              # critical value

1

Either method gives the same result. Our 95% confidence interval is $[0.70338, 13.2966]$.