- Diasorin Example Continued
- Inference for Rates
- Two-Sample Poisson Exercise
set.seed(1) n1 <- 19 n2 <- 15 low <- c(91, 46, 95, 60, 33, 410, 105, 43, 189,1097, 54,178, 114, 137, 233, 101, 25,70,357) normal <- c(370, 267, 99,157, 75,1281, 48, 298, 268, 62,804,430,171,694,404) diasorin <- c(low,normal) group <- c(rep(1,n1),rep(2,n2)) group1 <- factor(group, 1:2, c('Low','Normal'))
gibbs <- function(reps, mu, tau, y.bar, s2, n, a, b, c, d){ output <- matrix(0, nrow=reps, ncol=2) a.gamma <- c+n/2 for(i in 1:reps){ mu.hat <- n * tau / (n * tau + b) * y.bar + b / (n * tau + b) * a sd.norm <- 1/sqrt(n*tau + b) mu <- rnorm(1, mu.hat, sd.norm) b.gamma <- d + ((n-1)*s2 + (y.bar - mu)^2)/2 tau <- rgamma(1, a.gamma, b.gamma) output[i,] <- c(mu, tau) } return(output) }
y.bar.l <- mean(log(low)) s2.l <- var(log(low)) y.bar.n <- mean(log(normal)) s2.n <- var(log(normal)) sample.l <- gibbs(50000, mu=5, tau=1, y.bar = y.bar.l, s2 = s2.l, n = n1, a=4.87, b=0.00288, c=1.0376, d=0.001) sample.n <- gibbs(50000, mu=5, tau=1, y.bar = y.bar.n, s2 = s2.n, n = n2, a=5.39, b=0.00280, c=1.04653, d=0.001)
mu1 <- sample.l[,1] tau1 <- sample.l[,2] mu2 <- sample.n[,1] tau2 <- sample.n[,2] mu.diff <- mu2-mu1 tau.ratio <- tau2/tau1 post.sum <- function(vec){ return(c(mean(vec), sd(vec), quantile(vec, probs=c(0.25, .5, .975)))) } sum.table <- rbind(post.sum(mu1), post.sum(mu2), post.sum(mu.diff), post.sum(tau1), post.sum(tau2), post.sum(tau.ratio)) rownames(sum.table) <- c("mu1", "mu2", "mu2-mu1", "tau1", "tau2", "tau2/tau1") colnames(sum.table) <- c("mean", "sd", "2.5%", "50%", "97.5%")
round(sum.table, 3)
## mean sd 2.5% 50% 97.5% ## mu1 4.705 0.210 4.566 4.705 5.117 ## mu2 5.489 0.241 5.332 5.489 5.968 ## mu2-mu1 0.784 0.319 0.572 0.784 1.415 ## tau1 1.316 0.406 1.024 1.274 2.227 ## tau2 1.295 0.443 0.976 1.246 2.299 ## tau2/tau1 1.088 0.543 0.711 0.975 2.439
plot(density(mu1,n=1500), type='l', xlab="", main="", ylab="", xlim=c(4, 6.2), ylim=c(0,2), lwd=3) lines(density(mu2,n=1500),lty=2,lwd=3) text(4.2,1,"Low",lwd=2,cex=1.2) text(6,1,"Normal",lwd=2,cex=1.2)
lowf <- rnorm(50000, mu1, 1/sqrt(tau1)) normalf <- rnorm(50000, mu2, 1/sqrt(tau2)) plot(density(lowf,n=1500), type='l', xlab="", main="", ylab="", xlim=c(2,8), ylim=c(0,0.5), lwd=3) lines(density(normalf,n=1500),lty=2,lwd=3) mtext("log (Diasorin Score)",line=3,side=1,cex=1.5) mtext("Predictive Density",line=2.5,side=2,cex=1.5) text(3.2,0.3,"Low",lwd=2,cex=1.2) text(7,.3,"Normal",lwd=2,cex=1.2)
mean(as.numeric(mu2>mu1))
## [1] 0.9914
mean(as.numeric(normalf>lowf))
## [1] 0.72214
mean(as.numeric(normalf>5.1))
## [1] 0.66176
mean(as.numeric(lowf<5.1))
## [1] 0.66744
low.act <- exp(lowf) normal.act <- exp(normalf) plot(density(low.act,n=2500,from=0), type='l', xlab="", main="", ylab="", xlim=c(0,1000), ylim=c(0,0.006), lwd=3) lines(density(normal.act,n=2500,from=0),lty=2,lwd=3) lines(c(0,0),c(0,0.000593),lwd=3,lty=1) mtext("Diasorin Score",line=3,side=1,cex=1.5) mtext("Predictive Density",line=2.5,side=2,cex=1.5) text(150,0.0055,"Low",lwd=2,cex=1.2) text(380,0.0018,"Normal",lwd=2,cex=1.2)
rbind(quantile(exp(mu1), probs=c(0.05, .5, .95)), quantile(exp(mu2), probs=c(0.05, .5, .95)))
## 5% 50% 95% ## [1,] 78.32559 110.5186 155.4947 ## [2,] 162.87723 242.0655 359.1196
EXERCISE 5.38. Suppose we have completely independent count data \[
y_{11}, \dots, y_{1 n_1} | \theta_1 \stackrel{iid}{\sim} \text{Poisson}(\theta_1)
\] and \[
y_{21}, \dots, y_{2 n_1} | \theta_2 \stackrel{iid}{\sim} \text{Poisson}(\theta_2).
\] Assume priors \[
\theta_1 \sim \text{Gamma} (a_1, b_1) \perp \theta_2 \sim \text{Gamma} (a_2, b_2).
\] Derive the analytical form of the posterior distribution of \((\theta_1, \theta_2)\) and characterize it as a well-known distribution. Also identify the joint posterior when independent Jeffreys' priors are used for \(\theta_1\) and \(\theta_2\). Finally, write R
or WinBUGS
code to handle the model, including making inferences about the risk difference and risk ratio.