BayesianFirstAid (version 0.1)

bayes.prop.test: Bayesian First Aid Alternative to a Test of Proportions


bayes.prop.test estimates the relative frequency of success for two or more groups using Bayesian estimation and is intended as a replacement for prop.test.


bayes.prop.test(x, n, comp.theta = NULL, alternative = NULL, cred.mass = 0.95, correct = NULL, n.iter = 15000, = "none", p, conf.level)


a vector of counts of successes, a one-dimensional table with two entries, or a two-dimensional table (or matrix) with 2 columns, giving the counts of successes and failures, respectively.
a vector of counts of trials; ignored if x is a matrix or a table.
a vector of fixed relative frequencies of success to compare with the estimated relative frequency of success. The length of comp.theta must be the same as the number of groups specified by x, and its elements must be greater than 0 and less than 1. This argument fills a similar role as p in prop.test.
ignored and is only retained in order to mantain compatibility with prop.test.
the amount of probability mass that will be contained in reported credible intervals. This argument fills a similar role as conf.level in prop.test.
ignored and is only retained in order to mantain compatibility with prop.test
The number of iterations to run the MCMC sampling.
The type of progress bar. Possible values are "text", "gui", and "none".
same as comp.theta and is only retained in order to mantain compatibility with prop.test.
same as cred.mass and is only retained in order to mantain compatibility with prop.test.


A list of class bayes_prop_test that contains information about the analysis. It can be further inspected using the functions summary, plot, diagnostics and model.code.


Given data on the number of successes and failures bayes.prop.test estimates \theta₁…ₘ, the relative frequencies of success for each of the $m$ groups. The following model is assumed for each group:

$$x \sim \mathrm{Binom}(\theta, n)$$ $$\theta \sim \mathrm{Beta}(1, 1)$$

Here the prior on the $\theta$s is a non-informative $Beta(1, 1)$ distribution which is identical to a $Uniform(0, 1)$ distribution. By ploting and looking at a summary of the object returned by bayes.prop.test you can get more information about the shape of the posterior and the posterior predictive distribution. model.code prints out the corresponding R code underlying bayes.prop.test which can be copy-n-pasted into an R script and modified, for example, changing the prior on $\theta$.

The print and plot function will only work well with a small number of groups (2 to 6). If you have more groups you might want to run the model with a small number of groups and then print the model code using model.code and fit the model using that code. The current model does not assume any dependency between the groups, if this is an unreasonable assumption you might want to modify the model code (from model.code) to include a dependency between the groups (see here for an example).

See Also

bayes.binom.test for when you want to estimate the relative frequency for only one group.


Run this code
# Data from Muller, F. H., Tobakmissbrauch und Lungencarcinom,
# Zeit. f. Krebsforsch. 49, 57-85, 1939. One of the early papers
# investigating the relation between smoking and lung cancer.

# Number of heavy smokers in one group of 86 lung cancer patients
# and one group of 86 healthy individuals.
no_heavy_smokers <- c(56, 31)
no_cases <- c(86, 86)

bayes.prop.test(no_heavy_smokers, no_cases)

# Save the return value in order to inspect the model result further.
fit <- bayes.prop.test(no_heavy_smokers, no_cases)

# MCMC diagnostics (should not be necessary for such a simple model)

# Print out the R code to run the model. This can be copy'n'pasted into
# an R-script and further modified.

Run the code above in your browser using DataCamp Workspace