Performs exact binomial test and pairwise comparisons following a significant exact multinomial test. Wrapper around the R base function link[stats]{binom.test}() that returns a data frame as a result.

binom_test(
x,
n,
p = 0.5,
alternative = "two.sided",
conf.level = 0.95,
detailed = FALSE
)

pairwise_binom_test(
x,
alternative = "two.sided",
conf.level = 0.95
)

pairwise_binom_test_against_p(
x,
p = rep(1/length(x), length(x)),
alternative = "two.sided",
conf.level = 0.95
)

## Arguments

x

numeric vector containing the counts.

n

number of trials; ignored if x has length 2.

p

a vector of probabilities of success. The length of p must be the same as the number of groups specified by x, and its elements must be greater than 0 and less than 1.

alternative

indicates the alternative hypothesis and must be one of "two.sided", "greater" or "less". You can specify just the initial letter.

conf.level

confidence level for the returned confidence interval.

detailed

logical value. Default is FALSE. If TRUE, a detailed result is shown.

method to adjust p values for multiple comparisons. Used when pairwise comparisons are performed. Allowed values include "holm", "hochberg", "hommel", "bonferroni", "BH", "BY", "fdr", "none". If you don't want to adjust the p value (not recommended), use p.adjust.method = "none".

## Value

return a data frame containing the p-value and its significance. with some the following columns:

• group, group1, group2: the categories or groups being compared.

• statistic: the number of successes.

• parameter: the number of trials.

• p: p-value of the test.

• p.adj: the adjusted p-value.

• method: the used statistical test.

• p.signif, p.adj.signif: the significance level of p-values and adjusted p-values, respectively.

• estimate: the estimated probability of success.

• alternative: a character string describing the alternative hypothesis.

• conf.low,conf.high: Lower and upper bound on a confidence interval for the probability of success.

The returned object has an attribute called args, which is a list holding the test arguments.

## Functions

• binom_test(): performs exact binomial test. Wrapper around the R base function binom.test that returns a dataframe as a result.

• pairwise_binom_test(): performs pairwise comparisons (binomial test) following a significant exact multinomial test.

• pairwise_binom_test_against_p(): performs pairwise comparisons (binomial test) following a significant exact multinomial test for given probabilities.

## Examples

# Exact binomial test
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Data: 160 mice with cancer including 95 male and 65 female
# Q1: Does cancer affect more males than females?
binom_test(x = 95, n = 160)
#> # A tibble: 1 × 6
#>       n estimate conf.low conf.high      p p.signif
#> * <dbl>    <dbl>    <dbl>     <dbl>  <dbl> <chr>
#> 1   160    0.594    0.513     0.671 0.0216 *
# => yes, there are a significant difference

# Q2: compare the observed proportion of males
# to an expected proportion (p = 3/5)
binom_test(x = 95, n = 160, p = 3/5)
#> # A tibble: 1 × 6
#>       n estimate conf.low conf.high     p p.signif
#> * <dbl>    <dbl>    <dbl>     <dbl> <dbl> <chr>
#> 1   160    0.594    0.513     0.671 0.872 ns
# => there are no significant difference

# Multinomial test
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Data
tulip <- c(red = 81, yellow = 50, white = 27)
# Question 1: are the color equally common ?
# this is a test of homogeneity
res <- multinom_test(tulip)
res
#> # A tibble: 1 × 2
#>             p p.signif
#> *       <dbl> <chr>
#> 1 0.000000711 ****
attr(res, "descriptives")
#> # A tibble: 3 × 3
#>   group  observed expected
#>   <chr>     <dbl>    <dbl>
#> 1 red          81     52.7
#> 2 yellow       50     52.7
#> 3 white        27     52.7

# Pairwise comparisons between groups
#> # A tibble: 3 × 9
#> * <chr>  <chr>  <dbl>    <dbl>    <dbl>     <dbl>       <dbl>      <dbl> <chr>
#> 1 red    yellow   131    0.618    0.529     0.702 0.00851        2.55e-2 *
#> 2 red    white    108    0.75     0.657     0.828 0.000000191    5.72e-7 ****
#> 3 yellow white     77    0.649    0.532     0.755 0.0117         3.5 e-2 *
#> # … with abbreviated variable name ¹​p.adj.signif

# Question 2: comparing observed to expected proportions
# this is a goodness-of-fit test
expected.p <- c(red = 0.5, yellow = 0.33, white = 0.17)
res <- multinom_test(tulip, expected.p)
res
#> # A tibble: 1 × 2
#>       p p.signif
#> * <dbl> <chr>
#> 1 0.942 ns
attr(res, "descriptives")
#> # A tibble: 3 × 3
#>   group  observed expected
#>   <chr>     <dbl>    <dbl>
#> 1 red          81     79
#> 2 yellow       50     52.1
#> 3 white        27     26.9

# Pairwise comparisons against a given probabilities
pairwise_binom_test_against_p(tulip, expected.p)
#> # A tibble: 3 × 10