The paired t-test is a special case of the more generalized 2-sample t-test for comparing the sample means of a continuous response variable (Y) measured for two different values of a categorical predictor variable (X). However, in the case of the paired t-test, the individual observations are paired in some scientifically reasonable way. For example, they might be the same individuals measured under two different situations, or a before vs.Ā after situation using the same subjects, or twins that are genetically identical but subjected to different treatments. In R, it can be implemented using the t.test()
function, just like the two-sample t-test. If you are new to t-tests, I recommend that you start with the 2-sample t-test KickstartR first.
As an example, letās consider data from Cushny and Peebles (1905) who gave two different optical isomers of the same soporific drug to ten individuals. They then measured how much extra time the individuals slept, compared to a control, undrugged state.
require(mosaic) # be sure to install and load the usual packages if you have not already
require(lattice)
require(datasets)
data(sleep)
You can find out more about the data using help()
and once the data are loaded you can click on the data frame to view the actual dataset in the data window. Note that the two drugs are indicated by the āgroupā variable.
help(sleep)
It is always helpful to look at a plot and summary of the data first. Since there are so few observations, we can use a dotplot()
to see the distribution of extra sleep time for the two drugs. We can also use favstats()
to see a comprehensive summary of the data.
dotplot(extra~group, data=sleep, col=sleep$ID, ylab="Extra sleep (h)", xlab="Drug")
favstats(extra~group, data=sleep)
## .group min Q1 median Q3 max mean sd n missing
## 1 1 -1.6 -0.175 0.35 1.70 3.7 0.75 1.789 10 0
## 2 2 -0.1 0.875 1.75 4.15 5.5 2.33 2.002 10 0
Here, we colored the dots to represent each individual, though it is not perfect because we ran out of colors before we indicated each of the 10 individuals. It is easy to see that drug 2 generally produced more extra sleep, but there really was a lot of overlap. We can use a paired t-test to compare the two effects. Since the data are in ID order for each of the drugs (take a look at the data to be sure!), the easiest way is to simply use the paired=TRUE
option in the t.test()
command.
t.test(extra~group, data=sleep, paired=TRUE) # again, you can only do this if the observations are in the same order for each of the two drug groups
##
## Paired t-test
##
## data: extra by group
## t = -4.062, df = 9, p-value = 0.002833
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -2.4599 -0.7001
## sample estimates:
## mean of the differences
## -1.58
Sometimes, your data will be organized a bit differently, with the paired observations for a given individual in different columns of the same row. We can reorganize the sleep
data in this way, but it only changes the syntax of the test a little bit, but it demonstrates that the paired t-test is really just a one-sample t-test comparing the mean difference to zero.
sleep2 <- data.frame(ID=1:10, drug1=sleep$extra[1:10], drug2=sleep$extra[11:20]) # reorganize the data
sleep2 # look at the data
## ID drug1 drug2
## 1 1 0.7 1.9
## 2 2 -1.6 0.8
## 3 3 -0.2 1.1
## 4 4 -1.2 0.1
## 5 5 -0.1 -0.1
## 6 6 3.4 4.4
## 7 7 3.7 5.5
## 8 8 0.8 1.6
## 9 9 0.0 4.6
## 10 10 2.0 3.4
t.test(~(drug1-drug2), data=sleep2) # note that here the outcome is exactly the same as above
##
## One Sample t-test
##
## data: data$(drug1 - drug2)
## t = -4.062, df = 9, p-value = 0.002833
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
## -2.4599 -0.7001
## sample estimates:
## mean of x
## -1.58
With t = -4.06
and a 95% confidence interval on the difference in means that does not include zero (-2.46 to -0.7), the analysis suggests that drug 1 does indeed produce less extra sleep than drug 2. The fact that the p-value = 0.003
reinforces this conclusion, telling us that a difference at least this large would be very unlikely if there was in fact no difference between the drugs.
So, we can comfortably conclude that the drug 2 isomer is a more potent soporific.
So why use the paired t anyway?
In testing for a difference between the means, the paired t-test takes advantage of the fact that the observations were made on the same (or otherwise reasonably paired) individuals, so simply taking a difference between the individual measures is actually meaningful. This additional information increases our statistical power to discern a difference. To see what this means, compare the paired t-test results above to a simple 2-sample t-test on the same data, ignoring the fact that it was the same individuals taking the two drugs.
t.test(extra~group, data=sleep) # again, we are now ignoring the pairing
##
## Welch Two Sample t-test
##
## data: extra by group
## t = -1.861, df = 17.78, p-value = 0.07939
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -3.3655 0.2055
## sample estimates:
## mean in group 1 mean in group 2
## 0.75 2.33
Notice that the value of t
is now smaller in magnitude (-1.86) and the 95% confidence interval (-3.37 to 0.02) now includes zero, indicating that we should not be too sure that there really is a difference in the effects of the two drugs. Similarly, because p-value = 0.08
, there is a non-negligible chance that we could observe such a difference even if the two drugs had the same effect.
Thus, the added information on the pairing of the samples really does allow us to be more confident about ascribing differences in the effects of the two isomers, because although there was a lot of overlap in the effects, almost every individual exhibited more extra sleep with drug 2.
So if your experimental design includes a natural pairing (before after, etc.), the paired t-test gives you a more sensitive analysis. But keep in mind that the pairing decision occurs at the design stageā¦