@@ -739,6 +739,15 @@ Now we analyze the road safety data allowing for unobserved
739739heterogeneity. We first set up the two versions of the three-block
740740MH-within-Gibbs sampler.
741741
742+ Note that the negative binomial distribution in R is specified
743+ as
744+ $$ p(y|\alpha, p)={\alpha-1+y \choose \alpha-1} p^\alpha (1-p)^y $$
745+ or alternatively by the parameters $\alpha$ and its expected value $$ \mu=\alpha (1-p)/p. $$ The expected value of
746+ $p(y|\alpha, \beta)$ his given as
747+ $$ \mu=\alpha \frac{\frac{1}{1+\alpha/e \exp(- \mathbf{x}\boldsymbol{\beta})}}
748+ {\frac{\alpha/e \exp(- \mathbf{x}\boldsymbol{\beta})}{1+\alpha/e \exp(- \mathbf{x}\boldsymbol{\beta})}} = e \exp( \mathbf{x}\boldsymbol{\beta}), $$
749+ and we will use $\alpha$ and $\mu$ to specify the negative binomial distribution.
750+
742751``` {r}
743752negbin <- function(y, X, e, b0 = 0, B0 = 100, qmean, qvar, pri.alpha,
744753 full.gibbs = FALSE, burnin = 1000L, M = 50000L) {
@@ -779,12 +788,12 @@ negbin <- function(y, X, e, b0 = 0, B0 = 100, qmean, qvar, pri.alpha,
779788 lpri_old <- mvtnorm::dmvnorm(beta.old, mean = b0, sigma = B0, log = TRUE)
780789
781790 # Compute log likelihood of proposed and old value
782- lh_proposed <- dpois(y, exp(X %*% beta.proposed), log = TRUE)
783- lh_old <- dpois(y, exp(X %*% beta.old), log = TRUE)
784-
785- maxlik <- max(lh_old,lh_proposed)
791+ lh_proposed <- dpois(y, e * exp(X %*% beta.proposed), log = TRUE)
792+ lh_old <- dpois(y, e * exp(X %*% beta.old), log = TRUE)
793+
794+ maxlik <- max(lh_old, lh_proposed)
786795 ll <- sum(lh_proposed - maxlik) - sum(lh_old - maxlik)
787-
796+
788797 # Compute acceptance probability and accept or not
789798 log_acc <- min(0, ll + lpri_proposed - lpri_old + lq_old - lq_proposed)
790799
@@ -848,6 +857,7 @@ We specify the prior on $\alpha$ as the Gamma distribution with shape
8488572 and rate 0.5 and use both samplers to estimate the model parameters.
849858
850859``` {r}
860+ set.seed(1234)
851861pri.alpha <- data.frame(shape = 2, rate = 0.5)
852862
853863res1 <- negbin(y, X, e, qmean = parms.proposal$mean, qvar = parms.proposal$var,
0 commit comments