@@ -604,7 +604,7 @@ gen_proposal_poisson <- function(y, X, e, b0 = 0, B0 = 100, t.max = 20) {
604604 N <- length(y)
605605 d <- ncol(X)
606606
607- beta_new <- matrix(c(log(mean(y/e)), rep(0, d - 1)), nrow = d)
607+ beta_new <- matrix(c(log(mean(y/e)),rnorm( d - 1)/10 ), nrow = d)
608608
609609 XtXi <- lapply(seq_len(N), function(i) tcrossprod(X[i,]))
610610 B0.inv <- solve(B0)
@@ -723,7 +723,7 @@ poisson <- function(y, X, e, b0 = 0, B0 = 100, burnin = 1000L, M = 10000L) {
723723}
724724```
725725
726- We perform MCMC and report the results.
726+ We perform MCMC for !0000 iterations after a burnin of 1000 and report the results.
727727
728728``` {r}
729729set.seed(1234)
@@ -776,7 +776,7 @@ knitr::kable(res_poisson2)
776776With 2.8 deadly or seriously injured children per 10000 at risk, the
777777estimated baseline risk is very similar to that from Model 1. Also the
778778estimated intervention effect is very similar in both models,
779- indicating a reduction of the risk by a factor of 0.69 in Model 2
779+ indicating a reduction of the risk by a factor of 0.692 in Model 2
780780(compared to 0.697 in Model 1). The monthly effects have rather wide
78178195\% HPD intervals that cover 0 for all months except for July and
782782August. For these two holiday months they are clearly negative,
@@ -908,7 +908,7 @@ negbin<- function(y,X,e, b0,B0, pri_alpha,c_alpha,
908908
909909We use the same Normal prior as in the Poisson model for the
910910regression effects $\boldsymbol{\beta}$ and a Gamma prior
911- $\mathcal{G}(2 , 0.5)$ for $\alpha$. We first run the full Gibbs sampler for $M=10 ,000$ iterations after a burn-in of 1000.
911+ $\mathcal{G}(2 , 0.5)$ for $\alpha$. We first run the full Gibbs sampler for $M=50 ,000$ iterations after a burn-in of 1000.
912912
913913``` {r,negbin}
914914d <- ncol(X)
@@ -966,14 +966,14 @@ sampler.
966966
967967
968968## Section 8.2.3: Evaluating MCMC samplers
969+
969970### Example 8.10 Verifying the correctness of the full conditional MCMC samper
970971
971972To check the MCMC algorithm for correctness, we extend the sampler by adding sampling the data from the prior as a further sampling step.
972973
973974``` {r}
974975negbin_check <- function(X, e, b0, B0, pri_alpha, c_alpha,
975- full_gibbs = TRUE, burnin = 1000L,
976- M = 50000L) {
976+ full_gibbs = TRUE, burnin = 1000L, M = 50000L) {
977977
978978 N <- nrow(X)
979979 d <- ncol(X)
@@ -1338,13 +1338,13 @@ negbin_check_cba <- function(X, e, b0, B0, pri_alpha, c_alpha,
13381338 alpha_draw <- sample_alpha(y, e * exp(linpred), phi, pri_alpha, alpha,
13391339 c_alpha, full_gibbs)
13401340 alpha <- alpha_draw$alpha
1341-
1341+
13421342 # Step a
13431343 parms_proposal <- gen_proposal_poisson(y, X, e * phi, b0, B0)
13441344 beta_draw <- sample_beta(y, X, e * phi, b0, B0, parms_proposal$mean,
13451345 parms_proposal$var, beta)
13461346 beta <- beta_draw$beta
1347-
1347+
13481348 # Save the draws
13491349 if (m > burnin) {
13501350 beta_post[m - burnin, ] <- beta
@@ -1369,14 +1369,15 @@ X <- cbind(rep(1, N), c(rep(0,N/2),rep(1,N/2)))
13691369e <- rep(1, N)
13701370
13711371d <- ncol(X)
1372- b0 <- c(0,1 )
1373- B0 <- diag(0.25 , d)
1372+ b0 <- c(0.5,2 )
1373+ B0 <- diag(0.2 , d)
13741374
1375- pri_alpha <- data.frame(shape = 1. 5, rate = 3 )
1375+ pri_alpha <- data.frame(shape = 5, rate = 10 )
13761376c_alpha <- 0.35
13771377```
13781378We run the full Gibbs sampler under this scheme. To check the correctness of the
1379- sampler we focus on the overdispersion $\frac{\mu_i^2}{\alpha}$ computed from
1379+ sampler we focus on the overdispersion $\frac{\mu_i^2}{\alpha}$ for $x_i=0$ and
1380+ $x-i=1$ which we compute from
13801381the draws of the augmented MCMC sampler as well as from draws of the prior
13811382distribution.
13821383
@@ -1386,11 +1387,11 @@ if (pdfplots) {
13861387}
13871388par(mfrow = c(1, 2), mar = c(2.5, 2.5, 1.5, .1), mgp = c(1.5, .5, 0), lwd = 1.5)
13881389
1389- M=20000
1390+ M=30000
13901391set.seed(1234)
13911392res_check_full <- negbin_check_cba(X, e, b0, B0, pri_alpha, c_alpha,
13921393 full_gibbs = TRUE, M = M)
1393- h=200
1394+ h=300
13941395thin=seq(from=1, to=M,by=h)
13951396
13961397mu1=exp(res_check_full$beta_post[,1])
@@ -1404,21 +1405,26 @@ print(coda::effectiveSize(ov2))
14041405set.seed(1234)
14051406beta_prior <- t(mvtnorm::rmvnorm(M/h, mean = b0, sigma = B0))
14061407alpha_prior <- rgamma(M/h, shape = pri_alpha$shape,rate = pri_alpha$rate)
1407- ov1_pri<- exp(beta_prior[1,])^2/alpha_prior
1408- ov2_pri<- exp(beta_prior[1,]+beta_prior[2,])^2/alpha_prior
1408+ ov1_prior<- exp(beta_prior[1,])^2/alpha_prior
1409+ ov2_prior<- exp(beta_prior[1,]+beta_prior[2,])^2/alpha_prior
1410+ print(coda::effectiveSize(ov2_prior))
1411+ print(coda::effectiveSize(ov1_prior))
14091412
1410- ks1<- ks.test(ov1_pri ,ov1)
1411- qqplot(ov1_pri, ov1,xlab = "Prior",xlim=c(0,50 ), ylim=c(0,50 ),
1413+ ks1<- ks.test(ov1_prior ,ov1)
1414+ qqplot(log(ov1_prior), log( ov1) ,xlab = "Prior",xlim=c(0,6 ), ylim=c(0,6 ),
14121415 ylab = "Posterior", main = "Overdispersion for X=0")
14131416abline(a = 0, b = 1)
1414- text(30 ,0.1, paste0('KS-test: p-value= ', round(ks1$p.value,4)))
1417+ text(3 ,0.1, paste0('KS-test: p-value= ', round(ks1$p.value,4)))
14151418
1416- ks2<- ks.test(ov2_pri ,ov2)
1417- qqplot(ov2_pri, ov2, xlab = "Prior",xlim=c(0,300 ), ylim=c(0,300 ),
1419+ ks2<- ks.test(ov2_prior ,ov2)
1420+ qqplot(log(ov2_prior),log( ov2) , xlab = "Prior",xlim=c(0,10 ), ylim=c(0,10 ),
14181421 ylab = "Posterior", main = "Overdispersion for X=1")
14191422abline(a = 0, b = 1)
1420- text(200 ,0.1, paste0('KS-test: p-value= ', round(ks2$p.value,4)))
1423+ text(5 ,0.1, paste0('KS-test: p-value= ', round(ks2$p.value,4)))
14211424```
1425+ Both p_values are larger than 0.05, which is as expected for the full-conditional
1426+ Gibbs sampler.
1427+
14221428Now we check the partially marginalised Gibbs sampler.
14231429``` {r, echo = -c(1:3)}
14241430if (pdfplots) {
@@ -1431,26 +1437,29 @@ res_check_partial <- negbin_check_cba(X, e, b0, B0, pri_alpha, c_alpha,
14311437 full_gibbs = FALSE, M = M)
14321438
14331439mu1=exp(res_check_partial$beta_post[,1])
1434- ov1<-(mu1^2/res_check_partial$alpha_post)[thin]
1440+ ov1<-(( mu1^2) /res_check_partial$alpha_post)[thin]
14351441print(coda::effectiveSize(ov1))
14361442
14371443mu2=exp(res_check_partial$beta_post[,1]+res_check_partial$beta_post[,2])
1438- ov2<-(mu2^2/res_check_partial$alpha_post)[thin]
1444+ ov2<-(( mu2^2) /res_check_partial$alpha_post)[thin]
14391445print(coda::effectiveSize(ov2))
14401446
1441- ks1<- ks.test(ov1_pri ,ov1)
1442- qqplot(ov1_pri, ov1,xlab = "Prior",xlim=c(0,30 ), ylim=c(0,30 ),
1447+ ks1<- ks.test(ov1_prior ,ov1)
1448+ qqplot(log(ov1_prior), log( ov1) ,xlab = "Prior",xlim=c(0,5 ), ylim=c(0,5 ),
14431449 ylab = "Posterior", main = "Overdispersion for X=0")
14441450abline(a = 0, b = 1)
1445- text(20 ,0.1, paste0('KS-test: p-value= ', round(ks1$p.value,4)))
1451+ text(3 ,0.1, paste0('KS-test: p-value= ', round(ks1$p.value,4)))
14461452
1447- ks2<- ks.test(ov2_pri ,ov2)
1448- qqplot(ov2_pri, ov2, xlab = "Prior",xlim=c(0,300 ), ylim=c(0,300 ),
1453+ ks2<- ks.test(ov2_prior ,ov2)
1454+ qqplot(log(ov2_prior),log( ov2) , xlab = "Prior",xlim=c(0,10 ), ylim=c(0,10 ),
14491455 ylab = "Posterior", main = "Overdispersion for X=1")
14501456abline(a = 0, b = 1)
1451- text(200 ,0.1, paste0('KS-test: p-value= ', round(ks2$p.value,4)))
1457+ text(5 ,0.1, paste0('KS-test: p-value= ', round(ks2$p.value,4)))
14521458
14531459```
1460+ Also for the partially marginalised Gibbs sampler both p-values are larger than
1461+ 0.05 and hence we fail to detect that this sampler is wrong.
1462+
14541463
14551464# Section 8.3: Beyond i.i.d. Gaussian error distributions
14561465
0 commit comments