aov 和 t.test 提供不同的结果

aov and t.test deliver different results

据我所知,当应用于具有一个解释变量的数据时,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 

如您所见,方差分析的 p 值为 0.256,t 检验的 p 值为 0.0796

为了了解这种偏差的原因,我使用 t-test and for ANOVA 的公式自己计算了测试统计数据。当组的大小不同时,t 检验函数似乎给出了错误的结果。

是否有设置使 t 检验能够正确处理不同的组大小?

结果没有错,如果两组的方差不相等,t-test函数只是应用Welch-correction。您可以像这样抑制它:

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 

它给出与方差分析相同的 p-value(另请注意,输出的标题现在不是 "Welch Two Sample t-test",而是简单的 "Two Sample t-test")。