据我所知,当应用于具有一个解释变量的数据时,t 检验应该提供与方差分析相同的结果(相同的 p 值)。为了测试这一点,我运行了以下命令来比较结果:
df <- structure(list(y = c(1, 1, 1, 1, 1, 1, 2, 2, 1, 2, 1), x = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("FP", "WP" ), class = "factor")), class = "data.frame", row.names = c(NA,-11L))
summary(aov(y ~ x, data = df))
Df Sum Sq Mean Sq F value Pr(>F)
x 1 0.3068 0.3068 1.473 0.256
Residuals 9 1.8750 0.2083
t.test(y ~ x, data = df)
Welch Two Sample t-test
data: y by x
t = -2.0494, df = 7, p-value = 0.0796
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.80768193 0.05768193
sample estimates:
mean in group FP mean in group WP
1.000 1.375
Run Code Online (Sandbox Code Playgroud)
可以看出,方差分析的 p 值为0.256,t 检验的 p 值为0.0796。
为了理解这种偏差的原因,我自己计算了测试统计量,使用t 检验和ANOVA的公式。当组的大小不同时,t 检验函数似乎给出了错误的结果。
是否有设置可以使 t 检验正确处理不同的组大小?
结果没有错,如果两组的方差不相等,该t-test函数只是应用韦尔奇校正。你可以像这样抑制它:
t.test(y ~ x, data = df, var.equal = TRUE)
Two Sample t-test
data: y by x
t = -1.2136, df = 9, p-value = 0.2558
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-1.0740253 0.3240253
sample estimates:
mean in group FP mean in group WP
1.000 1.375
Run Code Online (Sandbox Code Playgroud)
这给出了与方差分析相同的 p 值(还要注意输出的标题现在不是“Welch Two Sample t-test”,而是“Two Sample t-test”)。
| 归档时间: |
|
| 查看次数: |
63 次 |
| 最近记录: |