Failure running textbook example in JAGS

  • Jeroen Minderman


    I am new to Bayesian modelling and to JAGS. I’ve been working through the
    examples in McCarthy’s ‘Bayesian Methods for Ecology’ which has so far been
    excellent but uses WinBUGS. As an exercise, I’m attempting to replicate the
    examples from this book in JAGS.

    I realise there are some syntax differences between JAGS and WinBUGS but I
    think my understanding of either is too limited to figure out what I’ve got
    wrong here… I’m stuck on the example in Box 5.9, ‘Accounting for imperfect
    detection’. This is binomial regression predicting the occurrence of a tree
    frog in survey sites from two explanatory variables (size of streams, ‘LnCV’
    and presence of palms, ‘palms’). The observed number of frogs using two survey
    methods (Y1 and Y2 (given the number of surveys V1 and V2) are modelled as the
    product of the predicted probability of the presence of the frog, and the
    detection probability using each survey method.

    The model runs fine using WinBUGS. However, when I attempt to replicate it in
    JAGS (exact same model, inits, priors and data), calling JAGS from R using
    R2jags, the following error message is returned:

    “Error in jags.model(model.file, data = data, inits = inits, n.chains =
    n.chains, :

    Error in node Y1

    Observed node inconsistent with unobserved parents at initialization”

    I thought the problem might be the inits, but changing them to default (=NULL)
    yields the same problem.

    I suspect something might be going wrong with the Y1/Y2 distributions, but I’m
    not sure I understand this enough to solve it…!

    Here is the R code I use, which first sinks the model to a file, then reads
    data, then attempts to run the model:

        # uninformative priors for the variables
      a ~ dnorm(0, 1.0E-6)
      b[1] ~ dnorm(0, 1.0E-6)
      b[2] ~ dnorm(0, 1.0E-6)
      b[3] ~ dnorm(0, 1.0E-6)
        # detection probabilities when the species is present:
      d[1] ~ dunif(0, 1)
      d[2] ~ dunif(0, 1)
        # average catchment volume:
      mLnCV <- mean(LnCV[])
      for (i in 1:64)  # for each of the 64 sites
          # probability of presence:
        logit(p[i]) <- a + b[1]*(LnCV[i] - mLnCV) + b[2]*palms[i] + b[3]*(LnCV[i] - mLnCV)*palms[i]
          # actual presence:
        Lp[i] ~ dbern(p[i])
          # detectability of nocturnal searches:
        dd1[i] <- d[1]*Lp[i]
          # detectability of automatic tape recorders:
        dd2[i] <- d[2]*Lp[i]
          # number of detections with searches:
        Y1[i] ~ dbin(dd1[i], V1[i])
          # number of detections with tapes:
        Y2[i] ~ dbin(dd2[i], V2[i])
    ", fill=T)
    # Data:
    dat <- list(
    LnCV = c(3.871, 3.714, 3.113, 3.117, 3.434,2.932,4.241,3.382, 4.197, 2.872, 3.583, 2.986, 2.131, 4.794, 3.749, 3.123, 3.220, 3.836, 4.542, 3.382, 3.513, 3.193, 4.336, 4.221, 2.541, 4.261, 3.764, 5.019, 2.517, 3.621, 3.178, 3.211, 3.028, 3.204, 2.065, 4.114, 4.662, 2.593, 2.170, 3.557, 3.162, 3.596, 4.229, 3.809, 3.641, 3.042, 3.122, 3.465, 3.376, 3.949, 2.591, 3.175, 2.633, 2.955, 3.172, 3.078, 3.842, 2.770, 3.454, 2.459, 2.941, 3.120, 3.679, 4.202),
    # Run the model:
    inits <- list('a'=0,'b'=c(0,0,0),'d'=c(0.5,0.5))
    jagsfit <- jags('example5-9.jag', data=dat, inits=inits,

    Returns error message as above.

    Any suggestions?

    Like I say, I am new to JAGS entirely, there may be something I’ve
    missed/misunderstood about the model specification being very different.

    I apologise if I’ve done something silly; I am only trying to learn JAGS (and
    Bayesian modelling!) from scratch based on familiar WinBUGS examples…!

    R session info/version below.

    Many thanks in advance and best wishes,

    Jeroen Minderman


    R version 2.12.0 (2010-10-15)

    Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)



    attached base packages:

    tools stats graphics grDevices utils datasets methods base

    other attached packages:

    R2jags_0.02-15 rjags_2.2.0-4 R2WinBUGS_2.1-16 coda_0.13-5

    lattice_0.19-13 SciViews_0.9-2 ellipse_0.3-5 MASS_7.3-8

    svGUI_0.9-50 svMisc_0.9-60

    loaded via a namespace (and not attached):


    • Max Joseph

      Max Joseph - 2013-01-18

      I encountered the same error with an occupancy model in JAGS that ran in OpenBUGS and WinBUGS. Tim was right - the error message results from inconsistency between initial values and observed presences (in this case, Y1 = 1). One solution is to supply initial values for the latent occupancy state (Lp in this example), ensuring that the initial values are consistent with observed presences so that if Y1[4] = 1, Lp[4] = 1.

      Last edit: Max Joseph 2013-01-19
  • Tim Handley

    Tim Handley - 2011-04-18

    This error means there is a mismatch between your inits and your model/data.
    It means that one or more of the initial values are so far away from the
    'best' value that the MCMC just can't proceed. While the problem could be in
    either the inits or the model/data, when I get this error, it has always been
    a result of poor initial values.

    I usually write a small function to choose initial values randomly from a
    reasonable range. So when I get this error, I just rerun the function to
    select a new set of initial values, and try running JAGS again. In rare cases,
    where my guess about a 'reasonable range' has been poor, it has taken up to 10
    tries to find a set of initial values which are acceptable.


Log in to post a comment.