下面更像是我的想法和快速搜索的一个小总结。我以前从未使用过加权 t.test,只有线性回归中的权重。
对于什么是加权 t 检验没有明确的定义。问题在于如何使用权重来估计误差,因为这是 t 检验的基础。您可以查看 discussion 和 this paper 的线性回归权重。
所以你的数据:
df = structure(list(PopDens = c(93.53455, 137.13861, 35.98619, 89.698,
16.27796, 25.33346, 89.698, 46.27796, 25.33346, 36.27796, 1.33346
), Score1 = c(17.985288, 10.549394, 13.392857, 8.644537, 29.591635,
21.081301, 2.644537, 29.591635, 5.081301, 29.591635, 9.081301
), Group = structure(c(2L, 1L, 1L, 2L, 1L, 4L, 3L, 1L, 2L, 1L,
2L), .Label = c("A", "B", "C", "F"), class = "factor")), class = "data.frame", row.names = c(NA,
-11L))
我们仅在 A 和 B 上进行子集化:
df = subset(df,Group %in% c("A","B"))
我们可以比较 t-test 和 lm 的结果:
coefficients(summary(lm(Score1~ Group,data=df)))
Estimate Std. Error t value Pr(>|t|)
(Intercept) 22.54343 3.653195 6.170881 0.0004580837
GroupB -12.34532 5.479793 -2.252882 0.0589470215
t.test(df$Score1[df$Group=="B"],df$Score1[df$Group=="A"],data=df)
Welch Two Sample t-test
data: df$Score1[df$Group == "B"] and df$Score1[df$Group == "A"]
t = -2.404, df = 6.463, p-value = 0.05007
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-24.695931765 0.005282865
sample estimates:
mean of x mean of y
10.19811 22.54343
对于 B 与 A 的差异的影响,您得到的 p 值为 0.0589470215。对于 t.test 0.05007,它并没有太大的不同。
现在进行加权线性回归:
coefficients(summary(lm(Score1~ Group,data=df,weight=df$PopDens)))
Estimate Std. Error t value Pr(>|t|)
(Intercept) 17.845885 3.780246 4.7208269 0.00215547
GroupB -5.466244 5.727617 -0.9543663 0.37168503
您可以看到系数的估计方式不同.. 更倾向于更高权重的样本。
对于包装重量中提供的加权 t 检验:
library(weights)
wtd.t.test(x=df$Score1[df$Group=="A"],y=df$Score1[df$Group=="B"],
weight=df$Score1[df$Group=="A"],weighty=df$Score1[df$Group=="B"],samedata=FALSE)
$test
[1] "Two Sample Weighted T-Test (Welch)"
$coefficients
t.value df p.value
2.90701563 6.97938063 0.02283172
$additional
Difference Mean.x Mean.y Std. Err
13.468496 25.884728 12.416232 4.633101
显然,这是加权 t 检验中的频率权重,但我不确定。如果您更喜欢使用它,最好详细阅读代码,因为它没有很好地记录标准错误等是如何计算的。