BUGS Created 2026-10-06 Updated 2026-10-14
BUGS specifies a probabilistic model through stochastic nodes, deterministic nodes and observed data, then uses Markov chain Monte Carlo to simulate its Bayesian posterior. Its normal sampling notation uses a precision parameter rather than a variance, and its gamma notation uses shape and rate.
Model 2 is an errors-in-variables model: the follow-up regression depends on a latent true baseline rather than substituting its noisy observation. A common normal distribution for the true baselines provides partial pooling and allows uncertainty about each baseline to propagate to the regression coefficients and predictions. Conditional on the shared parameters, the child-specific factorization is
The Directed acyclic graph below includes the shared parameters, the two local latent variables, both observed measurements, and the deterministic regression mean. The outer plate repeats over children and the inner plate over follow-up visits. Filled nodes are observed; rectangular mean nodes are deterministic.
Figure 1.
Directed graph of the latent-baseline random-intercept model, including shared priors, baseline measurement error and repeated follow-up visits
. Details.
One complete adapted BUGS specification uses the non-centered form of the random intercept, retaining the original priors and adding proper broad priors for and :
for (i in 1:N) {
  z[i] ~ dnorm(0, 1)
  alpha[i] <- delta + tau*z[i]
  truebase[i] ~ dnorm(theta, baseprec)
  baseline[i] ~ dnorm(truebase[i], noiseprec)
  for (j in 1:3) {
    followmean[i,j] <- alpha[i] + beta*log(time[i,j]) + gamma*truebase[i]
    followup[i,j] ~ dnorm(followmean[i,j], noiseprec)
  }
}
noiseprec ~ dgamma(0.001, 0.001)
beta ~ dunif(-100, 100)
gamma ~ dunif(-100, 100)
delta ~ dunif(-100, 100)
tau ~ dunif(0, 100)
theta ~ dunif(-100, 100)
psi ~ dunif(0, 100)
baseprec <- 1/(psi*psi)
Here is the number of children, baseline and followup contain the observed data, and dnorm uses precision rather than variance. The priors on the newly introduced population baseline mean and standard deviation are explicit choices, not consequences of the likelihood; sensible alternatives can be checked for sensitivity. The code uses the noisy baseline only in its own measurement likelihood, and uses truebase in every follow-up mean.
There is a notation problem here. The posterior predictive probability calculated above is already a fixed number conditional on . Literally,
No simulation is needed for that interpretation. The known exposure is also necessary, despite its omission from this part's list of inputs.
The natural uncertain quantity is instead . For its posterior probability of being below one half, draw the rate from its gamma distribution Bayesian posterior and average an indicator. Rough BUGS code is
model {
  lambda ~ dgamma(a+n, b+T)
  q <- exp(-lambda)
  belowHalf <- step(lambda-log(2))
}
The monitored average of belowHalf estimates , equivalently . The equality boundary has zero probability. This code samples the already updated Bayesian posterior; adding the count likelihood function again would count the data twice.
A uniform prior on makes the posterior density proportional to the Poisson change-point posterior likelihood above. The zeros trick introduces an observed zero with Poisson distribution mean , making its likelihood function equal to . Choose ; since , this mean is strictly positive. Rough BUGS code, with zero=0 supplied as data, is
model {
  theta ~ dunif(0,T)
  for (i in 1:n) {
    before[i] <- step(theta-time[i])
  }
  j <- sum(before[])
  logL <- theta-2*T+(n-j)*log(2)
  zero ~ dpois(K-logL)
}
For , omit the array and set j <- 0. Monitor the sampled theta to obtain its posterior mean, credible interval and interval probabilities.
There is also an exact sampling method. On the posterior density is proportional to , so choose the interval with weights
Then draw from a uniform distribution on and set . These weighted interval draws sample the posterior directly, without asking a local Markov chain Monte Carlo update to cross its discontinuities.
In Markov chain Monte Carlo, retain the derived quantity at every iteration. Rough BUGS code using the actual observations is
model {
  pM ~ dbeta(0.5,0.5)
  pT ~ dbeta(0.5,0.5)
  milkAnswers ~ dbin(pM,4)
  teaAnswers ~ dbin(pT,4)
  delta <- pM-pT
  positive <- step(delta)
}
Supply milkAnswers=3 and teaAnswers=1. Summarize delta by its posterior mean, empirical quantiles and credible interval; the average of positive estimates . Here . Check Markov chain Monte Carlo convergence diagnostics before interpreting the simulation. Since both Bayesian posteriors are independent known beta distributions, direct independent sampling is an equally valid, simpler way to obtain the same summaries.
Represent the independent locally flat intercept prior distributions by broad finite uniform priors, for example on ; this is proper and approximately constant over plausible mortality logits. With calibrated above, rough BUGS code is
model {
  mu ~ dnorm(0,0.25)
  tau ~ dunif(0,A)
  invtau2 <- pow(tau,-2)
  for (j in 1:J) {
    alpha[j] ~ dunif(-10,10)
    beta[j] ~ dnorm(mu,invtau2)
    logit(thetaC[j]) <- alpha[j]-beta[j]/2
    logit(thetaT[j]) <- alpha[j]+beta[j]/2
    rC[j] ~ dbin(thetaC[j],nC[j])
    rT[j] ~ dbin(thetaT[j],nT[j])
    oddsRatio[j] <- exp(beta[j])
  }
}
Use and supply treated death counts with totals , and control death counts with totals . In BUGS, the second dnorm argument is a precision parameter, so 0.25 corresponds to variance four. Initialize the positive scale away from zero. Monitor and study odds ratios, checking Markov chain Monte Carlo convergence diagnostics and sensitivity to the finite intercept bounds and scale prior distribution. The fitted hierarchy combines binomial sampling uncertainty with between-study heterogeneity.