The bang package simulates from the posterior distributions involved in certain Bayesian models. See the vignette Introducing bang: Bayesian Analysis, No Gibbs for an introduction. In this vignette we consider the Bayesian analysis of certain conjugate hierarchical models. We give only a brief outline of the structure of these models. For a full description see Chapter 5 of Gelman et al. (2014).
Suppose that, for j = 1, …, J, experiment j of J experiments yields a data vector Yj and associated parameter vector θj. Conditional on θj the data Yj are assumed to follow independent response distributions from the exponential family of probability distributions. A prior distribution π(θj ∣ ϕ) is placed on each of the population parameters θ1, …, θJ, where ϕ is a vector of hyperparameters. For mathematical convenience π(θj ∣ ϕ) is selected to be conditionally conjugate, that is, conditionally on ϕ the posterior distribution of θj of the same type as π(θj ∣ ϕ).
Use of a conditionally conjugate prior means that it is possible to
derive, and simulate from, the marginal posterior density π(ϕ ∣ y).
The hef
function does this using the generalized
ratio-of-uniforms method, implemented by the function ru
in
the rust package (Northrop
2017)). By default a model-specific transformation of the
parameter vector ϕ is used to
improve efficiency. See the documentation of hef
and the
two examples below for details. Simulation from the full posterior
density π(θ, ϕ ∣ y) = π(θ ∣ ϕ, y)π(ϕ ∣ y)
follows directly because conditional conjugacy means that it is simple
to simulate from π(θ ∣ ϕ, y),
given the values simulated from π(ϕ ∣ y).
The simulation is performed in the function hef
.
We consider the example presented in Section 5.3 of Gelman et al. (2014), in which the data (Tarone 1982) in the matrix rat
are
analysed. These data contain information about an experiment in which,
for each of 71 groups of rats, the total number of rats in the group and
the numbers of rats who develop a tumor is recorded, so that J = 71. Conditional on θ = (θ1, …, θJ) = (p1, …, pJ)
we assume independent binomial distributions for (Y1, …, YJ),
that is, $Y_j \sim {\rm binomial}(n_j,
p_j)$. We use the conditionally conjugate priors $p_j \sim {\rm Beta}(\alpha, \beta)$, so that
ϕ = (α, β).
The conditional conjagacy of the priors means that the marginal posterior of (α, β) given y = (y1, …, yJ) can be determined (equation (5.8) of Gelman et al. (2014)) as $$ \pi(\alpha, \beta \mid \boldsymbol{\mathbf{y}}) \propto \pi(\alpha, \beta) \prod_{j=1}^J \frac{B(\alpha + y_j, \beta + n_j - y_j)}{B(\alpha, \beta)}, $$ where π(α, β) is the hyperprior density for (α, β). By default ϕ = (α, β) is transformed prior to sampling using (ρ1, ρ2) = (log (α/β), log (α + β)). The aim of this is to improve efficiency by rotating and scaling the (mode-relocated) conditional posterior density in an attempt to produce near circularity of this density’s contours.
To simulate from the full posterior density π(θ, α, β ∣ y) = π(θ ∣ α, β, y)π(α, β ∣ y) we first sample from π(α, β ∣ y). Simulation from the conditional posterior distribution of θ given (α, β, y) is then straightforward on noting that $$ \theta_j \mid \alpha, \beta, y_j \sim {\rm beta}(\alpha + y_j, \beta + n_j - y_j) $$ and that θj, j = 1, …, J are conditionally independent.
The hyperprior for (α, β) used by default in
hef
is π(α, β) ∝ (α + β)−5/2, α > 0, β > 0,
following Section 5.3 of Gelman et al.
(2014). A user-defined prior may be set using
set_user_prior
.
library(bang)
# Default prior, sampling on (rotated) (log(mean), log(alpha + beta)) scale
rat_res <- hef(model = "beta_binom", data = rat, n = 10000)
plot(rat_res)
plot(rat_res, ru_scale = TRUE)
The plot on the left shows the values sampled from the posterior distribution of (α, β) with superimposed density contours. On the right is a similar plot displayed on the scale used for sampling, that is, (ρ1, ρ2).
The following summary is of properties of the generalized ratio-of uniforms algorithm, in particular the probability of acceptance, and summary statistics of the posterior sample of (α, β).
summary(rat_res)
#> ru bounding box:
#> box vals1 vals2 conv
#> a 1.0000000 0.00000000 0.00000000 0
#> b1minus -0.2382163 -0.40313465 -0.03906169 0
#> b2minus -0.2174510 0.05447431 -0.35297539 0
#> b1plus 0.2231876 0.36718396 -0.06551365 0
#> b2plus 0.2512577 0.05665707 0.44459818 0
#>
#> estimated probability of acceptance:
#> [1] 0.5258729
#>
#> sample summary
#> alpha beta
#> Min. : 0.6152 Min. : 3.883
#> 1st Qu.: 1.7895 1st Qu.:10.675
#> Median : 2.2198 Median :13.329
#> Mean : 2.4051 Mean :14.341
#> 3rd Qu.: 2.8034 3rd Qu.:16.830
#> Max. :14.1474 Max. :80.895
We perform a fully Bayesian analysis of an empirical Bayesian example
presented in Section 4.2 of Gelfand and Smith
(1990), who fix the hyperparameter α described below at a point
estimate derived from the data. The pump
dataset (Gaver and O’Muircheartaigh 1987) is a matrix in
which each row gives information about one of 10 different pump systems.
The first column contains the number of pump failures. The second column
contains the length of operating time, in thousands of hours.
pump
#> failures time
#> [1,] 5 94.320
#> [2,] 1 15.720
#> [3,] 5 62.880
#> [4,] 14 125.760
#> [5,] 3 5.240
#> [6,] 19 31.440
#> [7,] 1 1.048
#> [8,] 1 1.048
#> [9,] 4 2.096
#> [10,] 22 10.480
The general setup is similar to the beta-binomial model described above but now the response distribution is taken to be Poisson, the prior distribution is gamma and J = 10. For j = 1, …, J let Yj denote the number of failures and ej the length of operating time for pump system j. Conditional on λ = (λ1, …, λJ) we assume independent Poisson distributions for (Y1, …, YJ) with means that are proportional to the exposure time ej, that is, $Y_j \sim {\rm Poisson}(e_j \lambda_j)$. We use the conditionally conjugate priors $\lambda_j \sim {\rm gamma}(\alpha, \beta)$, so that ϕ = (α, β). We use the parameterization where β is a rate parameter, so that ${\rm E}(\lambda_j) = \alpha/\beta$ a priori.
The marginal posterior of (α, β) given y = (y1, …, yJ) can be determined as $$ \pi(\alpha, \beta \mid \boldsymbol{\mathbf{y}}) \propto \pi(\alpha, \beta) \prod_{j=1}^J \frac{\beta^\alpha\Gamma(\alpha + y_j)}{(\beta+e_j)^{\alpha+y_j} \Gamma(\alpha)}, $$ where π(α, β) is the hyperprior density for (α, β). The scale used for sampling is (ρ1, ρ2) = (log (α/β), log β)).
To simulate from the full posterior π(λ, α, β ∣ y) = π(λ ∣ α, β, y)π(α, β ∣ y) we first sample from π(α, β ∣ y) and then note that $$ \lambda_j \mid \alpha, \beta, y_j \sim {\rm gamma}(\alpha + y_j, \beta + e_j) $$ and that λj, j = 1, …, J are conditionally independent.
By default hef
takes α and β to be independent gamma random
variables a priori. The parameters of these gamma distributions
can be set by the user, using the argument hpars
or a
different prior may be set using set_user_prior
.
We produce similar output to the beta-binomial example above.
pump_res <- hef(model = "gamma_pois", data = pump, hpars = c(1, 0.01, 1, 0.01))
plot(pump_res)
plot(pump_res, ru_scale = TRUE)
summary(pump_res)
#> ru bounding box:
#> box vals1 vals2 conv
#> a 1.0000000 0.00000000 0.00000000 0
#> b1minus -0.5174980 -0.91869101 -0.06060116 0
#> b2minus -0.5150835 0.15757254 -0.92429417 0
#> b1plus 0.4124640 0.65433383 -0.11046433 0
#> b2plus 0.4224941 0.08788857 0.67847965 0
#>
#> estimated probability of acceptance:
#> [1] 0.5094244
#>
#> sample summary
#> alpha beta
#> Min. :0.2007 Min. :0.08544
#> 1st Qu.:0.8185 1st Qu.:1.29559
#> Median :1.0598 Median :1.94558
#> Mean :1.1496 Mean :2.16025
#> 3rd Qu.:1.3915 3rd Qu.:2.73560
#> Max. :3.6629 Max. :9.28544