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) uniform prior. By
23 default, the function `seq_bayes` returns the mean of the last updated posterior
24 distribution as its estimate of R0. However, by setting the parameter `post` to
25 `TRUE`, it is possible to return the final distribution itself:
29 cases <- c(1, 4, 10, 5, 3, 4, 19, 3, 3, 14, 4)
31 posterior <- seq_bayes(cases, mu = 8, kappa = 7, post = TRUE)
34 First, the distribution can be used to retrieve the original estimate (had
35 `post` been left to its default value of `FALSE`) by calculating its mean:
38 # `supp` is the support of the distribution, and `pmf` is its probability mass
40 post_mean <- sum(posterior$supp * posterior$pmf)
43 # Verify that the following is true:
44 post_mean == seq_bayes(cases, mu = 8, kappa = 7)
47 Another use of the posterior is to obtain an alternative estimate of R0. For
48 instance, the following extracts the posterior mode rather than the mean:
51 post_mode <- posterior$supp[which.max(posterior$pmf)]
55 Returning the posterior is suitable for visualization purposes. Below is a graph
56 containing the uniform prior, final posterior distribution, posterior mean and
59 ```{r, dpi = 192, echo = FALSE}
60 par(mar = c(4.1, 4.1, 0.5, 0.5))
63 plot(posterior$supp, posterior$pmf, xlab = "x", ylab = "p(x)",
64 col = "black", lty = 1, type = "l"
67 segments(x0 = 0, x1 = 7, y0 = 1 / (7 / 0.01 + 1), y1 = 1 / (7 / 0.01 + 1),
71 abline(v = post_mean, col = "blue", lty = 2)
73 abline(v = post_mode, col = "red", lty = 2)
76 legend = c("Prior", "Posterior", "Posterior mean", "Posterior mode"),
77 col = c("orange", "black", "blue", "red"),
78 lty = c(1, 1, 2, 2), cex = 0.5