---
title: "Hierarchical Bayesian Model for Binary Responses"
author: "Kristian Brock"
date: "`r Sys.Date()`"
output: 
  rmarkdown::html_vignette:
    fig_caption: yes
bibliography: library.bib
vignette: >
  %\VignetteIndexEntry{Hierarchical Bayesian Model for Binary Responses}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

# Hierarchical Bayesian model for binary responses
@Thall2003 described a method for analysing treatment effects of a common intervention in several sub-types of a single disease.
The treatment effects are assumed to be different but exchangeable and correlated.
Observing good efficacy in one cohort, for example, increases one's expectations of efficacy in other cohorts.

They demonstrate the hierarchical model in a trial with binary response outcomes and in another with time-to-event outcomes.
This vignette describes the sarcoma example with binary response outcomes.
The authors provide WinBUGS code in the appendix of their paper [@Thall2003].
We port their model to Stan and illustrate usage with the example given in their paper.


# Implementation in `trialr`
Statistically, the authors assume that in a trial of $k$ disease subtypes, the treatment effects are drawn from the same common normal distribution

$\rho_1, ..., \rho_k \sim N(\mu, \sigma^2)$

As is the convention in BUGS, the authors define normal distributions by a precision parameter $\tau$ as opposed to the standard deviation parameter $\sigma$ used here.
We have re-specified the model to comply with the Stan convention of using standard deviation.
The authors use a normal hyperprior on $\mu$, and a gamma hyperprior on $\tau$, equivalent to an inverse gamma hyperprior on $\sigma^2$.

We observe $x_i$ responses in $n_i$ patients in disease sub-type $i$.
The rate of response in subtype $i$ is modelled as $p_i = \text{logit}^{-1}(\rho_i)$.
Each $x_i$ is assumed to be binomially distributed with success parameter $p_i$.
In Stan, that relationship is described as
`x ~ binomial_logit(n, rho);`

The treatment is judged to be worthy of further investigation in cohort $i$ if

$\text{Pr}\left\{ p_i > \theta | \mathcal{D} \right\} > q$

where $\theta$ is the minimum required response rate, and $q$ is the required certainty to approve.
In their provided example, @Thall2003 use $\theta = 0.3$.

# Example
```{r message=FALSE}
library(trialr)
```

We reproduce Thall _etl al._'s example.
We have outcomes in 10 disease subgroups.
The number of responses is stored in `group_responses` and the number of patients in `group_sizes`.
There have been 3 / 7 responses in subgroup 4, for example, but 0 / 2 responses in subgroup 2, and zero patients treated at all in subgroups 1, 6 and 10.

```{r, results = "hide"}
fit <- stan_hierarchical_response_thall(
  group_responses = c(0, 0, 1, 3, 5, 0, 1, 2, 0, 0), 
  group_sizes = c(0, 2 ,1, 7, 5, 0, 2, 3, 1, 0), 
  mu_mean = -1.3863,
  mu_sd = sqrt(1 / 0.1),
  tau_alpha = 2,
  tau_beta = 20)
```

```{r}
fit
```

The probability of response in each subgroup is calculated under `prob_response`.
We can use the generic summary function from rstan to get a nice summary:
```{r}
knitr::kable(rstan::summary(fit, par = 'prob_response')$summary, digits = 3)
```

Let us say that we are willing to approve the treatment for further study in a subgroup if we have at least $q = 70$% certainty that the probability of efficacy exceeds the target response rate of 30%.
```{r}
colMeans(as.data.frame(fit, pars = 'prob_response') > 0.3)
```

On that basis, at this interim stage, we would be eager to approve the treatment in subgroups 3, 5 and 8.
Subgroups 4 and 7 are close to the boundary.

Some distribution plots of the probabilities of efficacy in some subgroups may be informative.

```{r, message = FALSE, fig.width = 7, fig.height = 7, fig.cap = "Prob(Response | D) in subgroup 3"}
library(ggplot2)
library(rstan)
library(dplyr)

plot(fit, pars = 'prob_response') + 
  geom_vline(xintercept = 0.3, col = 'orange', linetype = 'dashed') +
  labs(title = 'Partially-pooled analysis of response rate in 10 sarcoma subtypes')
```

We see that the inferred efficacy in subgroup 5 is very high.
In contrast, efficacy is subject to more uncertainty in subgroup 4, but the majority of the mass clearly lies to the right of 30%.

# trialr
`trialr` is available at https://github.com/brockk/trialr and https://CRAN.R-project.org/package=trialr 



# References
