BDA3 Chapter 3 Exercise 4

Here’s my solution to exercise 4, chapter 3, of Gelman’s Bayesian Data Analysis (BDA), 3rd edition. There are solutions to some of the exercises on the book’s webpage.

\(\DeclareMathOperator{\dbinomial}{Binomial} \DeclareMathOperator{\dbern}{Bernoulli} \DeclareMathOperator{\dpois}{Poisson} \DeclareMathOperator{\dnorm}{Normal} \DeclareMathOperator{\dt}{t} \DeclareMathOperator{\dcauchy}{Cauchy} \DeclareMathOperator{\dexponential}{Exp} \DeclareMathOperator{\dgamma}{Gamma} \DeclareMathOperator{\dinvgamma}{InvGamma} \DeclareMathOperator{\invlogit}{InvLogit} \DeclareMathOperator{\logit}{Logit} \DeclareMathOperator{\ddirichlet}{Dirichlet} \DeclareMathOperator{\dbeta}{Beta}\)

Suppose we have two independent trials where the likelihood of death is binomial, \(y_i \mid p_0, p_1 \sim \dbinomial(n_i, p_i)\), \(i = 0, 1\). We will compare two different non-informative priors on the odds ratio

\[ \theta := \frac{p_0}{1 - p_0} / \frac{p_1}{1 - p_1} . \]

Here are the given data.

df <- tibble(
    cohort = c('control', 'treatment'),
    patients = c(674, 680),
    deaths = c(39, 22)
  ) %>% 
  mutate(survived = patients - deaths)
cohort patients deaths survived
control 674 39 635
treatment 680 22 658

We’ll need a couple of functions for drawing random samples for \(\theta\).

odds <- function(p) p / (1 - p)

simulate <- function(n, k, a, b, draws = 10000)
  # draws from posterior
  # n bernouille trials, k successes with beta(a, b) prior
  tibble(
    draw = 1:draws, 
    value = rbeta(draws, k + a, n - k + b)
  )

posterior <- function(a, b, draws = 10000)
  # random samples from theta posterior with beta(a, b) prior
  df %>% 
    transmute(
      cohort,
      draws = map2(patients, deaths, simulate, a, b, draws)
    ) %>% 
    unnest(draws) %>% 
    spread(cohort, value) %>% 
    mutate(theta = odds(control) / odds(treatment))

Let’s compare a uniform prior to a prior close to \(\dbeta(0, 0)\).

uni <- posterior(1, 1) %>% 
  mutate(prior = 'uniform')

zero <- posterior(0.000000001, 0.000000001) %>% 
  mutate(prior = 'zero')

posteriors <- bind_rows(uni, zero)

Here are the 95% posterior credible intervals for \(\theta\).

cis <- posteriors %>% 
  group_by(prior) %>% 
  summarise(
    q05 = quantile(theta, 0.05),
    q50 = quantile(theta, 0.5),
    q95 = quantile(theta, 0.95)
  )
prior q05 q50 q95
uniform 1.173093 1.807827 2.845203
zero 1.189055 1.842631 2.961087

The estimates with the “zero” prior are slightly higher than those from the uniform prior, especially in the tails.