Skip to content

Commit 82ccce2

Browse files
committed
new example: yearly maxima
1 parent 9a2d7b5 commit 82ccce2

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

vignettes/Chapter09.Rmd

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,3 +242,36 @@ abline(h = q_normal, col = 4, lty = 1, lwd = 1.5)
242242
abline(h = q_t, col = 2, lty = 2, lwd = 1.5)
243243
legend("topleft", c("Normal", "Student t"), lty = 1:2, col = c(4,2), lwd = 1.5)
244244
```
245+
246+
247+
## Example 9.9: Predicting yearly maxima for the road safety data
248+
249+
We use a sampling-based approach to obtain draws from posterior predictive
250+
by first drawing from the posterior $\mu|\mathbb{y} \sim \mathcal{G}(a_N, b_N)$.
251+
Then, using these draws as mean parameters for the Poisson likelihood, we draw
252+
12 times each to obtain yearly predictions. Of these, we take the maxima.
253+
254+
```{r}
255+
set.seed(1)
256+
y <- accidents[, "seniors_accidents"]
257+
aN <- sum(y) + 1
258+
bN <- length(y)
259+
mus <- rgamma(ndraws, aN, bN)
260+
yfs <- matrix(rpois(12 * ndraws, mus), ncol = 12)
261+
Us <- apply(yfs, 1, max)
262+
```
263+
264+
Now we visualize.
265+
266+
```{r, echo = -c(1:2)}
267+
if (pdfplots) {
268+
pdf("9-1_5.pdf", width = 10, height = 4)
269+
par(mar = c(2.5, 1.5, .1, .1), mgp = c(1.6, .6, 0))
270+
}
271+
par(mfrow = c(1, 2))
272+
plot(tab <- proportions(table(Us)), xlab = "U", ylab = "")
273+
plot(as.table(cumsum(tab)), type = "h", xlab = "U", ylab = "")
274+
probs <- c(0.025, 0.975)
275+
abline(h = probs, lty = 3)
276+
mtext(probs, side = 2, at = probs, adj = c(0, 1), cex = .8, col = "dimgrey")
277+
```

0 commit comments

Comments
 (0)