Search code examples
rwinbugs

Code syntax in calculating posterior distribution in WinBUGS


Recently I read "The BUGS Book – A Practical Introduction to Bayesian Analysis" to learn WinBUGS. The way WinBUGS describes the derivation of posterior distribution makes me feel confused.

Let's take Example 4.1.1 in this book to illustrae:

Suppose we observe the number of deaths y in a given hospital for a high-risk operation. Let n denote the total number of such operations performed and suppose we wish to make inferences regarding the underlying true mortality rate, $\theta$.

The code of WinBUGS is:

y <- 10  # the number of deaths
n <- 100 # the total number of such operations
#########################
y ~ dbin(theta,n)             # likelihood, also a parametric sampling distribution
logit(theta) <- logit.theta   # normal prior for the logistic transform of theta
logit.theta ~ dnorm(0,0.368)  # precision = 1/2.71

The author said that:

The software knows how to derive the posterior distribution and subsequently sample from it.

My question is:

Which code reflects the logic structure to tell WinBUGS about "which parameter that I want to calculate its posterior distribution"?

This question seems silly, but if I do not read the background first, I truly cannot find directly in the code above about which parameter is focused on (e.g., theta, or y?).

Below are some of my thoughts (as a beginner of WinBUGS):

I think the following three attributions of the code style in WinBUGS makes me confused:

(1) the code does not follow "a specific sequence". For example, why is logit.theta ~ dnorm(0,0.368) not in front of logit(theta) <- logit.theta?

(2) repeated variable. Foe example, why did the last two lines not be reduced into one line: logit(theta) ~ dnorm(0,0.368)?

(3) variables are defined in more than one place. For example, y is defined two times: y <- 10 and y ~ dbin(theta, n). This one has been explained in Appendix A of the book (i.e., However, a check has been built in so that when finding a logical node that also features as a stochastic node, a stochastic node is created with the calculated values as fixed data), yet I still cannot catch its meaning.


Solution

    1. BUGS is a declarative language. For the most part, statements aren't executed in sequence, they define different parts of the model. BUGS works on models that can be represented by directed acyclic graphs, i.e. those where you put a prior on some components, then conditional distributions on other components given the earlier ones.

    2. It's a fairly simple language, so I think logit(theta) ~ dnorm(0, 0.368) is just too complicated for it.

    3. The language lets you define a complicated probability model, and declare observations of certain components in it. Once you declare an observation, the model that BUGS samples from is the the original full model conditioned on that observation. y <- 10 defines observed data. y ~ dbin(theta,n) is part of the model.
      The statement n <- 100 could be either: for fixed constants like n, it doesn't really matter which way you think of it. Either the model says that n is always 100, or n has an undeclared prior distribution not depending on any other parameter, and an observed value of 100. These two statements are equivalent.

    Finally, your big question: Nothing in the code above says which parameter you want to look at. BUGS will compute the joint posterior distribution of every parameter. n and y will take on their fixed values, theta and logit.theta will both be simulated from the posterior. In another part of your code (or by using the WinBUGS menus) you can decide which of those to look at.