【问题标题】:How to do a weighted T-test in R?如何在 R 中进行加权 T 检验?
【发布时间】:2020-04-02 15:35:03
【问题描述】:

我有 df1:

PopDens     Score1   Group
93.53455  17.985288   B
137.13861 10.549394   A
35.98619  13.392857   A
89.69800   8.644537   B
16.27796  29.591635   A
25.33346  21.081301   F
89.69800   2.644537   C
46.27796  29.591635   A
25.33346   5.081301   B
36.27796  29.591635   A
 1.33346   9.081301   B

我想在 AB 组之间进行 t 检验,查看 score1 的均值差异。

但是,我想对分析进行加权,以便具有较大 PopDens 的行在分析中具有更强的权重。例如,我不希望最后一行在分析中的权重与第二行一样大,因为人口密度非常不同。

这是怎么做到的?

【问题讨论】:

    标签: r statistics weighted t-test


    【解决方案1】:

    下面更像是我的想法和快速搜索的一个小总结。我以前从未使用过加权 t.test,只有线性回归中的权重。

    对于什么是加权 t 检验没有明确的定义。问题在于如何使用权重来估计误差,因为这是 t 检验的基础。您可以查看 discussionthis 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 检验中的频率权重,但我不确定。如果您更喜欢使用它,最好详细阅读代码,因为它没有很好地记录标准错误等是如何计算的。

    【讨论】:

    • 这很棒。在 wtd.t.test 中,如果 A 组有 400 人,B 组有 600 人,那么总权重是 100。A 组的权重是 40%,B 组的权重是 60%,还是它只是保证 A 组的权重为 100%,B 组的权重为 100%,而不是跨组的权重?
    • 在代码中可以,他们使用 wtd.mean,即 sum(weights * x)/sum(weights)。他们分别为组执行此操作。所以是后者,A 中的权重为 100%,B 中的权重为 100%
    • 有没有快速或简单的方法来改变它?
    • 我如何跨组加权?
    • 代码中没有选项,您需要计算出如何实现这种称重的数学...快速猜测是调整秤的最小或最大重量在两组中一样的
    【解决方案2】:

    如果您有 2 个以上的组,您还可以使用以下方法进行 wighted anova:

    library(stats)
    aov(Score1 ~ Group, data = df1, weight = PopDens)
    

    【讨论】:

      猜你喜欢
      • 2015-09-17
      • 2019-07-22
      • 2021-05-27
      • 2021-06-16
      • 1970-01-01
      • 2017-05-17
      • 1970-01-01
      • 2020-01-11
      • 2015-04-16
      相关资源
      最近更新 更多