1

我正在尝试使用通过倾向得分计算的权重进行独立样本 t 检验。Y 是结果变量(连续变量),sec 是具有两个类别(代码 0 和 1)的分组变量。我使用了以下命令:

          wtd.t.test(Ya1, sec1, weight=weights1T)

产生了以下结果。

         $test
         [1] "Two Sample Weighted T-Test (Welch)"

         $coefficients
           t.value        df   p.value 
         -25.14739 670.43022   0.00000 

         $additional
           Difference       Mean.x       Mean.y     Std. Err 
         -0.496466247  0.003533753  0.500000000  0.019742259 

现在这些结果还不清楚。我想知道两组的平均值。上述结果也没有说明差异是(第 1 组 - 第 0 组)还是(第 0 组 - 第 1 组)。简单的 t.test 不考虑权重。我该如何处理这个问题?

4

2 回答 2

3

您没有指定 wtd.t.test 函数来自哪个包,因此我假设使用“weights”包中的函数。根据文档,前两个参数是来自两组的数据,第三和第四个参数是两组观察值的权重。如果未提供第四个参数,则给定的权重将用于两组。这意味着您编写的代码正在测试 Ya1 的加权平均值是否与 sec1 的加权平均值不同。这似乎不是您想要做的。我认为 lm 更适合您的用例:

# Make some example data
sec1 <- factor(sample(0:1, replace=TRUE, size=700))
Ya1 <- rnorm(700) + as.numeric(sec1)
weights1T <- 1.4^(rnorm(700))
# Use lm() to perform a weighted t-test
summary(lm(Ya1 ~ sec1, weights=weights1T))

这使:

> summary(lm(Ya1 ~ sec1, weights=weights1T))

Call:
lm(formula = Ya1 ~ sec1, weights = weights1T)

Weighted Residuals:
    Min      1Q  Median      3Q     Max 
-3.1921 -0.6672 -0.0374  0.7025  4.4411 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  0.92035    0.05376   17.12   <2e-16 ***
sec11        1.11120    0.07874   14.11   <2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 1.061 on 698 degrees of freedom
Multiple R-squared:  0.222, Adjusted R-squared:  0.2209 
F-statistic: 199.1 on 1 and 698 DF,  p-value: < 2.2e-16

如果你真的想使用wtd.t.test,你可以这样做:

library(weights)
ysplit <- split(Ya1, sec1)
wsplit <- split(weights1T, sec1)
wtd.t.test(y1split[[1]], y1split[[2]], w1split[[1]], w1split[[2]])

这给了你几乎相同的答案lm()

> wtd.t.test(x=ysplit[[1]], y=ysplit[[2]],
+            weight=wsplit[[1]], weighty=wsplit[[2]])
$test
[1] "Two Sample Weighted T-Test (Welch)"

$coefficients
  t.value        df   p.value 
-13.50571 697.25403   0.00000 

$additional
 Difference      Mean.x      Mean.y    Std. Err 
-1.00357229  1.04628894  2.04986124  0.07430724 

Warning message:
In wtd.t.test(y1split[[1]], y1split[[2]], w1split[[1]], w1split[[2]]) :
  Treating data for x and y separately because they are of different lengths
于 2016-09-22T18:47:45.063 回答
1

在我看来,结果就在你面前。

     $additional
       Difference       Mean.x       Mean.y     Std. Err 
     -0.496466247  0.003533753  0.500000000

Mean.xMean.y为您提供第一组和第二组的平均值(您所称的组 0 和 1,或Ya1sec1您的代码中)。

Difference显然是Mean.x负数Mean.y

于 2016-09-22T18:44:25.030 回答