# Bayesian Meta-Analysis with R and Stan

### Overview

Meta-analysis is frequently used to summarize results from multiple research studies. Since studies can be thought of as exchangeable, it is natural to analyze them using a hierarchical structure.

This page uses a Bayesian hierarchical model to conduct a meta-analysis of 9 randomized controlled trials (RCTs) of breast cancer screening. The analysis first replicates the frequentist results reported by Marmot et al. and then reexamines them in a Bayesian framework. The RCTs used in the meta-analysis are summarized in more detail by Gøtzsche et al..

R and Stan code for the analysis can be found here and here.

### Previous RCTs and Relative Risks

We begin by placing data from previous trials into a data frame using the summaries provided by Gøtzsche et al.. The treatment (group 1) is screening with mammography and the control (group 0) is no screening. The outcome in the treatment and control groups for study , and respectively, is the number of breast cancer deaths during 13 years of follow up for women at least 50 years of age. There are and patients in the treatment and control groups respectively.

```
rct <- data.frame(study = c("New York", "Malamo I", "Kopparberg", "Ostergotland",
"Canada I", "Canada II", "Stockholm", "Goteborg", "UK age trial"))
rct$year <- c(1963, 1976, 1977, 1978, 1980, 1980, 1981, 1982, 1991)
rct$d1 <- c(218, 87, 126, 135, 105, 107, 66, 88, 105)
rct$n1 <- c(31000, 20695, 38589, 38491, 25214, 19711, 40318, 21650, 53884)
rct$d0 <- c(262, 108, 104, 173, 108, 105, 45, 162, 251)
rct$n0 <- c(31000, 20783, 18582, 37403, 25216, 19694, 19943, 29961, 106956)
```

The relevant statistic for the meta-analysis is the relative risk ratio, or /, where and . We work with the log of the relative risk ratio, , because it is approximately normally distributed even in relatively small samples. We can calculate the variance of each term of by treating and as sample proportions and using the delta method, so that the variance of is,

We can then calculate a point estimates for the relative risk in each study as well as a 95 percent confidence interval.

```
rct$p1 <- rct$d1/rct$n1
rct$p0 <- rct$d0/rct$n0
rct$rr <- rct$p1/rct$p0
rct$lrr <- log(rct$rr)
rct$lse <- sqrt((1 - rct$p1)/(rct$p1 * rct$n1) + (1 - rct$p0)/(rct$p0 * rct$n0))
rct$lower <- exp(rct$lrr - qnorm(.975) * rct$lse)
rct$upper <- exp(rct$lrr + qnorm(.975) * rct$lse)
```

The results can be visualized nicely by creating a forest plot with the `metafor`

package.

```
library("metafor")
p <- forest(x = rct$rr, ci.lb = rct$lower, ci.ub = rct$upper,
slab = paste(rct$study, rct$year, sep = ", "), refline = 1)
text(min(p$xlim), .88 * max(p$ylim), "Study and Year", pos = 4, font = 2)
text(max(p$xlim), .88 * max(p$ylim), "Relative Risk [95% CI]", pos = 2, font = 2)
```

### Hiearchical Model

The results from the separate RCTs can be modeled using a hierarchical model. We use the fact that the log of the relative risk is approximately normally distributed and *assume* that the random effects follow a normal distribution,

where is assumed to be known with certainty (this assumption is not problematic because the binomial variances in each study are estimated precisely due to the large sample sizes). Meta-analyses are typically concerned with the overall mean, .

There are, in general, three ways to estimate the random effects, .

*No-pooling:*there is a separate model for each study and . This is a special case of the hierarchical model in which .*Complete-pooling:*patients in each study are random samples from a common distribution so . This is a special case of the hierarchical model in with .*Partial-pooling:*the hierarchical model is a compromise between the no-pooling and the complete-pooling estimates. In this case is unknown and is closer to when is small relative to , and closer to when the reverse is true.

### Estimation

#### No-pooling Estimates

A fixed-effect meta-analysis model completely pools the relative risk estimates across studies. The overall mean is commonly estimated by taking an inverse-variance weighted average of studies. This can be done in using the `rma`

function in the `metafor`

package.

```
me.fe <- rma(rct$lrr, rct$lse^2, method = "FE")
c(exp(me.fe$b), exp(me.fe$ci.lb), exp(me.fe$ci.ub))
```

```
## [1] 0.8041868 0.7406291 0.8731987
```

The relative risk and 95 percent confidence intervals are identical to the fixed-effect meta-analysis results reported in Gøtzsche et al.. We can check to see that the point estimate is identical to taking a weighted average of the relative risks in the RCTs.

```
exp(weighted.mean(rct$lrr, 1/(rct$lse^2)))
```

```
## [1] 0.8041868
```

#### Maximum Likelihood Estimation

A hierarchical model applied to meta-analysis is typically referred to as a random-effect meta-analysis model in the medical literature. The parameters of the hierarchical model can be estimated in either a frequentist or a Bayesian framework. In a frequentist setup, point estimates (rather than probability distributions) for the parameters are estimated. This can be done in a number of ways, but here we will estimate the parameters with restricted maximum likelihood (REML) using the `rma`

function.

```
me.re <- rma(rct$lrr, rct$lse^2)
c(exp(me.re$b), exp(me.re$ci.lb), exp(me.re$ci.ub))
```

```
## [1] 0.8029497 0.7259418 0.8881267
```

This analysis reproduces the results reported by Marmot et al..

#### Bayesian Estimation

One problem with the maximum likelihood approach is that it does not account for uncertainty in and produces confidence intervals for that are too narrow. A Bayesian model that produces complete probability distributions for each parameter can be estimated using the probabilistic programming language Stan. We begin by preparing the data.

```
library("rstan")
set.seed(101)
J <- nrow(rct)
stan.dat <- list(J = J, y = rct$lrr, sigma = rct$lse)
```

Next we specify the model. Note that we can rewrite the upper-level model as where , which speeds up the Stan code. Furthermore, and are given uniform priors.

We fit the model and extract samples from the joint posterior distribution.

```
fit <- stan(file = "_rmd-posts/bayesian_meta_analysis.stan",
data = stan.dat, iter = 2000, chains = 4)
post <- extract(fit, permuted = TRUE)
```

As expected, the 95 percent credible interval for the exponential of the overall mean is slightly wider that the 95 percent confidence interval produced using REML.

```
quantile(exp(post$mu), probs = c(.025, .5, .975))
```

```
## 2.5% 50% 97.5%
## 0.6889931 0.8011223 0.9110904
```

This addditional uncertainty comes from averaging over , which has a rather wide probability distribution.

```
quantile(post$tau, probs = c(.025, .5, .975))
```

```
## 2.5% 50% 97.5%
## 0.006847247 0.102239111 0.337733161
```

The effects, , are shrunk toward the overall mean, . The following plot examines the degree of shrinkage by comparing the effects from the Bayesian model to the relative risks when each study is analyzed separately.

```
library("ggplot2")
theme_set(theme_bw())
p.dat <- apply(exp(post$theta), 2, quantile, probs = c(.025, .5, .975))
p.dat <- data.frame(lower = p.dat[1, ], rr = p.dat[2, ], upper = p.dat[3, ])
p.dat <- rbind(p.dat, rct[, c("lower", "upper", "rr")])
p.dat$lab <- rep(c("Theta", "Y"), each = J)
p.dat$id <- rep(seq(9, 1), 2)
p.dat$idlab <- factor(p.dat$id, labels = rev(paste(rct$study, rct$year, sep = ", ")))
p.dat$yint <- mean(exp(post$mu))
ggplot(p.dat, aes(x = idlab, y = rr, ymin = lower, ymax = upper, col = lab)) +
geom_pointrange(aes(col = lab), position = position_dodge(width = 0.50)) +
coord_flip() + geom_hline(aes(yintercept = yint), lty = 2) + xlab("") +
ylab("") + theme(legend.position="bottom") +
scale_colour_discrete(name="",
labels = c("Theta" = bquote("Random effect:"~exp(theta[J])~" "),
"Y"= bquote("Relative risk:"~exp(Y[J]))))
```

There is considerable shrinkage and the degree of shrinkage is larger for studies where the relative risks are estimated less precisely. The 95 percent credible intervals using the Bayesian approach are also narrower than the 95 percent confidence intervals from the individual studies because the hierarchical model pools information across RCTs

Although most meta-anlayes focus on the overall mean, there are other quantities of interest that may be more meaningful. For example, it might me more useful to predict the effect of mammography screening in a new population by making a prediction about a new study effect, say , rather than from . Predictions in a new population can be made very easily in a Bayesian framework because the study effects are assumed to be exchangable; that is, we can simulate the posterior distribution of by drawing using the values of and drawn from the posterior simulation.

```
n.sims <- nrow(post$mu)
theta.new <- rep(NA, n.sims)
for (i in 1:n.sims){
theta.new[i] <- rnorm(1, post$mu[i], post$tau[i])
}
```

Although the posterior medians are similar, the 95 percent credible interval for is much wider than for .

```
quantile(exp(theta.new), probs = c(.025, .5, .975))
```

```
## 2.5% 50% 97.5%
## 0.5711096 0.8017504 1.1106414
```