如何在 R 中复制精确二项式检验,以便它遍历数据框中的整个列并生成输出

How to replicate an Exact Binomial Test in R so that it runs through entire columns in a dataframe and generates an output

    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.test。

  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-value

  4. 我可以做一个非常简单的编码调整,但我被卡住了,我是 R 的新手。感谢您的帮助。

  5. 如何在输出中添加 header "Taxon2015_2016" 以便它显示在转置结果的分类群列上方?

遍历df2中的每一行数据,执行binom.test和return 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")