# Binomial testing with buttered toast

**mages' blog**, 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.

Rasmus’ post of last week on binomial testing made me think about p-values and testing again. In my head I was tossing coins, thinking about gender diversity and toast. The toast and tossing a buttered toast in particular was the most helpful thought experiment, as I didn’t have a fixed opinion on the probabilities for a toast to land on either side. I have yet to carry out some real experiments.

Suppose I tossed 6 buttered toasts and observed that all but one toast landed on their buttered side.

Now I have two questions:

- Would it be reasonable to assume that there is a 50/50 chance for a toast to land on either side?
- Which probability should I assume?

Suppose the toast is fair, so that the probability for landing on the buttered (B) or unbuttered (U) side is 50%.

Then, the probability of observing one ore more B or U (right tail event) is:

(gt <- 1-1/2^6)<br /># [1] 0.984375<br />and the probability of observing one or fewer B or U (left tail event) is:

(lt <- 1/2^6*choose(6,1) + 1/2^6)<br /># [1] 0.109375<br />while the probability of either extreme event, one or fewer B (or U), is:

2*min(c(gt, lt))<br /># [1] 0.21875<br />In summary, if the toast has an equally probability to fall on either side, then there is 22% chance to observe one or fewer B (or U) in 6 tosses. That’s not that unlikely and hence I would not dismiss the hypothesis that the toast is fair, despite the fact that the sample frequency is only 1/6.

Actually, the probabilities I calculated above are exactly the p-values I get from the classical binomal tests:

## Right tail event<br />binom.test(1, 6, alternative="greater")<br />## Left tail event<br />binom.test(1, 6, alternative="less")<br />## Double tail event<br />binom.test(1, 6, alternative="two.sided")Additionally I can read from the tests that my assumption of a 50% probability is on the higher end of the 95 percent confidence interval. Thus, wouldn’t it make sense to update my belief about the toast following my observations? In particular, as unlike with a coin, I am far less convinced that a 50/50 probability is a good assumption to start with. Arguably the toast is biased by the butter.

Here the concept of a conjugate prior becomes handy again. The idea is to assume that the parameter \(\theta\) of the binomial distribution is a random variable itself. Suppose I have no prior knowledge about the true probability of the toast falling on either side, then a flat prior, such as the Uniform distribution would be reasonable. However, the beta distribution with parameter \(\alpha=1\) and \(\beta=1\) has the same property and is a conjugate to the binomial distribution with parameter \(\theta\). That means there is an analytical solution, in this case the posterior distribution is beta-binomial with hyperparaemters:

\[\alpha’:=\alpha + \sum_{i=1}^n x_i,\; \beta’:=\beta + n – \sum_{i=1}^n x_i,\]

and the posterior predictor for one trial is given as

\[\frac{\alpha’}{\alpha’ + \beta’}\]

so in my case:

alpha <- 1; beta <- 1; n <- 6; success <- 1<br />alpha1 <- alpha + success<br />beta1 <- beta + n - success<br />(theta <- alpha1 / ( alpha1 + beta1))<br /># [1] 0.25My updated believe about the toast landing on the unbuttered side is a probability of 25%. That’s lower than my prior of 50% but still higher than the sample frequency of 1/6. If I would have more toasts I could run more experiments and update my posterior predictor.

I get the same answer from Rasmus `bayes.binom.test`

function:

> library(BayesianFirstAid)<br />> bayes.binom.test(1, 6)<br /><br /> Bayesian first aid binomial test<br /><br />data: 1 and 6<br />number of successes = 1, number of trials = 6<br />Estimated relative frequency of success:<br /> 0.25 <br />95% credible interval:<br /> 0.014 0.527 <br />The relative frequency of success is more than 0.5 by a probability of 0.061 <br />and less than 0.5 by a probability of 0.939 <br />Of course I could change my view on the prior and come to a different conclusion. I could follow the Wikipedia article on buttered toast and believe that the chance of the toast landing on the buttered side is 62%. I further have to express my uncertainty, say a standard deviation of 10%, that is a variance of 1%. With that information I can update my belief of the toast landing on the unbuttered side following my observations (and transforming the variables):

x <- 0.38<br />v <- 0.01<br />alpha <- x*(x*(1-x)/v-1)<br />beta <- (1-x)*(x*(1-x)/v-1)<br />alpha1 <- alpha + success<br />beta1 <- beta + n - success<br />(theta <- alpha1 / ( alpha1 + beta1))<br /># [1] 0.3351821I would conclude that for my toasts / tossing technique the portability is 34% to land on the unbuttered side.

In summary, although their is no sufficient evidence to reject the hypothesis that the ‘true’ probability is not 50% (at the typical 5% level), I would work with 34% until I have more data. Toast and butter please!

### Session Info

R version 3.0.1 (2013-05-16)<br />Platform: x86_64-apple-darwin10.8.0 (64-bit)<br /><br />locale:<br />[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8<br /><br />attached base packages:<br />[1] stats graphics grDevices utils datasets methods <br />[7] base <br /><br />other attached packages:<br />[1] BayesianFirstAid_0.1 rjags_3-12 coda_0.16-1 <br />[4] lattice_0.20-24 <br /><br />loaded via a namespace (and not attached):<br />[1] grid_3.0.1 tools_3.0.1

**leave a comment**for the author, please follow the link and comment on their blog:

**mages' blog**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

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