【问题标题】:Paired t.test gives no result for NA配对 t.test 没有给出 NA 的结果
【发布时间】:2018-05-11 21:49:19
【问题描述】:

我正在使用配对 t 检验比较 3 个实验的测量值。 但是,如果缺少某些值(NA),我不会得到任何结果

数据:

dat <- read.table(text='sample  G1  G2  G3
S1   0.002458717   0.01045769    0.003770503
S2   0.010009784   0.02465088    0.011505804
S3   NA            NA            0.006403423
S1   0.003458717   0.01145769    0.004770503
S2   0.011009784   0.02565088    0.012505804
S3   NA            NA            0.007403423
S1   0.001458717   0.00945769    0.002770503
S2   0.009009784   0.02365088    0.010505804
S3   NA            NA            0.0054034232', header=TRUE)

请注意,G1/2/3 不是独立的测量值,而是代表已测试的不同基因。它们不相关,每个 G 都需要自己的 t.test。

t.test:

sig<-lapply(dat[-1], function(x) 
  pairwise.t.test(x, dat$sample,
                  p.adjust.method = "BH"))



 > sig
    $G1

        Pairwise comparisons using t tests with pooled SD 

    data:  x and dat$sample 

       S1 S2
    S2 -  - 
    S3 -  - 

    P value adjustment method: BH 

    $G2

        Pairwise comparisons using t tests with pooled SD 

    data:  x and dat$sample 

       S1 S2
    S2 -  - 
    S3 -  - 

    P value adjustment method: BH 

    $G3

        Pairwise comparisons using t tests with pooled SD 

    data:  x and dat$sample 

       S1      S2     
    S2 0.00024 -      
    S3 0.01803 0.00117

P value adjustment method: BH

我理解为什么 G1/2 的 S1/2 和 S3 之间没有比较,因为缺少这些值。但是,我不明白为什么比较 S1 和 S2 没有结果?

谢谢!

【问题讨论】:

  • 你能删除 NA 的吗? lapply(2:4, function(x) { tmp &lt;- na.omit(dat[,c(1,x)]) pairwise.t.test(tmp[,2], tmp$sample) })
  • 每组至少需要 2 个观察值才能获得 p 值。在您的示例中,G1 和 G2 完全缺少 S3。因此,删除观测值
  • 谢谢@Jimbou。我知道我需要至少 2 次观察,因此,我不会得到 p 值来比较一组具有 NA 的组,这不是问题。但是,即使比较具有 3 个观察值的组(S1 与 S2),我也没有得到任何 p 值。我将尝试实施您的解决方案来删除 NAs

标签: r statistics na


【解决方案1】:

对于pairwise.t.test,您需要一个响应和一个分组向量;所以问题出在数据格式上,而不是NAs。您只需要相应地转换来自dat 的数据(有关详细信息,请参阅?pairwise.t.test)。

这是使用基本 R 函数stack 的一种方法:

# Convert wide to long dataframe, then select values 
val <- stack(dat, select = -sample)$values;

# Sample labels (the grouping vector)
grp <- rep(dat$sample, 3);


pairwise.t.test(val, grp, p.adjust.method = "BH");
#    
#        Pairwise comparisons using t tests with pooled SD
#    
#data:  val and grp
#
#   S1     S2
#S2 0.0031 -
#S3 0.8160 0.0319

请注意,NAs 将被自动省略(默认情况下)。

【讨论】:

  • 嗨。感谢那。我认为我的例子不清楚,G1/2/3 实际上是不相关的。它们不是我想比较的 3 个测量值。这就是为什么使用 lapply 对每一列执行配对 t 检验。数据实际上是长格式
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-14
  • 2018-11-30
  • 2014-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-28
相关资源
最近更新 更多