binomial_test() tests whether the observed proportion of a binary
variable differs from a hypothesized proportion. It uses the exact binomial
test, making it valid for any sample size.
Think of it as:
Testing whether a coin is fair (proportion of heads = 50 percent)
Checking if your sample's gender ratio matches the population
Verifying if satisfaction rates meet a target proportion
The test tells you:
Whether the observed proportion differs significantly from the expected
The exact p-value (based on the binomial distribution)
A confidence interval for the true proportion
Arguments
- data
Your survey data (a data frame or tibble)
- ...
One or more binary variables to test. Each must have exactly 2 categories (e.g., Yes/No, Male/Female, 0/1, TRUE/FALSE)
- p
The hypothesized proportion to test against (Default: 0.50 = 50 percent). It refers to Group 1, the category of the first valid case in the data (as in SPSS). With 0.5 the test is two-tailed; with any other value it is one-tailed in the direction of the observed proportion, as SPSS reports it.
- weights
Optional survey weights for population-representative results. Give a column name (unquoted or as a string), an expression such as
sampling_weight * 2, or a numeric vector with one weight per row.- conf.level
Confidence level for intervals (Default: 0.95 = 95 percent)
Value
Test results showing whether the proportion differs from expected, including:
Category counts and observed proportions
Test proportion (null hypothesis)
Exact p-value (two-tailed for
p = 0.5, otherwise one-tailed; columnalternative: "two.sided", "less" or "greater")Confidence interval for the true proportion of Group 1 (Clopper-Pearson)
Details
Understanding the Results
P-value: If p < 0.05, the proportion differs significantly from the test proportion
p < 0.001: Very strong evidence the proportion differs
p < 0.01: Strong evidence the proportion differs
p < 0.05: Moderate evidence the proportion differs
p > 0.05: No significant difference from expected proportion
Observed Proportion: The actual proportion in your data. Compare this with the test proportion to see the direction of any difference.
Confidence Interval: The range likely to contain the true population proportion. If the test proportion falls outside this range, the result is significant.
When to Use This
Use the binomial test when:
You want to compare an observed proportion to a known value
Your variable has exactly 2 categories
You need an exact test (not relying on normal approximation)
Sample size is small (where chi-square may not be reliable)
Relationship to Other Tests
For testing association between two categorical variables: Use
chi_square()insteadFor comparing proportions between groups: Use chi-square or z-test for proportions
For larger samples with normal approximation: results will be very similar to a one-sample z-test for proportions
Group 1 and one-tailed tests
As in SPSS NPAR TESTS /BINOMIAL, Group 1 is the category of the
first valid case in the data (per group with group_by()), and
p is its hypothesized proportion. For p = 0.5 the p-value
is two-tailed. For any other p SPSS reports a one-tailed p-value
in the direction of the observed proportion ("the proportion of cases in
the first group < p"); binomial_test() does the same. To test the
other category, put a case of it first or use 1 - p.
Weighted analyses
Each case weight is rounded to a whole number before the categories are
counted. With all weights equal to 1 the weighted test reproduces the
unweighted one exactly. Weighted results are not compared with SPSS
output; see vignette("spss-compatibility").
See also
binom.test for the base R exact binomial test.
chi_square for testing associations between categorical
variables.
Other hypothesis_tests:
ancova(),
chi_square(),
chisq_gof(),
factorial_anova(),
fisher_test(),
friedman_test(),
kruskal_wallis(),
mann_whitney(),
mcnemar_test(),
oneway_anova(),
t_test(),
wilcoxon_test()
Examples
# Load required packages and data
library(dplyr)
data(survey_data)
# Test whether gender split is 50/50
survey_data %>%
binomial_test(gender, p = 0.50)
#> Binomial Test: gender
#> Group 1 (Female): prop = 0.522 vs 0.500, p = 0.026 *, N = 2500
#> Use summary() for detailed output.
# Test whether East region proportion is 50%
survey_data %>%
binomial_test(region, p = 0.50)
#> Binomial Test: region
#> Group 1 (East): prop = 0.194 vs 0.500, p < 0.001 ***, N = 2500
#> Use summary() for detailed output.
# Multiple variables at once
survey_data %>%
binomial_test(gender, region, p = 0.50)
#> Binomial Test: gender
#> Group 1 (Female): prop = 0.522 vs 0.500, p = 0.026 *, N = 2500
#> Binomial Test: region
#> Group 1 (East): prop = 0.194 vs 0.500, p < 0.001 ***, N = 2500
#> Use summary() for detailed output.
# Weighted analysis
survey_data %>%
binomial_test(gender, p = 0.50, weights = sampling_weight)
#> Binomial Test: gender [Weighted]
#> Group 1 (Female): prop = 0.522 vs 0.500, p = 0.026 *, N = 2500
#> Use summary() for detailed output.
# Grouped analysis (separate test per region)
survey_data %>%
group_by(region) %>%
binomial_test(gender, p = 0.50)
#> [region = East]
#> Binomial Test: gender
#> Group 1 (Female): prop = 0.509 vs 0.500, p = 0.716, N = 485
#> [region = West]
#> Binomial Test: gender
#> Group 1 (Male): prop = 0.474 vs 0.500, p = 0.023 *, N = 2015
#> Use summary() for detailed output.
