对表中的几个变量应用t检验和Wilcoxon检验

时间:2014-08-19 12:34:39

标签: r testing statistics

我想对所有变量的两个子组应用均值t检验和中值Wilcoxon检验,但是我的数据表中的分组变量和Obs.Number。我有两个组:组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     ?                  ?                            ?             ?
...

我使用了sapply()来使用t.test()和wilcox.test(),但是我没有成功。任何想法如何得到结果?

1 个答案:

答案 0 :(得分:1)

这可以完成工作(尽管非常不优雅):

## 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
...
相关问题