我想对我的数据表中除分组变量和Obs.Number外的所有变量的两个子组应用均值t检验和中位数Wilcoxon检验。我有两个组:组0和组1。我的数据如下所示:

Obs.Number     Var1         Var2      Var3     Var4      Var5       Var6   GroupingVar
     1        32153.00    48516.00   0.3687    0.0160   12.4288   28646.0       0
     2        22465.00    34494.00   0.1804    0.0213   10.5003   20988.0       1
     3         1393.10     4257.40  10.1413    0.0359   53.2323     127.3       1
     4            0.99      164.07   0.4906    0.0817 -989.5800       0.0       1
     5         4745.60     5712.80   0.4914    0.1766   62.5905    2488.7       1
     6        10222.80     9898.20   0.8993    0.0115   15.6440    7665.1       0
        ...


现在,我想对所有变量应用这两个测试,以得到如下所示的输出:

              Test-Statistic    Test-Statistic               p-value        p-value
              for Mean T-test   for Wilcoxon Median-test     of Mean-test   of Median-test

Variable 1     ?                  ?                            ?             ?
Variable 2     ?                  ?                            ?             ?
...


我将t.test()和wilcox.test()与sapply()一起使用,但是没有成功。任何想法如何获得结果?

最佳答案

这可以完成工作(尽管非常麻烦):

## using am as the grouping variable
data.frame(stats_t = apply(subset(mtcars, select=-am), 2, function(y) t.test(y ~ mtcars$am)$statistic),
           p_t     = apply(subset(mtcars, select=-am), 2, function(y) t.test(y ~ mtcars$am)$p.value),
           stats_w = apply(subset(mtcars, select=-am), 2, function(y) wilcox.test(y ~ mtcars$am, exact = FALSE)$statistic),
           p_w     = apply(subset(mtcars, select=-am), 2, function(y) wilcox.test(y ~ mtcars$am, exact = FALSE)$p.value)
)

        stats_t          p_t stats_w          p_w
mpg  -3.7671231 1.373638e-03    42.0 1.871391e-03
cyl   3.3541138 2.464713e-03   194.0 3.899814e-03
disp  4.1977266 2.300413e-04   214.0 5.493451e-04
hp    1.2661888 2.209796e-01   176.0 4.570132e-02
drat -5.6460883 5.266742e-06    24.0 1.426919e-04
...

07-26 05:54