@@ -680,7 +680,10 @@ gen_proposal_poisson <- function(y, X, e, b0 = 0, B0 = 100, t.max = 20) {
680680 d <- ncol(X )
681681
682682 beta_new <- matrix (c(log(mean(y / e )),rnorm(d - 1 )/ 10 ), nrow = d )
683-
683+ if (is.nan(beta_new [1 ])){
684+ beta.new [1 ]<- log(mean(y )/ mean(e ))
685+ }
686+
684687 XtXi <- lapply(seq_len(N ), function (i ) tcrossprod(X [i ,]))
685688 B0.inv <- solve(B0 )
686689 for (t in seq_len(t.max )) {
@@ -1046,7 +1049,7 @@ b0=rep(0,d)
10461049B0 = diag(100 ,d )
10471050
10481051pri_alpha <- data.frame (shape = 2 , rate = 0.5 )
1049- c_alpha = 0.1
1052+ c_alpha = 0.3
10501053
10511054M <- 50000L / mcmcspeedup
10521055
@@ -1066,16 +1069,16 @@ knitr::kable(round(res_negbin_full, 3))
10661069
10671070| | 2.5% | Posterior mean | 97.5% | IF |
10681071| :-------------| -------:| ---------------:| -------:| -------:|
1069- | intercept | -8.382 | -8.216 | -8.059 | 1.605 |
1070- | intervention | -0.592 | -0.363 | -0.138 | 1.473 |
1071- | holiday | -1.181 | -0.786 | -0.417 | 1.525 |
1072- | alpha | 6.397 | 12.240 | 21.899 | 83.470 |
1072+ | intercept | -8.377 | -8.216 | -8.059 | 2.066 |
1073+ | intervention | -0.593 | -0.361 | -0.139 | 1.537 |
1074+ | holiday | -1.181 | -0.789 | -0.412 | 1.494 |
1075+ | alpha | 6.814 | 12.394 | 20.184 | 42.790 |
10731076
10741077``` r
10751078
10761079
10771080c(mean(res1 $ acc_beta ), mean(res1 $ acc_alpha ))
1078- # > [1] 0.9424 0.7130
1081+ # > [1] 0.9374 0.3806
10791082```
10801083
10811084Next we run the partially marginalized Gibbs sampler under the same
@@ -1096,18 +1099,18 @@ res_negbin_partial <- cbind(res_negbin_partial, IF = IF_res2)
10961099knitr :: kable(round(res_negbin_partial , 3 ))
10971100```
10981101
1099- | | 2.5% | Posterior mean | 97.5% | IF |
1100- | :-------------| -------:| ---------------:| -------:| ------- :|
1101- | intercept | -8.372 | -8.216 | -8.059 | 1.630 |
1102- | intervention | -0.586 | -0.361 | -0.136 | 1.417 |
1103- | holiday | -1.219 | -0.792 | -0.404 | 1.848 |
1104- | alpha | 5.945 | 11.791 | 19.973 | 45.943 |
1102+ | | 2.5% | Posterior mean | 97.5% | IF |
1103+ | :-------------| -------:| ---------------:| -------:| ------:|
1104+ | intercept | -8.372 | -8.217 | -8.063 | 1.678 |
1105+ | intervention | -0.591 | -0.361 | -0.132 | 1.607 |
1106+ | holiday | -1.193 | -0.787 | -0.410 | 1.575 |
1107+ | alpha | 6.419 | 12.519 | 22.179 | 9.119 |
11051108
11061109``` r
11071110
11081111
11091112c(mean(res2 $ acc_beta ), mean(res2 $ acc_alpha ))
1110- # > [1] 0.9344 0.8902
1113+ # > [1] 0.9334 0.6988
11111114```
11121115
11131116Both samplers yield essentially the same estimation results, which is to
@@ -1122,7 +1125,7 @@ sampler.
11221125
11231126### Section 8.2.3: Evaluating MCMC samplers
11241127
1125- #### Example 8.10 Verifying the correctness of the full conditional MCMC samper
1128+ #### Example 8.10 Verifying the correctness of the full conditional MCMC sampler
11261129
11271130To check the MCMC algorithm for correctness, we extend the sampler by
11281131adding sampling the data from the prior as a further sampling step.
@@ -1556,24 +1559,24 @@ the prior distribution.
15561559
15571560``` r
15581561
1559- M <- 300000 / (10 * mcmcspeedup )
1562+ M <- 1000000 / (10 * mcmcspeedup )
15601563set.seed(1234 )
15611564res_check_full <- negbin_check_cba(X , e , b0 , B0 , pri_alpha , c_alpha ,
15621565 full_gibbs = TRUE , M = M )
1563- h = 300
1566+ h = 1000
15641567thin = seq(from = 1 , to = M ,by = h )
15651568
15661569mu1 = exp(res_check_full $ beta_post [,1 ])
15671570ov1 <- (mu1 ^ 2 / res_check_full $ alpha_post )[thin ]
15681571print(coda :: effectiveSize(ov1 ))
1569- # > var1
1570- # > 10
1572+ # > var1
1573+ # > 24.30867
15711574
15721575mu2 = exp(res_check_full $ beta_post [,1 ]+ res_check_full $ beta_post [,2 ])
15731576ov2 <- (mu2 ^ 2 / res_check_full $ alpha_post )[thin ]
15741577print(coda :: effectiveSize(ov2 ))
1575- # > var1
1576- # > 9.454177
1578+ # > var1
1579+ # > 10
15771580
15781581set.seed(1234 )
15791582beta_prior <- t(mvtnorm :: rmvnorm(M / h , mean = b0 , sigma = B0 ))
@@ -1616,8 +1619,8 @@ res_partial <- negbin_check(X, e, b0, B0, pri_alpha, c_alpha,
16161619mu1 = exp(res_partial $ beta_post [,1 ])
16171620ov1 <- ((mu1 ^ 2 )/ res_partial $ alpha_post )[thin ]
16181621print(coda :: effectiveSize(ov1 ))
1619- # > var1
1620- # > 44.37082
1622+ # > var1
1623+ # > 10
16211624
16221625mu2 = exp(res_partial $ beta_post [,1 ]+ res_partial $ beta_post [,2 ])
16231626ov2 <- ((mu2 ^ 2 )/ res_partial $ alpha_post )[thin ]
@@ -1640,7 +1643,7 @@ text(5,0.1, paste0('KS-test: p-value= ', round(ks2$p.value,4)))
16401643
16411644![ ] ( Chapter08_files/figure-html/unnamed-chunk-52-1.png )
16421645
1643- We next run the ivalid partially marginalised Gibbs sampler.
1646+ We next run the invalid partially marginalised Gibbs sampler.
16441647
16451648``` r
16461649
@@ -1651,8 +1654,8 @@ res_check_partial <- negbin_check_cba(X, e, b0, B0, pri_alpha, c_alpha,
16511654mu1 = exp(res_check_partial $ beta_post [,1 ])
16521655ov1 <- ((mu1 ^ 2 )/ res_check_partial $ alpha_post )[thin ]
16531656print(coda :: effectiveSize(ov1 ))
1654- # > var1
1655- # > 33.0562
1657+ # > var1
1658+ # > 10
16561659
16571660mu2 = exp(res_check_partial $ beta_post [,1 ]+ res_check_partial $ beta_post [,2 ])
16581661ov2 <- ((mu2 ^ 2 )/ res_check_partial $ alpha_post )[thin ]
@@ -1682,6 +1685,15 @@ $`\beta_1`$ versus the heterogeneity parameter $`\alpha`$
16821685
16831686``` r
16841687
1688+ plot(res_check_full $ beta_post [thin ,1 ],res_check_full $ beta_post [thin ,2 ],
1689+ xlab = " beta1" , ylab = " beta2" ,main = " full" ,xlim = c(- 1 ,2 ),
1690+ ylim = c(0.5 ,3.5 ))
1691+ plot(res_partial $ beta_post [thin ,1 ],res_partial $ beta_post [thin ,2 ],
1692+ xlab = " beta1" , ylab = " beta2" ,main = " correct partial" ,xlim = c(- 1 ,2 ), ylim = c(0.5 ,3.5 ))
1693+ plot(res_check_partial $ beta_post [thin ,1 ],res_check_partial $ beta_post [thin ,2 ],
1694+ xlab = " beta1" , ylab = " beta2" ,main = " incorrect partial" ,xlim = c(- 1 ,2 ),
1695+ ylim = c(0.5 ,3.5 ))
1696+
16851697plot(res_check_full $ beta_post [thin ,1 ],res_check_full $ alpha_post [thin ],
16861698 xlab = " beta1" , ylab = " alpha" ,main = " full" ,xlim = c(- 1 ,2 ), ylim = c(0 ,1.4 ))
16871699plot(res_partial $ beta_post [thin ,1 ],res_partial $ alpha_post [thin ],
@@ -1690,11 +1702,11 @@ plot(res_check_partial$beta_post[thin,1],res_check_partial$alpha_post[thin],
16901702 xlab = " beta1" , ylab = " alpha" ,main = " incorrect partial" ,xlim = c(- 1 ,2 ), ylim = c(0 ,1.4 ))
16911703
16921704plot(res_check_full $ beta_post [thin ,2 ],res_check_full $ alpha_post [thin ],
1693- xlab = " beta2" , ylab = " alpha" ,xlim = c(0.5 ,3.5 ), ylim = c(0 ,1.4 ))
1705+ xlab = " beta2" , ylab = " alpha" ,main = " full " , xlim = c(0.5 ,3.5 ), ylim = c(0 ,1.4 ))
16941706plot(res_partial $ beta_post [thin ,2 ],res_partial $ alpha_post [thin ],
1695- xlab = " beta2" , ylab = " alpha" ,xlim = c(0.5 ,3.5 ), ylim = c(0 ,1.4 ))
1707+ xlab = " beta2" , ylab = " alpha" ,main = " correct partial " , xlim = c(0.5 ,3.5 ), ylim = c(0 ,1.4 ))
16961708plot(res_check_partial $ beta_post [thin ,2 ],res_check_partial $ alpha_post [thin ],
1697- xlab = " beta2" , ylab = " alpha" ,xlim = c(0.5 ,3.5 ), ylim = c(0 ,1.4 ))
1709+ xlab = " beta2" , ylab = " alpha" ,main = " incorrect partial " , xlim = c(0.5 ,3.5 ), ylim = c(0 ,1.4 ))
16981710```
16991711
17001712![ ] ( Chapter08_files/figure-html/unnamed-chunk-54-1.png ) \# Section 8.3:
0 commit comments