R中两样本t检验:合并与非合并方差结果一致的疑惑
合并方差t检验与Welch检验结果近乎一致的疑问
问题重现
运行以下R代码:
library(oibiostat) data("swim") ## 独立两样本合并方差t检验 t.test(swim$wet.suit.velocity, swim$swim.suit.velocity, alternative = "two.sided", paired = FALSE, var.equal = TRUE) # 不等方差两样本t检验(Welch检验) t.test(swim$wet.suit.velocity, swim$swim.suit.velocity, alternative = "two.sided", paired = FALSE, var.equal = FALSE)
得到的两个检验结果几乎一致:
合并方差t检验结果
Two Sample t-test data: swim$wet.suit.velocity and swim$swim.suit.velocity t = 1.3688, df = 22, p-value = 0.1849 alternative hypothesis: true difference in means is not equal to 0 95 percent confidence interval: -0.03992124 0.19492124 sample estimates: mean of x mean of y 1.506667 1.429167
Welch检验结果
Welch Two Sample t-test data: swim$wet.suit.velocity and swim$swim.suit.velocity t = 1.3688, df = 21.974, p-value = 0.1849 alternative hypothesis: true difference in means is not equal to 0 95 percent confidence interval: -0.03992937 0.19492937 sample estimates: mean of x mean of y 1.506667 1.429167
从理论公式来看,合并方差两样本t检验与非合并方差的Welch检验结果本应存在差异,但在swim数据集上两者输出几乎完全一致。
对比案例
当运行以下模拟数据的代码时,两种检验的输出结果差异明显:
set.seed(5) x1 = rnorm(15, 95, 20) x2 = rnorm(50, 110, 5) t.test(x1, x2) # Welch检验 t.test(x1, x2, var.eq=T) # 合并方差检验
手动计算验证后,Welch检验的结果是正确的,因此疑惑为何swim数据集上合并方差检验的结果会与Welch检验近乎一致,这是数据集的巧合吗?
补充说明
oibiostat包不在CRAN上,托管在GitHub,若未安装可运行以下代码:
devtools::install_github("OI-Biostat/oi_biostat_data")
另外无需加载包即可访问其数据集,可通过以下代码加载:
data(swim, package = "oibiostat")
内容的提问来源于stack exchange,提问作者Pai
相关产品推荐
相关产品推荐

