1

I come across two methods of specifying Bayesian hierarchical model in the book "Bayesian methods: a social and behavioural approach" (2015), third edition by Jeff Gill.

The three examples from the book (as below) are about Bayesian hierarchical models. Key differences in the models have been circled in red:

In Example 1, the nesting structure is expressed through the nested indexing alpha[state.id[i]]. The two for loops are specified separately. However, in Example 2 and 3, the hierarchical structures are specified through nested for loop rather than nested indexing. Besides, there are i and j appearing within the same square brackets (Q[i,j] in Example 2 and mu[i,j] in Example 3), as contrast to Example 1 where only one index (either i or j) appears in the square brackets.

MY QUESTION: What are the differences between nested indexing VS. nested for loop when specifying the Bayesian hierarchical model? Can Example 1 be modelled in a way similar to Example 2 and 3 or vice versa? How can I choose between the two methods?

I will provide you the full example if you feel more comfortable knowing details of the examples.

Thank you!

Example 1

Example 2

Example 3

1 Answer 1

0

Yes, example 1 can be written similar to example 2 and 3 and vice versa. This is simplest if each value of the first iterator has the same values of the second iterator. Otherwise you will end up with something like

for(i in 1:VALUE) {
  for (j in 1:TIME[i]){
    ...
  }
}

but then using a matrix (or more generally an array), e.g. Q in example 2 and mu in example 3, is not as natural.

I typically use the nested indexing approach since I typically store my data in an R data.frame which is easily amenable to the nested indexing approach. In the first example, state.id, contracting, gov.influence, etc. would all be columns of the data.frame. I also find the code easier to read in the nested indexing, but I could probably improve my code in the situations where I use nested for loops by creating informative iterators.

Since you can write either example in either fashion, which you choose ends up being a matter of preference.

Sign up to request clarification or add additional context in comments.

5 Comments

I tried to use the nested for loop structure for Example 1 as below: <!-- language: lang-js --> model { for (i in 1:SUBJECTS) { for (j in 1:STATES) { mu[i,j] <- alpha[j] + beta[1]*contracting[i,j] + beta[2]*gov.influence[i,j] group.inf[i,j] ~ dnorm(mu[i,j], tau) eta[j] <- gamma[1]*gov.ideology[j] alpha[j] <- dnorm(eta[j], tau.alpha) } } }
Sorry that I did not find how to organize the code into different rows as desired. My major confusion is that I do not know whether to use '[i]', '[j]' vs. '[i,j]' in the code. Would you please give it a check.
You will need to set up the data properly. The code you have here only works if you have the same number of subjects in each state. Then, you will need to set up matrices for contracting, gov.influence and pass these matrices to jags. Again, I find the nested indexing approach simpler.
I see your idea and agree with you that nested indexing is simpler. I tried to use the nested indexing method to write code for Example 3, however, I cannot figure out where to put the nested indexing term (like the alpha[state.id[i]) part in Example 1) into the code. Would you please kindly show me how to write the code for Example 3 using the nested indexing method? Thx
Example 3 shouldn't need nested index, i.e. there are no random effects. But you could still "vectorize" the code by using y[i] and mu[i] and therefore remove the nested for loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.