@@ -566,15 +566,15 @@ knitr::kable(round(res_probit.labor * pi / sqrt(3), 3))
566566
567567### Example 8.8: Road safety data
568568
569- We fit two different Poisson regression models to the series of
569+ We will fit two different Poisson regression models to the series of
570570monthly deadly and seriously injured children aged 6-10 in Linz
571571introduced in Example 2.1:
572572
5735731 . a small model with intercept, intervention effect and holiday dummy
574574 (activated in July/August);
575575
5765762 . a larger model with intercept, intervention effect,
577- and a seasonal dummy variables for all months except for December.
577+ and a seasonal dummy variables for all months except December.
578578
579579The sampling performance for these two models is assessed to study how
580580the acceptance rate deteriorates, when the dimension of regression effects $d$
@@ -607,9 +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)), rep(0, d - 1)), nrow = d)
609609
610- b0 <- matrix(rep(b0, length.out = d), nrow = d)
611- B0.inv <- diag(rep(1 / B0, length.out = d), nrow = d)
612-
610+ B0.inv=solve(B0)
613611 for (t in seq_len(t.max)) {
614612 beta.old <- beta.new
615613
@@ -638,17 +636,16 @@ gen.proposal.poisson <- function(y, X, e, b0 = 0, B0 = 100, t.max = 20) {
638636
639637We use a rather flat normal independence prior
640638$\mathcal{N}(\mathbf{0}, 100 \mathbf{I})$ on the regression
641- effects. First we determine the parameters of the proposal
639+ effects and determine the parameters of the proposal
642640distribution.
643641
644642``` {r}
645- parms.proposal <- gen.proposal.poisson(y, X, e, b0 = 0, B0 = 100)
643+ d=ncol(X)
644+ parms.proposal <- gen.proposal.poisson(y, X, e, b0 = rep(0,d), B0 =diag(100,d))
646645parms.proposal
647646```
648647
649- To set up the independence Metropolis-Hastings algorithm for
650- the Poisson model, we first write a short program for the MH step
651- to sample the regression effects.
648+ To implement the independence Metropolis-Hastings algorithm we write a short program for the MH step for sampling the regression effects.
652649``` {r}
653650sample_beta<- function(y,X,e, b0, B0, qmean, qvar, beta.old){
654651
@@ -686,10 +683,11 @@ sample_beta<- function(y,X,e, b0, B0, qmean, qvar, beta.old){
686683}
687684```
688685
689- We use this program to sample from the posterior.
686+ Next we combine the determination of the proposal and the MH-sampling step
687+ in a program to sample from the posterior of a Poisson regression model.
688+
690689``` {r}
691- poisson <- function(y, X, e, b0 = 0, B0 = 100, qmean, qvar,
692- burnin = 1000L, M = 10000L) {
690+ poisson <- function(y, X, e, b0 = 0, B0 = 100, burnin = 1000L, M = 10000L) {
693691 d <- ncol(X)
694692
695693 b0 <- rep(b0, length.out = d)
@@ -699,6 +697,10 @@ poisson <- function(y, X, e, b0 = 0, B0 = 100, qmean, qvar,
699697 colnames(beta.post) <- colnames(X)
700698 acc <- numeric(length = M)
701699
700+ parms.proposal<- gen.proposal.poisson(y, X, e, b0 , B0)
701+ qmean <- parms.proposal$mean
702+ qvar<-parms.proposal$var
703+
702704 beta <- as.vector(mvtnorm::rmvnorm(1, mean = qmean, sigma = qvar))
703705
704706 for (m in seq_len(burnin + M)) {
@@ -719,8 +721,7 @@ We perform MCMC and report the results.
719721
720722``` {r}
721723set.seed(1234)
722- res1 <- poisson(y, X, e, b0 = 0, B0 = 100,
723- qmean = parms.proposal$mean, qvar = parms.proposal$var)
724+ res1 <- poisson(y, X, e, b0 = 0, B0 = 100)
724725
725726res.poisson1 <- cbind(t(round(apply(res1$beta.post, 2, res.mcmc), 3)),
726727 "exp(beta)" = round(exp(colMeans(res1$beta.post)), 5))
@@ -750,22 +751,14 @@ seas <- rbind(diag(1, 11), rep(0, 11))
750751seas.dummies <- matrix(rep(t(seas), 16), ncol = 11, byrow = TRUE)
751752colnames(seas.dummies) <- c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
752753 "Aug", "Sep", "Oct", "Nov")
753- X.large <- cbind(X[, -3],
754- seas.dummies)
754+ X.large <- cbind(X[, -3], seas.dummies)
755755```
756756
757- We set the prior parameters and compute the parameters of the proposal
758- distribution.
759-
760- ``` {r}
761- parms.proposal2 <- gen.proposal.poisson(y, X.large, e, b0 = 0, B0 = 100)
762- ```
763- Next we fit the model.
757+ We set the prior parameters and fit the model.
764758
765759``` {r}
766760set.seed(1234)
767- res2 <- poisson(y, X.large, e, b0 = 0, B0 = 100,
768- qmean = parms.proposal2$mean, qvar = parms.proposal2$var)
761+ res2 <- poisson(y, X.large, e, b0 = 0, B0 = 100)
769762
770763res.poisson2 <- cbind(t(round(apply(res2$beta.post, 2, res.mcmc), 3)),
771764 "exp(beta)" = round(exp(colMeans(res2$beta.post)), 5))
@@ -797,7 +790,7 @@ be estimated.
797790### Example 8.9: Road safety data
798791
799792Now we re-analyze the road safety data allowing for unobserved
800- heterogeneity. We first set up the two versions of the three-block
793+ heterogeneity. We will first set up the two versions of the three-block
801794MH-within-Gibbs sampler.
802795
803796
0 commit comments