Alus
Alus

Reputation: 33

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. I cannot seem to figure how to apply the binom.test to each of the four rows in the designated columns (see below: Fct3=number of successes and Tct3 = n trials) and instead I've been doing an individual binom.test manually for each taxon which is tedious.

  2. Here is an example portion of the data frame:

                    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. I would like for the taxon, the 95% confidence intervals, and the p-value be represented in the output

  4. There is probably a pretty simple coding adjustment I could make but am stuck and am a novice in R. Thanks for any help.

  5. How is it possible to add the header "Taxon2015_2016" in the output so that it is shown above the taxa column in the transposed results?

Upvotes: 3

Views: 1076

Answers (1)

Sathish
Sathish

Reputation: 12723

Loop through each row of the data in df2, perform binom.test and return pvalue and confidence interval values. Then assign column names to the results.

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

Data:

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")

Upvotes: 2

Related Questions