BDA3 Chapter 3 Exercise 2
Here’s my solution to exercise 2, chapter 3, of Gelman’s Bayesian Data Analysis (BDA), 3rd edition. There are solutions to some of the exercises on the book’s webpage.
| survey | bush | dukakis | other | total |
|---|---|---|---|---|
| pre-debate | 294 | 307 | 38 | 639 |
| post-debate | 288 | 332 | 19 | 639 |
Let θpre∼Dirichlet3(αpre) and θpost∼Dirichlet3(αpost) be the pre- and post-debate priors. Then the posteriors are
θpre∣y∼Dirichlet(294+α(pre,b),307+α(pre,d),38+α(pre,o))θpost∣y∼Dirichlet(288+α(post,b),332+α(post,d),19+α(post,o)).
Denote the proportion of support for Bush amongst Bush/Dukakis supporters by ϕ∙:=θ(∙,b)θ(∙,b)+θ(∙,d). The results of the previous exercise show that we can treat this as a beta-binomial problem by simply ignoring the results for other. More precisely,
ϕpre∣y∼Beta(294+α(pre,b),307+α(pre,d))ϕpost∣y∼Beta(288+α(post,b),332+α(post,d)).
Let’s plot these posteriors and their difference δ:=(ϕpost∣y)−(ϕpre∣y).
alpha_bush <- 1
alpha_dukakis <- 1
posteriors <- expand.grid(
value = seq(0, 1, 0.001),
survey = df$survey
) %>%
as_tibble() %>%
inner_join(df, by = 'survey') %>%
mutate(
post_bush = bush + alpha_bush,
post_dukakis = dukakis + alpha_dukakis,
density = dbeta(value, post_bush, post_dukakis)
) N <- 10000
shift <- expand.grid(
draw = 1:N,
survey = df$survey
) %>%
as_tibble() %>%
inner_join(df, by = 'survey') %>%
mutate(
post_bush = bush + alpha_bush,
post_dukakis = dukakis + alpha_dukakis,
value = rbeta(n(), post_bush, post_dukakis)
) %>%
select(draw, survey, value) %>%
spread(survey, value) %>%
mutate(difference = `post-debate` - `pre-debate`) There is a 19% probability (the area above 0) that there was a positive shift towards Bush.