t-test

Problem

You want to test whether two samples are drawn from populations with different means, or test whether one sample is drawn from a population with a mean different from some theoretical mean.

Solution

Sample data

We will use the built-in sleep data set.

  1. sleep
  2. #> extra group ID
  3. #> 1 0.7 1 1
  4. #> 2 -1.6 1 2
  5. #> 3 -0.2 1 3
  6. #> 4 -1.2 1 4
  7. #> 5 -0.1 1 5
  8. #> 6 3.4 1 6
  9. #> 7 3.7 1 7
  10. #> 8 0.8 1 8
  11. #> 9 0.0 1 9
  12. #> 10 2.0 1 10
  13. #> 11 1.9 2 1
  14. #> 12 0.8 2 2
  15. #> 13 1.1 2 3
  16. #> 14 0.1 2 4
  17. #> 15 -0.1 2 5
  18. #> 16 4.4 2 6
  19. #> 17 5.5 2 7
  20. #> 18 1.6 2 8
  21. #> 19 4.6 2 9
  22. #> 20 3.4 2 10

We’ll also make a wide version of the sleep data; below we’ll see how to work with data in both long and wide formats.

  1. sleep_wide <- data.frame(
  2. ID=1:10,
  3. group1=sleep$extra[1:10],
  4. group2=sleep$extra[11:20]
  5. )
  6. sleep_wide
  7. #> ID group1 group2
  8. #> 1 1 0.7 1.9
  9. #> 2 2 -1.6 0.8
  10. #> 3 3 -0.2 1.1
  11. #> 4 4 -1.2 0.1
  12. #> 5 5 -0.1 -0.1
  13. #> 6 6 3.4 4.4
  14. #> 7 7 3.7 5.5
  15. #> 8 8 0.8 1.6
  16. #> 9 9 0.0 4.6
  17. #> 10 10 2.0 3.4

Comparing two groups: independent two-sample t-test

Suppose the two groups are independently sampled; we’ll ignore the ID variable for the purposes here.

The t.test function can operate on long-format data like sleep, where one column (extra) records the measurement, and the other column (group) specifies the grouping; or it can operate on two separate vectors.

  1. # Welch t-test
  2. t.test(extra ~ group, sleep)
  3. #>
  4. #> Welch Two Sample t-test
  5. #>
  6. #> data: extra by group
  7. #> t = -1.8608, df = 17.776, p-value = 0.07939
  8. #> alternative hypothesis: true difference in means is not equal to 0
  9. #> 95 percent confidence interval:
  10. #> -3.3654832 0.2054832
  11. #> sample estimates:
  12. #> mean in group 1 mean in group 2
  13. #> 0.75 2.33
  14. # Same for wide data (two separate vectors)
  15. # t.test(sleep_wide$group1, sleep_wide$group2)

By default, t.test does not assume equal variances; instead of Student’s t-test, it uses the Welch t-test by default. Note that in the Welch t-test, df=17.776, because of the adjustment for unequal variances. To use Student’s t-test, set var.equal=TRUE.

  1. # Student t-test
  2. t.test(extra ~ group, sleep, var.equal=TRUE)
  3. #>
  4. #> Two Sample t-test
  5. #>
  6. #> data: extra by group
  7. #> t = -1.8608, df = 18, p-value = 0.07919
  8. #> alternative hypothesis: true difference in means is not equal to 0
  9. #> 95 percent confidence interval:
  10. #> -3.363874 0.203874
  11. #> sample estimates:
  12. #> mean in group 1 mean in group 2
  13. #> 0.75 2.33
  14. # Same for wide data (two separate vectors)
  15. # t.test(sleep_wide$group1, sleep_wide$group2, var.equal=TRUE)

Paired-sample t-test

You can also compare paired data, using a paired-sample t-test. You might have observations before and after a treatment, or of two matched subjects with different treatments.

Again, the t-test function can be used on a data frame with a grouping variable, or on two vectors. It relies the relative position to determine the pairing. If you are using long-format data with a grouping variable, the first row with group=1 is paired with the first row with group=2. It is important to make sure that the data is sorted and there are not missing observations; otherwise the pairing can be thrown off. In this case, we can sort by the group and ID variables to ensure that the order is the same. For more on sorting see Sorting.

  1. # Sort by group then ID
  2. sleep <- sleep[order(sleep$group, sleep$ID), ]
  3. # Paired t-test
  4. t.test(extra ~ group, sleep, paired=TRUE)
  5. #>
  6. #> Paired t-test
  7. #>
  8. #> data: extra by group
  9. #> t = -4.0621, df = 9, p-value = 0.002833
  10. #> alternative hypothesis: true difference in means is not equal to 0
  11. #> 95 percent confidence interval:
  12. #> -2.4598858 -0.7001142
  13. #> sample estimates:
  14. #> mean of the differences
  15. #> -1.58
  16. # Same for wide data (two separate vectors)
  17. # t.test(sleep.wide$group1, sleep.wide$group2, paired=TRUE)

The paired t-test is equivalent to testing whether difference between each pair of observations has a population mean of 0. (See below for comparing a single group to a population mean.)

  1. t.test(sleep.wide$group1 - sleep.wide$group2, mu=0, var.equal=TRUE)
  2. #> Error in t.test(sleep.wide$group1 - sleep.wide$group2, mu = 0, var.equal = TRUE): object 'sleep.wide' not found

Comparing a group against an expected population mean: one-sample t-test

Suppose that you want to test whether the data in column extra is drawn from a population whose true mean is 0. In this case, the group and ID columns are ignored.

  1. t.test(sleep$extra, mu=0)
  2. #>
  3. #> One Sample t-test
  4. #>
  5. #> data: sleep$extra
  6. #> t = 3.413, df = 19, p-value = 0.002918
  7. #> alternative hypothesis: true mean is not equal to 0
  8. #> 95 percent confidence interval:
  9. #> 0.5955845 2.4844155
  10. #> sample estimates:
  11. #> mean of x
  12. #> 1.54

To visualize the groups, see ../../Graphs/Plotting distributions (ggplot2), ../../Graphs/Histogram and density plot,and ../../Graphs/Box plot.