Skip to content

Commit 275cc5f

Browse files
committed
stylistic changes
1 parent 35735eb commit 275cc5f

2 files changed

Lines changed: 47 additions & 34 deletions

File tree

vignettes/Chapter08.Rmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ gen.proposal.poisson <- function(y, X, e, b0 = 0, B0 = 100, t.max = 20) {
607607
betas <- matrix(NA_real_, ncol = t.max, nrow = d)
608608
beta.new <- matrix(c(log(mean(y/e)), rep(0, d - 1)), nrow = d)
609609
610-
B0.inv=solve(B0)
610+
B0.inv <- solve(B0)
611611
for (t in seq_len(t.max)) {
612612
beta.old <- beta.new
613613

vignettes/Chapter09.Rmd

Lines changed: 46 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ knitr::opts_chunk$set(
1717
fig.height = 6
1818
)
1919
knitr::opts_knit$set(global.par = TRUE)
20-
pdfplots <- FALSE # default: FALSE; set this to TRUE only if you like pdf figures
20+
pdfplots <- TRUE # default: FALSE; set this to TRUE only if you like pdf figures
2121
```
2222

2323
```{r, include = FALSE}
@@ -349,8 +349,12 @@ points(pk3, col = 3, cex = 1.5, pch = 16)
349349
# Section 9.4 Posterior Predictive Distributions in Regression Analysis
350350

351351
## Example 9.12: Road Safety Data; potential outcome analysis
352-
We are now interested in predicting the number of children who would have been killed or seriously injured without the legal intervention on October 1, 1994.
353-
To do so we reuse functions defined in Example 8.8.
352+
353+
We are now interested in predicting the number of children who would
354+
have been killed or seriously injured without the legal intervention
355+
on October 1, 1994. To do so we reuse functions defined in Example
356+
8.8.
357+
354358
```{r}
355359
gen.proposal.poisson <- function(y, X, e, b0 = 0, B0 = 100, t.max = 20) {
356360
N <- length(y)
@@ -359,7 +363,7 @@ gen.proposal.poisson <- function(y, X, e, b0 = 0, B0 = 100, t.max = 20) {
359363
betas <- matrix(NA_real_, ncol = t.max, nrow = d)
360364
beta.new <- matrix(c(log(mean(y/e)), rep(0, d - 1)), nrow = d)
361365
362-
B0.inv=solve(B0)
366+
B0.inv <- solve(B0)
363367
for (t in seq_len(t.max)) {
364368
beta.old <- beta.new
365369
@@ -417,7 +421,7 @@ sample_beta<- function(y,X,e, b0, B0, qmean, qvar, beta.old){
417421
beta <- beta.old
418422
acc <- 0
419423
}
420-
return(res = list(beta=beta, acc=acc))
424+
return(res = list(beta = beta, acc = acc))
421425
}
422426
423427
poisson <- function(y, X, e, b0 = 0, B0 = 100, burnin = 1000L, M = 10000L) {
@@ -449,36 +453,41 @@ poisson <- function(y, X, e, b0 = 0, B0 = 100, burnin = 1000L, M = 10000L) {
449453
return(list(beta.post = beta.post, accept = mean(acc)))
450454
}
451455
```
452-
Our goal is to predict the number of killed or seriously injured children
453-
from October 1994 (i.e when the legal intervention giving priority to pedestrians became effective) using only data before that time point. Hence we estimate the model in Example 8.8. with an intercept and a holiday effect using only the information up to September 1994.
456+
457+
Our goal is to predict the number of killed or seriously injured
458+
children from October 1994 onward (i.e., when the legal intervention
459+
giving priority to pedestrians became effective) using only data
460+
before that time point. Hence we estimate the model in Example
461+
8.8. with an intercept and a holiday effect using only the information
462+
up to September 1994.
454463

455464
```{r}
456465
data("accidents", package = "BayesianLearningCode")
457-
y <- accidents[1:93, "children_accidents"]
458-
t=length(y)
459-
e <- accidents[1:93, "children_exposure"]
466+
y <- window(accidents[, "children_accidents"], end = c(1994, 9))
467+
e <- window(accidents[, "children_exposure"], end = c(1994, 9))
468+
t <- length(y)
460469
461470
X <- cbind(intercept = rep(1, length(y)),
462-
holiday = c(rep(rep(c(0, 1, 0), c(6, 2, 4)),7), rep(0,6), rep(1,2),0))
463-
M=10000
471+
holiday = rep(rep(c(0, 1, 0), c(6, 2, 4)), length.out = t))
472+
M <- 10000
464473
set.seed(1)
465-
res <- poisson(y, X, e, b0 = 0, B0 = 100,M=M)
474+
res <- poisson(y, X, e, b0 = 0, B0 = 100, M = M)
466475
```
467476

468477
We define the covariates for the time points from October 1994 on and use the
469478
draws from the posterior distribution to predict the values of the time series.
470479

471480
```{r}
472-
X.pred <- cbind(intercept = rep(1, 99),
473-
holiday = c( rep(0,3),rep(rep(c(0, 1, 0), c(6, 2, 4)),8)))
474-
e.pred <- accidents[94:192, "children_exposure"]
475-
t.pred=length(e.pred)
481+
e.pred <- window(accidents[, "children_exposure"], start = c(1994, 10))
482+
t.pred <- length(e.pred)
483+
X.pred <- cbind(intercept = rep(1, t.pred),
484+
holiday = c(rep(0, 3), rep(rep(c(0, 1, 0), c(6, 2, 4)),
485+
length.out = t.pred - 3)))
476486
477-
lambda = matrix(e.pred, ncol=M, nrow=t.pred)*exp(X.pred%*%t(res$beta.post))
487+
lambda <- matrix(e.pred, ncol = M, nrow = t.pred) * exp(X.pred %*% t(res$beta.post))
478488
479489
set.seed(2)
480-
pred<-matrix(rpois(M*t.pred, lambda), ncol=M, nrow=t.pred)
481-
490+
pred <- matrix(rpois(M * t.pred, lambda), ncol = M, nrow = t.pred)
482491
```
483492

484493
We reuse also the function to determine mean and quantiles of the draws.
@@ -489,35 +498,39 @@ res.mcmc <- function(x, lower = 0.025, upper = 0.975) {
489498
paste0(upper * 100, "%"))
490499
res
491500
}
492-
pred.int<-t(round(apply(pred, 1, res.mcmc), 3))
501+
pred.int <- t(round(apply(pred, 1, res.mcmc), 3))
493502
```
494503

495-
Then we plot the predictive mean together with the (equal-tailed) 95\% prediction intervals.
504+
Then we plot the predictive mean together with the (equal-tailed) 95%
505+
prediction intervals.
496506

497507
```{r, echo = -c(1:2)}
498508
if (pdfplots) {
499509
pdf("9-4_1.pdf", width = 8, height = 4)
500510
par(mar = c(2.7, 2, 1.5, .5), mgp = c(1.6, .6, 0))
501511
}
502512
par(mfrow = c(1, 1))
503-
plot(1:192, accidents[,"children_accidents"], type="p" ,ylim=c(0,7),
504-
xaxt="n",xlab="", ylab="",
505-
main="Number of children killed or seriously injured")
506-
axis(1, at = seq(1, 192, by = 12), las=2, labels=1987:2002)
507-
matplot(94:192, pred.int, col="blue", type="l",lty=c(2,1,2),lwd=2,add=T)
508-
abline(v=94, col="red")
509-
513+
plot(time(accidents), accidents[, "children_accidents"], type = "p", ylim = c(0, 7),
514+
xlab = "", ylab = "",
515+
main = "Number of children killed or seriously injured")
516+
matplot(as.vector(time(e.pred)), pred.int, col = "blue", type = "l",
517+
lty = c(2, 1, 2), lwd = 2, add = TRUE)
518+
abline(v = 1994.75, col = "red")
510519
```
511520

512-
We see that the prediction intervals after the intervention are much too wide which again indicates that there is an intervention effect. Note that whereas the risk
513-
for a child to be killed or seriously injured in this model is constant the predicted mean number of killed and seriously injured children decreases from 1994 due to the decreasing number of exposed in that time period.
521+
We see that the prediction intervals after the intervention are much
522+
too wide which again indicates that there is an intervention
523+
effect. Note that whereas the risk for a child to be killed or
524+
seriously injured in this model is constant the predicted mean number
525+
of killed and seriously injured children decreases from 1994 due to
526+
the decreasing number of exposures in that time period.
514527

515528
# Section 9.5: Bayesian Forecasting of Time Series
516529

517530
## Example 9.13: US GDP data - one-step-ahead forecasting
518531

519-
For creating the design matrix for an AR model, we re-use the function from
520-
Chapter 7.
532+
For creating the design matrix for an AR model, we re-use the function
533+
from Chapter 7.
521534

522535
```{r}
523536
ARdesignmatrix <- function(dat, p = 1) {

0 commit comments

Comments
 (0)