Skip to main content

Test a Single Binomial Proportion

Source: R/bin_prop_test.R

Description

Conducts a basic hypothesis test for a single Binomial proportion. This is a wrapper around functions such as binom.test() but takes a factor vector with two levels.

Usage

bin_prop_test(y, ...)

## Default S3 method:
bin_prop_test(y, ...)

## S3 method for class 'factor'
bin_prop_test(
  y,
  method = c("exact", "exact_sas", "score"),
  event_level = NULL,
  null_value = 0.5,
  alternative = "two_sided",
  ...
)

Arguments

Arguments Description
y A factor vector with two levels.
… Not currently used.
method A single character string with possible values: • "exact": An exact test via binom.test(). • "exact_sas": An exact test via exact_binom_test_sas() that emulates SAS’s Proc Freq results. • "score": An asymptotic score test via prop.test().
event_level A single character value for the level of the factor that corresponds to the event of interest. The default is the first factor level.
null_value A single number in [0, 1] for the proportion under the null hypothesis.
alternative A character value indicating the alternative hypothesis and must be one of "two_sided", "greater", or "lesser".

Details

This function performs one of several Frequentist hypothesis tests on a single Binomial proportion.

‘me_t_h_o_d = “ex_a_c_t”’:

Rsbinom.test()` implements the exact test described in Section 1.4 of Agresti (2012). Once the data have been transformed into the number of events and the total number of non-missing values, the code using default argument values is

binom.test(num_events, num_total, alternative = “two.sided”, p = 0.5)

There is no corresponding Proc Freq code; there are several methodological differences between the exact test produced by SAS and binom.test(). Using the terminology of Fay (2010):

  1. One-sided p-values. Both use the same tail sums, P(X <= x) and P(X >= x).

• binom.test() returns whichever tail you ask for.

• Proc Freq keeps only the smaller tail, in variables XPL_BIN or XPR_BIN, and leaves the other variable missing.

  1. Two-sided p-values.

• binom.test() uses the “minlike” method (short for “minimum likelihood”): it adds the probabilities of every outcome no more likely than the observed one.

• SAS uses the “central” method: twice the smaller tail, capped at 1. The two agree only when p_0 = 0.5, where the null distribution is symmetric.

  1. Numerical calculation (not a definition difference).

• SAS`s right tail behaves as if it were computed as 1 - P(X <= x - 1). That subtraction leaves an absolute error of about 1e-14.

• R computes the upper tail directly.

‘me_t_h_o_d = “ex_a_c_t_sa_s”’:

This method mimics the results produced by:

proc freq data={data set}; tables {analysis column} / binomial; exact binomial; run;

In the terms of Fay (2010), Proc Freq uses the “central” method: twice the smaller tail, capped at 1. This is also called “doubling” or “TST” by Hirji (2006).

• One-sided p-values: only the smaller of the two tails is reported. If the requested tail is the larger one, the p-value is NA, as it is missing in SAS.

• Two-sided p-value: twice the smaller tail, capped at 1 (the “central” method).

• The right tail is computed as 1 - P(X <= x - 1), as SAS does, so very small right tails lose precision and can be exactly 0.

The results agree with Proc Freq to about 1e-13 but are not identical to the last bit, because R and SAS use different algorithms for the binomial distribution function.

The underlying R function for this method is [binom_test_sas()], which gives more details for the comparison to SAS.

‘me_t_h_o_d = “sc_o_r_e”’:

The score test produces a Z statistic that uses the null value to compute the variance in the denominator. To compute a two-sided p-value, compare the square of the Z statistic to a chi-squared distribution with a single degree of freedom. For a one-sided test, use the Z statistic and compare it to the appropriate standard normal tail.

This method`s defaults does not use a continuity correction (for SAS compatibility) and is is equivalent to

prop.test(num_events, num_total, alternative = “two.sided”, p = 0.5, correct = FALSE)

The equivalent SAS code is

proc freq data={data set}; tables {analysis column} / binomial(cl=score); run;

The SAS output data set has Z_BIN (the statistic), P2_BIN (two-sided p-value), PL_BIN (left-sided p-value) and PR_BIN (right-sided p-value). Proc Freq only reports one of the two one-sided p-values for each test: the one in the direction of the observed deviation. The other is missing.

Sign of z PL_BIN (less) PR_BIN (greater) P2_BIN (two.sided) z > 0 missing reported reported z < 0 reported missing reported z = 0 reported (0.5) missing reported

The reported one-sided p-value is always the smaller tail, so it is never above 0.5. The larger tail is never produced. In the results, 3,822 of 11,466 rows are missing a p-value. Each of them is a one-sided test that points away from the data. No two-sided test`s p-value is missing. Some examples:

events total null p z alternative p_value 1 2 0.01 6.96 greater 1.6e-12 1 2 0.01 6.96 less missing 1 2 0.01 6.96 two.sided 3.3e-12 1 50 0.25 -3.76 greater missing 1 50 0.25 -3.76 less 8.6e-05 1 50 0.25 -3.76 two.sided 1.73e-04

To assess the level of equivalence, each was tested at three null values (0.01, 0.25, 0.5) and three alternatives, which gives 11,466 tests per language. SAS has no value for 3,822 one-sided tests. The comparison drops those, leaving 7,644 tests: 3,822 two-sided, 2,897 greater and 925 lesser. R has no missing p-values.

The two methods showed good concordance, with absolute differences ranging from -1.2e-15 to 8.3e-16.

Value

An object with classes "bin_prop_test" and "segue_object" with elements

• code: contains code in R and SAS for translate().

• results: the results,

• data_characteristics: characteristics of the data.

• options: records options that were used by the function.

Examples

if (rlang::is_installed("modeldata")) withAutoprint({ # examplesIf
cog_ability <- modeldata::ad_data$Class
str(cog_ability)
test_res <- bin_prop_test(cog_ability)
test_res

# Can be tidied to obtain a tibble of results:
tidy(test_res)
}) # examplesIf
> cog_ability <- modeldata::ad_data$Class
> str(cog_ability)
 Factor w/ 2 levels "Impaired","Control": 2 2 2 2 2 1 2 2 2 1 ...
> test_res <- bin_prop_test(cog_ability)
> test_res
> tidy(test_res)
# A tibble: 1 × 8
  estimate statistic  p.value parameter conf.low conf.high method    alternative
     <dbl>     <dbl>    <dbl>     <dbl>    <dbl>     <dbl> <chr>     <chr>      
1    0.273        91 5.86e-17       333    0.226     0.325 Exact bi… two.sided  

See Also

binom.test(), prop.test(), broom::tidy.htest()

References

Agresti, A. 2013. Categorical Data Analysis. John Wiley and Sons Inc., Hoboken.

Fay, M. 2010. “Two-Sided Exact Tests and Matching Confidence Intervals for Discrete Data.” The R Journal 2 (1): 53–58. .

Hirji, K. 2006. Exact Analysis of Discrete Data. Chapman and Hall/CRC.

CAMIS Working Group page on the Binomial Test: https://psiaims.github.io/CAMIS/Comp/r-sas_binomial_test.html