简体   繁体   English

对R中的多个箱形图进行ttest

[英]ttest on multiple boxplots in R

I have a facet plot with 40 boxplot in it. 我有一个带有40 boxplot的构面图。 I want to calculate a p-value (adjusted) for the boxplot being different from zero for every boxplot in the graph. 我想为箱形图计算一个p值(已调整),该值不同于图中每个箱形图的零。 So basically a simple ttest. 因此基本上是一个简单的ttest。 Is there a way do do this for the whole dataset? 有没有办法对整个数据集执行此操作?

Data example: 数据示例:

   Pos    Pair Fold_Change
    1     Yes     0.36170477
    1     Yes     0.63926759
    1     No     -0.26791834
    1     No      0.06245854
    2     Yes     0.95403940
    2     Yes     0.45182453
    2     No      0.95403940
    2     No      0.45182453
    ....

ggplot(Pairing,aes(Pairing,Fold_Change,fill=Pairing)) + geom_boxplot() + facet_grid(~Position)

You can split your dataset according to Pos and Pair and then "batch"-perform a t.test on Fold_Change using lapply . 您可以split根据数据集PosPair ,然后选择“批量” -perform一个t.testFold_Change使用lapply

With an example dataset as follows: 带有示例数据集,如下所示:

> Pairings <- expand.grid(Pos=1:4, Pair=rep(c('Yes','No'),2))
> Pairings$Fold_Change <- runif(16)
> Pairings
   Pos Pair Fold_Change
1    1  Yes  0.31076451
2    2  Yes  0.73035471
3    3  Yes  0.40153490
4    4  Yes  0.34368815
5    1   No  0.75398667
6    2   No  0.34578325
7    3   No  0.93771945
8    4   No  0.60309476
9    1  Yes  0.55861350
10   2  Yes  0.75467734
11   3  Yes  0.55299688
12   4  Yes  0.81453028
13   1   No  0.39110782
14   2   No  0.04561982
15   3   No  0.71373404
16   4   No  0.79267332

This is the one-liner that performs what you're looking for: 这是执行您要寻找的内容的一站式服务:

> lapply(split(Pairings,Pairings[,1:2]),function(x)t.test(x$Fold_Change)$p.value)
$`1.Yes`
[1] 0.1768022

$`2.Yes`
[1] 0.01042596

$`3.Yes`
[1] 0.1001815

$`4.Yes`
[1] 0.2458096

$`1.No`
[1] 0.1953704

$`2.No`
[1] 0.4164919

$`3.No`
[1] 0.08582059

$`4.No`
[1] 0.08594222

To explain let's do that step by step. 为了解释,让我们一步一步地做。 Step 1: split your dataset according to Pos and Pair: 步骤1:根据Pos和Pair拆分数据集:

> split(Pairings,Pairings[,1:2])
$`1.Yes`
  Pos Pair Fold_Change
1   1  Yes   0.3107645
9   1  Yes   0.5586135

$`2.Yes`
   Pos Pair Fold_Change
2    2  Yes   0.7303547
10   2  Yes   0.7546773

$`3.Yes`
   Pos Pair Fold_Change
3    3  Yes   0.4015349
11   3  Yes   0.5529969

$`4.Yes`
   Pos Pair Fold_Change
4    4  Yes   0.3436882
12   4  Yes   0.8145303

$`1.No`
   Pos Pair Fold_Change
5    1   No   0.7539867
13   1   No   0.3911078

$`2.No`
   Pos Pair Fold_Change
6    2   No  0.34578325
14   2   No  0.04561982

$`3.No`
   Pos Pair Fold_Change
7    3   No   0.9377195
15   3   No   0.7137340

$`4.No`
   Pos Pair Fold_Change
8    4   No   0.6030948
16   4   No   0.7926733

Step 2: Performs a t.test on the list: 步骤2:在清单上执行t.test:

> lapply(split(Pairings,Pairings[,1:2]),function(x)t.test(x$Fold_Change))
$`1.Yes`

    One Sample t-test

data:  x$Fold_Change 
t = 3.5077, df = 1, p-value = 0.1768
alternative hypothesis: true mean is not equal to 0 
95 percent confidence interval:
 -1.139921  2.009299 
sample estimates:
mean of x 
 0.434689 


$`2.Yes`

    One Sample t-test

data:  x$Fold_Change 
t = 61.0556, df = 1, p-value = 0.01043
alternative hypothesis: true mean is not equal to 0 
95 percent confidence interval:
 0.5879919 0.8970401 
sample estimates:
mean of x 
 0.742516 

...

Step 3: Keep only the p.value by taking function(x)t.test(x$Fold_Change)$p.value . 步骤3:通过采用function(x)t.test(x$Fold_Change)$p.value仅保留function(x)t.test(x$Fold_Change)$p.value

If you want to make it a bit more legible you can modify it as follows: 如果您想使其更清晰易读,可以按以下方式进行修改:

> do.call(rbind,
          lapply(split(Pairings,Pairings[,1:2]),   
                 function(x)data.frame(Pos=x$Pos[1],
                                       Pair=x$Pair[1],
                                       p.value=t.test(x$Fold_Change)$p.value)))
      Pos Pair    p.value
1.Yes   1  Yes 0.17680222
2.Yes   2  Yes 0.01042596
3.Yes   3  Yes 0.10018152
4.Yes   4  Yes 0.24580956
1.No    1   No 0.19537040
2.No    2   No 0.41649186
3.No    3   No 0.08582059
4.No    4   No 0.08594222

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM