For loop sequence

Help
NJR
2014-01-06
2014-01-14
  • NJR
    NJR
    2014-01-06

    I'm interested in how the for loop sequence in "for (i in 1:n)" is parsed
    when n = 0. Does 1:n give c(1, 0), so the for loop run for the indices 1
    and 0, or is the for loop then skipped over?

    An example: Say I have observations from 10 groups, and I have default and
    special observations which I model with a normal and t-distribution
    respectively. Each group can have both types of observations, or only
    default, or only special observations. Is it possible to set up the model
    in this way:

    model {
    for (g in 1:ngroups) {
    for (i in 1:ndefault.g[g]) { # ndefault.g[g] may be 0 for certain groups
    y.gi[g, i] ~ dnorm(yhat.gi[g, i], tauy.gi[g, i])
    }
    for (i in 1:nspecial.g[g]) { # nspecial.g[g] may be 0 for certain groups
    y.gi[g, i] ~ dt(yhat.gi[g, i], tauy.gi[g, i], dft)
    }
    }

    ...
    }

    Thanks!

     
  • Martyn Plummer
    Martyn Plummer
    2014-01-14

    If you have a loop for(i in A:B) with B < A then the body of the loop is skipped. This is also how OpenBUGS works.