2 title: "Sequential Bayes: Utilizing the Posterior Distribution"
3 output: rmarkdown::html_vignette
5 %\VignetteIndexEntry{Sequential Bayes: Utilizing the Posterior Distribution}
6 %\VignetteEngine{knitr::rmarkdown}
7 %\VignetteEncoding{UTF-8}
10 ```{r, include = FALSE}
11 knitr::opts_chunk$set(
17 ```{r setup, include = FALSE}
21 In the Sequential Bayes method, the probability distribution of R0 is updated
22 sequentially from one case count to the next, starting from a (discretized)
23 uniform prior. By default, the function `seq_bayes()` returns the mean of the
24 last updated posterior distribution as its estimate of R0. However, by setting
25 the parameter `post` to `TRUE`, it is possible to return the final distribution
30 cases <- c(1, 4, 10, 5, 3, 4, 19, 3, 3, 14, 4)
32 posterior <- seq_bayes(cases, mu = 8, kappa = 7, post = TRUE)
35 First, the distribution can be used to retrieve the original estimate (had
36 `post` been left to its default value of `FALSE`) by calculating its mean:
39 # `supp` is the support of the distribution, and `pmf` is its probability mass
41 post_mean <- sum(posterior$supp * posterior$pmf)
44 # Verify that the following is true:
45 post_mean == seq_bayes(cases, mu = 8, kappa = 7)
48 Another use of the posterior is to obtain an alternative estimate of R0. For
49 instance, the following extracts the posterior mode rather than the mean:
52 post_mode <- posterior$supp[which.max(posterior$pmf)]
56 Returning the posterior is suitable for visualization purposes. Below is a graph
57 containing the uniform prior, final posterior distribution, posterior mean and
60 ```{r, dpi = 192, echo = FALSE}
61 par(mar = c(4.1, 4.1, 0.5, 0.5))
64 plot(posterior$supp, posterior$pmf, xlab = "x", ylab = "p(x)",
65 col = "black", lty = 1, type = "l"
68 segments(x0 = 0, x1 = 7, y0 = 1 / (7 / 0.01 + 1), y1 = 1 / (7 / 0.01 + 1),
72 abline(v = post_mean, col = "blue", lty = 2)
74 abline(v = post_mode, col = "red", lty = 2)
77 legend = c("Prior", "Posterior", "Posterior mean", "Posterior mode"),
78 col = c("orange", "black", "blue", "red"),
79 lty = c(1, 1, 2, 2), cex = 0.5