# Bayesian First Aid: Test of Proportions

**Publishable Stuff**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Does pill A or pill B save the most lives? Which web design results in the most clicks? Which in vitro fertilization technique results in the largest number of happy babies? A lot of questions out there involves estimating the proportion or relative frequency of success of two or more groups (where success could be a saved life, a click on a link, or a happy baby) and there exists a little known R function that does just that, `prop.test`

. Here I’ll present the Bayesian First Aid version of this procedure. A word of caution, the example data I’ll use is mostly from the Journal of Human Reproduction and as such it might be slightly NSFW 🙂

*Bayesian First Aid is an attempt at implementing reasonable Bayesian alternatives to the classical hypothesis tests in R. For the rationale behind Bayesian First Aid see the original announcement. The development of Bayesian First Aid can be followed on GitHub. Bayesian First Aid is a work in progress and I’m grateful for any suggestion on how to improve it!*

## The Model

This is a straight forward extension of the Bayesian First Aid alternative to the binomial test which can be used to estimate the underlying relative frequency of success given a number of trials and, out of them, a number of successes. The model for `bayes.prop.test`

is just more of the same thing, we’ll just estimate the relative frequencies of success for two or more groups instead. Below is the full model where $\theta_i$ is the relative frequency of success estimated given $x_i$ successes out of $n_i$ trials:

## The `bayes.prop.test`

Function

The `bayes.prop.test`

function accepts the same arguments as the original `prop.test`

function, you can give it two vectors one with counts of successes and one with counts of trials or you can supply the same data as a matrix with two columns. If you just ran `prop.test(successes, trials)`

, prepending `bayes.`

(like `bayes.prop.test(successes, trials)`

) runs the Bayesian First Aid alternative and prints out a summary of the model result. By saving the output, for example, like `fit <- bayes.prop.test(successes, trials)`

you can inspect it further using `plot(fit)`

, `summary(fit)`

and `diagnostics(fit)`

.

To demonstrate the use of `bayes.prop.test`

I will use data from the Kinsey Institute for Research in Sex, Gender and Reproduction as described in the article *Genital asymmetry in men* by Bogaert (1997). The data consists of survey answers from 6544 “postpubertal males with no convictions for felonies or misdemeanours” where the respondents, among other things, were asked two questions:

*Are you right or left handed?**If you were standing up and had an erection, if you looked down at the penis, would it be pointing straight ahead or pointed somewhat to the right or left?*

I don’t know about you, but the first question I had was *are right handed people more like to have it on the right, or perhaps the oposite*? Here is the raw data given by Bogaert:

Seems like we have 4794 complete cases. Just looking at those with right or leftward disposition leaves us 1624 cases with 275 right leaners out of 1454 right-handers and 43 right leaners out of 170 non-right-handers. Bogaert uses a chi-square test to analyze this data but since we are interested in *comparing* proportions we’ll start with using `prop.test`

instead:

# Below, the data from the right handers are on the right, logical right? :) n_right_leaners <- c(43, 275) n_respondents <- c(170, 1454) prop.test(n_right_leaners, n_respondents)