如何在R中复制精确的二项式测试,以使其通过数据框架中的整个列运行并生成输出


    binom.test(4175, 6534, p = 0.5,
    +            alternative = c("two.sided"),
    +            conf.level = 0.95)
Exact binomial test #Output
  data:  4175 and 6534
  number of successes = 4175, number of trials = 6534, p-value < 2.2e-16
  alternative hypothesis: true probability of success is not equal to 0.5
    95 percent confidence interval:
     0.6271830 0.6506236
    sample estimates:
    probability of success 
         0.6389654 
  1. 我似乎无法弄清楚如何将binom.test应用于指定的列中的四个行中的每个行(请参阅下文: fct3 =成功数和 tct3 = n试验),相反,我一直在做一个单独的binom。

  2. 这是数据框架的示例部分:

                    Taxon2015_2016   Tct3  Fct3  SR(F/T)3
    1        Tanytarsus nearcticus   6534  4175 0.6389654
    2  Paratanytarsus penicillatus   4965  2487 0.5009063
    3              Corynoneura sp.   4553  3155 0.6929497
    4         Psectrocladius sp. 2   4355  2247 0.5159587
    
  3. 我想要分类单元,95%的置信区间和p值在输出中表示

  4. 可能有一个非常简单的编码调整,我可以做到,但是被卡住了。

  5. 如何在输出中添加标题" Taxon2015_2016",以便在转载结果中显示在分类单元列上方?

循环遍历df2中数据的每一行,执行binom.test并返回PVALUE和置信区间值。然后将列名分配给结果。

results <- apply(df2, 1, function( x ) {
  model_binom <- binom.test( x = as.numeric( x[3] ),
                             n = as.numeric( x[2] ), 
                             p = 0.5, 
                             alternative = "two.sided", 
                             conf.level = 0.95)
  return( c(pvalue = model_binom$p.value, 
            CI95_low = model_binom$conf.int[1],
            CI95_high = model_binom$conf.int[2]))
})
df2 <- do.call('cbind', list(df2, t(results)))
df2
#                Taxon2015_2016  Tct3 Fct3  SR(F/T)3        pvalue  CI95_low CI95_high
# 1       Tanytarsus nearcticus  6534 4175 0.6389654 4.151168e-113 0.6271830 0.6506236
# 2 Paratanytarsus penicillatus  4965 2487 0.5009063  9.096078e-01 0.4869008 0.5149108
# 3             Corynoneura sp.  4553 3155 0.6929497 3.469412e-153 0.6793208 0.7063307
# 4         Psectrocladius sp.2  4355 2247 0.5159587  3.650260e-02 0.5009950 0.5309009

数据:

df2 <- structure(list(Taxon2015_2016 = c("Tanytarsus nearcticus", "Paratanytarsus penicillatus", 
                                         "Corynoneura sp.", "Psectrocladius sp.2"),
                      ` Tct3` = c(6534L, 4965L, 4553L, 4355L), 
                      Fct3 = c(4175L, 2487L, 3155L, 2247L), 
                      `SR(F/T)3` = c(0.6389654, 0.5009063, 0.6929497, 0.5159587)), 
                 .Names = c("Taxon2015_2016", " Tct3", "Fct3", "SR(F/T)3"), 
                 row.names = c(NA, -4L), class = "data.frame")

最新更新