2.5 Posterior simulation
We can also validate the posterior model derived using the Bayes’ Rule via simulation.
Start off with specifying the prior and the likelihoods.
prior <- list(fake = 0.4, real = 0.6)
likelihood <- list(fake = 0.2667, real = 0.0222)We then simulate 10,000 articles as either “real” or “fake” based on the prior probabilities.
set.seed(84735)
articles <- sample_n(tibble(type = c("fake", "real")), size = 10000,
weight = prior, replace = TRUE)| Summary of simulated articles | ||
| type | n | percent |
|---|---|---|
| fake | 4031 | 40.3% |
| real | 5969 | 59.7% |
| Total | 10000 | 100.0% |
Against these simulated we add an attribute - “does it have an exclamation (!) mark” based on the likelihoods.
articles <- articles |>
mutate(data_model = if_else(type == "fake", likelihood$fake,
likelihood$real)) |>
rowwise() |>
mutate(has_exclam = sample(c(T, F), 1, prob = c(data_model, 1-data_model)))Finally, we filter for those articles which have an exclamation mark and compute the probabilitiy of them being “fake” or “real”. This gives us the posterior probability (simulated).
posterior_prob_sim <-
articles |>
filter(has_exclam) |>
tabyl(type) |>
adorn_pct_formatting(digits = 1) |>
pull(percent) | Fake | Real | |
|---|---|---|
| prior | 40.0% | 60.0% |
| likelihood | 26.7% | 2.2% |
| posterior | 88.9% | 11.1% |
| posterior (simulated) | 89.0% | 11.0% |