---
title: "Appendix A: The CAR Hierarchical Models"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Appendix A: The CAR Hierarchical Models}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

## Overview

In this vignette, we outline the hierarchical models used in the RSTr package, along with the full-conditional distributions used for each update.

## The CAR Hierarchical Model

The CAR model used by RSTr is based on the model developed by [Besag, York, and Mollié (1991)](https://doi.org/10.1007/BF00116466) with modifications using inverse transform sampling for restricted informativeness based on [Quick, et al. (2021)](https://doi.org/10.1016/j.sste.2021.100420):

For models using `method = "binomial"`,

$$
\begin{split}
Y_{i} &\sim \text{Binomial}(n_{i}, \lambda_{i}) \\
\theta_{i} &= \text{Logit}(\lambda_{i}) \\
\end{split}
$$

For models using `method = "poisson"`, $$  
\begin{split}
Y_{i} &\sim \text{Poisson}(n_{i} \lambda_{i}) \\
\theta_{i} &= \text{Log}(\lambda_{i}) \\
\end{split}
$$

For both models,

$$
\begin{split}
\theta_{i} &\sim \text{Normal}(\beta_{j} + Z_{i}, \tau^2), \\ 
i &=\{1,...,N_{s}\},\ j =\{1,...,N_{is}\} \\
p(\beta_{j}) &\propto 1 \\
Z &\sim \text{CAR}(\sigma^2) \\
\sigma^2 &\sim \text{InvGamma}(a_\sigma,b_\sigma) \\
\tau^2 &\sim \text{InvGamma}(a_\tau,b_\tau)
\end{split}
$$

## The MCAR Hierarchical Model

The MCAR model used by RSTr is based on the model developed by [Gelfand and Vounatsou (2003)](https://doi.org/10.1093/biostatistics/4.1.11):

For models using `method = "binomial"`,

$$
\begin{split}
Y_{ik} &\sim \text{Binomial}(n_{ik}, \lambda_{ik}) \\
\theta_{ik} &= \text{Logit}(\lambda_{ik}) \\
\end{split}
$$

For models using `method = "poisson"`,

$$
\begin{split}
Y_{ik} &\sim \text{Poisson}(n_{ik}, \lambda_{ik}) \\
\theta_{ik} &= \text{Log}(\lambda_{ik}) \\
\end{split}
$$

For both models,

$$
\begin{split}
\theta_{ik} &\sim \text{Normal}(\beta_{jk} + Z_{ik}, \tau_k^2), \\ 
i &=\{1,...,N_s\}, k =\{1,...,N_{g}\}, j=\{1,...,N_{is}\} \\
p(\beta_{jk}) &\propto 1 \\
Z &\sim \text{CAR}(G) \\
G &\sim \text{InvWishart}(\nu,G_0) \\
\tau^2 &\sim \text{InvGamma}(a_\tau,b_\tau)
\end{split}
$$

## The MSTCAR Hierarchical Model

The MSTCAR model used by RSTr is based on the model developed by [Quick, et al. (2017)](https://doi.org/10.1214/17-AOAS1068):

For models using `method = "binomial"`,

$$
\begin{split}
Y_{ikt} &\sim \text{Binomial}(n_{ikt}, \lambda_{ikt}) \\
\theta_{ikt} &= \text{Logit}(\lambda_{ikt}) \\
\end{split}
$$

For models using `method = "poisson"`,

$$
\begin{split}
Y_{ikt} &\sim \text{Poisson}(n_{ikt} \lambda_{ikt}) \\
\theta_{ikt} &= \text{Log}(\lambda_{ikt}) \\
\end{split}
$$

For both models,

$$
\begin{split}
\theta_{ikt} &\sim \text{Normal}(\beta_{jkt} + Z_{ikt}, \tau_k^2), \\ 
i &=\{1,...,N_s\},\ k =\{1,...,N_g\},\ t=\{1,...,N_t\},\ j=\{1,...,N_{is}\} \\
p(\beta_{j}) &\propto 1 \\
Z &\sim \text{MSTCAR}(\mathcal{G}, \mathcal{R}), \ \mathcal{G}=\{G_1,...,G_{N_t}\}, \ \mathcal{R}=\{R_1,...,R_{N_g}\} \\
G_t &\sim \text{InvWishart}(A_G, \nu) \\
A_G &\sim \text{Wishart}(A_{G_0}, \nu_0) \\
R_k &= \text{AR}(1,\rho_k) \\
\rho_k &\sim \text{Beta}(a_{\rho}, b_{\rho}) \\
\tau_k^2 &\sim \text{InvGamma}(a_\tau,b_\tau)
\end{split}
$$
