3
    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 值

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

  5. 如何在输出中添加标题“Taxon2015_2016”,使其显示在转置结果的分类列上方?

4

1 回答 1

2

循环遍历 中的每一行数据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")
于 2017-03-02T21:36:26.113 回答