7.2 t 检验

7.2 t 检验

7.2.1 问题

你想要检验来自两个总体的样本是否有不同的均值(显著性差异),或者检验从一个总体抽取的样本均值和理论均值有显著性差异。

7.2.2 方案

7.2.2.1 样本数据

我们将使用内置的 sleep 数据集。

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

我们将制造 sleep 数据的宽格式版本;下面我们将看看如何处理长格式和宽格式的数据。

sleep_wide <- data.frame(ID = 1:10, group1 = sleep$extra[1:10],
  group2 = sleep$extra[11:20])
sleep_wide
#>    ID group1 group2
#> 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

7.2.2.2 比较两组:独立双样本 t 检验

假设有两组独立样本(我们这里忽略ID变量)。

t.test() 函数能够操作像 sleep 这样的长格式数据——一列记录测量值,一列指定组别;或者操作两个单独的向量。

## Welch t 检验
t.test(extra ~ group, sleep)
#>
#>  Welch Two Sample t-test
#>
#> data:  extra by group
#> t = -1.9, df = 18, p-value = 0.08
#> 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

## 2 个独立的向量
t.test(sleep_wide$group1, sleep_wide$group2)
#>
#>  Welch Two Sample t-test
#>
#> data:  sleep_wide$group1 and sleep_wide$group2
#> t = -1.9, df = 18, p-value = 0.08
#> alternative hypothesis: true difference in means is not equal to 0
#> 95 percent confidence interval:
#>  -3.3655  0.2055
#> sample estimates:
#> mean of x mean of y
#>      0.75      2.33

默认,t 检验不假设有方差齐性(或称作方差同质)。默认的不是 Student t 检验而是使用了 Welch t 检验。注意 Welch t 检验结果中 df=17.776,这是因为对不同质方差进行了校正。要使用 Student t 检验的话,设置 var.equal=TRUE 。

## Student t 检验
t.test(extra ~ group, sleep, var.equal = TRUE)
#>
#>  Two Sample t-test
#>
#> data:  extra by group
#> t = -1.9, df = 18, p-value = 0.08
#> alternative hypothesis: true difference in means is not equal to 0
#> 95 percent confidence interval:
#>  -3.3639  0.2039
#> sample estimates:
#> mean in group 1 mean in group 2
#>            0.75            2.33

## 针对宽数据相同的操作 (2个分离的向量)
t.test(sleep_wide$group1, sleep_wide$group2, var.equal = TRUE)
#>
#>  Two Sample t-test
#>
#> data:  sleep_wide$group1 and sleep_wide$group2
#> t = -1.9, df = 18, p-value = 0.08
#> alternative hypothesis: true difference in means is not equal to 0
#> 95 percent confidence interval:
#>  -3.3639  0.2039
#> sample estimates:
#> mean of x mean of y
#>      0.75      2.33

7.2.2.3 配对样本t检验

你也可以使用配对样本 t 检验比较配对的数据。数据配对是指你可能有对某种药物治疗前后有观测值或者不同治疗有配对的研究对象。

再次说明,t-test() 函数可以用于有分组变量的数据框或者两个向量。它依赖相对位置来决定配对。如果你使用有分组变量的长格式数据,group 1 的第一行与 group 2 的第一行配对。确保数据排序好并且不存在缺失值是非常重要的;否则配对可以丢弃。这种情况中,我们能通过 group 和 ID 变量进行排序来确保顺序是一样的。

## 按 group 和 ID排序
sleep <- sleep[order(sleep$group, sleep$ID), ]

## 配对 t-test
t.test(extra ~ group, sleep, paired = TRUE)
#>
#>  Paired t-test
#>
#> data:  extra by group
#> t = -4.1, df = 9, p-value = 0.003
#> 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

## 针对宽数据相同的操作 (2个分离的向量)
t.test(sleep_wide$group1, sleep_wide$group2, paired = TRUE)
#>
#>  Paired t-test
#>
#> data:  sleep_wide$group1 and sleep_wide$group2
#> t = -4.1, df = 9, p-value = 0.003
#> 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

配对 t 检验等价于检测是否配对的观察值的总体均值是否为 0 。

t.test(sleep_wide$group1 - sleep_wide$group2, mu = 0, var.equal = TRUE)
#>
#>  One Sample t-test
#>
#> data:  sleep_wide$group1 - sleep_wide$group2
#> t = -4.1, df = 9, p-value = 0.003
#> alternative hypothesis: true mean is not equal to 0
#> 95 percent confidence interval:
#>  -2.4599 -0.7001
#> sample estimates:
#> mean of x
#>     -1.58

7.2.2.4 与期望的总体均值进行比较:单样本 t 检验

假设你想要检测是否 extra 列的数据抽取自总体均值为0的总体(这里忽略 group 与 ID 列)。

t.test(sleep$extra, mu = 0)
#>
#>  One Sample t-test
#>
#> data:  sleep$extra
#> t = 3.4, df = 19, p-value = 0.003
#> alternative hypothesis: true mean is not equal to 0
#> 95 percent confidence interval:
#>  0.5956 2.4844
#> sample estimates:
#> mean of x
#>      1.54