Menu

N-mixture model error message

Help
Dan Gwinn
2014-08-08
2014-08-13
  • Dan Gwinn

    Dan Gwinn - 2014-08-08

    I am trying to run a spatially auto correlated N-mixture model based on code for occupancy models provided in Royle and Dorazio 2008. The model derives the autocorrelation variable (delta[i]) for each site (i) based on the number of nearest neighbors, their distance from the site, and their estimated abundances. This part appears to work fine, however, when I include the autocorrelation variable (delta) as a covariate in the abundance model I get the following error……

    Compilation error on line 10.
    Unable to resolve node N[26]
    This may be due to an undefined ancestor node or a directed cycle in the graph

    When delta is commented out (as it is in the provided code), the model runs. I’m stumped. Does anyone know why this is happening?

    The following code demonstrates the error. The code simulates data and analyzes it. It runs when delta is commented out and bombs when delta is included as a covariate of lam.

    #THIS PART SIMULATES DATA
    nsite = 500
    krep = 10
    bet1 = log(10)
    eta = 0
    p = exp(eta)/(1+exp(eta))
    lam = exp(bet1)
    N = rpois(nsite,lam)
    C = rbinom(krep,N[1],p)
      for(i in 2:nsite){
        C=rbind(C,rbinom(krep,N[i],p))
        }
    lat = runif(nsite,0,100)
    lon = runif(nsite,0,100)
    
    #THIS PART CALCULATES NEAREST NEIGHBORS AND DISTANCES
    library(spdep)
      coords <- as.matrix(cbind(lat,lon))
      n.dist <- 10
      nb.10km <- dnearneigh(coords,d1=0,d2=n.dist)
      numnn <- rep(0,nsite)
      for (j in 1:nsite){ numnn[j] <- length(nb.5km[[j]]) }
      nb.10km.d <- nbdists(nb.10km, coords)
      NN <- matrix(rep(0,nsite*max(numnn)),nrow=nsite)
      for (j in 1:nsite) {
      NN[j,] <- append(as.vector(nb.10km[[j]]),rep(NA,max(numnn)-numnn[j]))
      }
      D <- matrix(rep(0,nsite*max(numnn)),nrow=nsite)
      for (j in 1:nsite) {
      D[j,] <- append(as.vector(nb.10km.d[[j]]),rep(NA,max(numnn)-numnn[j]))
      }
    
    #JAGS MODEL
    modelFilename = "dep_mod.txt"
      cat("
    model {
    #Priors
        for(i in 1:2){
        bet[i] ~ dnorm(0,.01)
        }
        eta ~ dt(0,pow(1.566267,-2),7.63179)
    
    #Process Model Predictions
      #autocorrelation part
       for(i in 1:n){
          x[i,1] <- 0
            for(g in 1:numnn[i]){
              x[i,g+1] <- x[i,g]+ N[NN[i,g]]/D[i,g]
              }
          delta[i] <- (x[i,numnn[i]+1]/numnn[i])
      #Nmixture part
          N[i] ~ dpois(lam[i])
          lam[i] <- exp(log.lam[i])
          log.lam[i] <- bet[1] + bet[2]#*delta[i]
    
    #Observation Models
          logit.p[i] <- eta
          p[i] <- exp(logit.p[i])/(1+exp(logit.p[i]))
          for(j in 1:krep){
          C[i,j] ~ dbinom(p[i],N[i])
          }
            }
               }
    ", fill=TRUE, file=modelFilename)
    
    #JAGS INPUTS
    jags.data = list(n=nsite,C=as.matrix(C),numnn=as.vector(numnn),NN=NN,D=D,krep=krep)
    jags.parms = c('delta','eta','bet')
    jags.inits = function(){list(N = rep(50,nsite))}
    
    #DEFINE JAGS MODEL
    ptm = proc.time()
      jmod = jags.model(file=modelFilename, data=jags.data, n.chains=2, inits=jags.inits, n.adapt=1000)  # inits=jags.inits,
      update(jmod, n.iter=5000, by=5, progress.bar='text')
      post = coda.samples(jmod, jags.parms, n.iter=5000, thin=5)
    endtime = proc.time()-ptm
    endtime[3]/60
    plot(post)
    
     
    • Krzysztof Sakrejda

      On Fri, Aug 8, 2014 at 12:20 PM, Dan Gwinn dgwinn@users.sf.net wrote:

      I am trying to run a spatially auto correlated N-mixture model based on code
      for occupancy models provided in Royle and Dorazio 2008. The model derives
      the autocorrelation variable (delta[i]) for each site (i) based on the
      number of nearest neighbors, their distance from the site, and their
      estimated abundances. This part appears to work fine, however, when I
      include the autocorrelation variable (delta) as a covariate in the abundance
      model I get the following error……

      Compilation error on line 10.
      Unable to resolve node N[26]
      This may be due to an undefined ancestor node or a directed cycle in the
      graph

      When delta is commented out (as it is in the provided code), the model runs.
      I’m stumped. Does anyone know why this is happening?

      The following code demonstrates the error. The code simulates data and
      analyzes it. It runs when delta is commented out and bombs when delta is
      included as a covariate of lam.

      THIS PART SIMULATES DATA

      nsite = 500
      krep = 10
      bet1 = log(10)
      eta = 0
      p = exp(eta)/(1+exp(eta))
      lam = exp(bet1)
      N = rpois(nsite,lam)
      C = rbinom(krep,N[1],p)
      for(i in 2:nsite){
      C=rbind(C,rbinom(krep,N[i],p))
      }
      lat = runif(nsite,0,100)
      lon = runif(nsite,0,100)

      THIS PART CALCULATES NEAREST NEIGHBORS AND DISTANCES

      library(spdep)
      coords <- as.matrix(cbind(lat,lon))
      n.dist <- 10
      nb.10km <- dnearneigh(coords,d1=0,d2=n.dist)
      numnn <- rep(0,nsite)
      for (j in 1:nsite){ numnn[j] <- length(nb.5km[[j]]) }
      nb.10km.d <- nbdists(nb.10km, coords)
      NN <- matrix(rep(0,nsitemax(numnn)),nrow=nsite)
      for (j in 1:nsite) {
      NN[j,] <- append(as.vector(nb.10km[[j]]),rep(NA,max(numnn)-numnn[j]))
      }
      D <- matrix(rep(0,nsite
      max(numnn)),nrow=nsite)
      for (j in 1:nsite) {
      D[j,] <- append(as.vector(nb.10km.d[[j]]),rep(NA,max(numnn)-numnn[j]))
      }

      JAGS MODEL

      modelFilename = "dep_mod.txt"
      cat("
      model {

      Priors

      for(i in 1:2){
      bet[i] ~ dnorm(0,.01)
      }
      eta ~ dt(0,pow(1.566267,-2),7.63179)
      

      Process Model Predictions

      #autocorrelation part
      for(i in 1:n){
      x[i,1] <- 0
      for(g in 1:numnn[i]){
      x[i,g+1] <- x[i,g]+ N[NN[i,g]]/D[i,g]
      }
      delta[i] <- (x[i,numnn[i]+1]/numnn[i])

      Your directed cycle is: x -> delta -> log.lam -> lam -> N -> x ->
      delta -> log.lam ....and so on

      This specification does not produce a DAG, therefore the message about
      a directed cycle. You need to respecify the model or use something
      that doesn't require DAGs like Stan... or I think there's a WinBUGS
      extension that does these kinds of models (?) Somebody else will have
      to chime in on that.

      Krzysztof

      #Nmixture part
      N[i] ~ dpois(lam[i])
      lam[i] <- exp(log.lam[i])
      log.lam[i] <- bet[1] + bet[2]#*delta[i]

      Observation Models

        logit.p[i] <- eta
        p[i] <- exp(logit.p[i])/(1+exp(logit.p[i]))
        for(j in 1:krep){
        C[i,j] ~ dbinom(p[i],N[i])
        }
          }
             }
      

      ", fill=TRUE, file=modelFilename)

      JAGS INPUTS

      jags.data =
      list(n=nsite,C=as.matrix(C),numnn=as.vector(numnn),NN=NN,D=D,krep=krep)
      jags.parms = c('delta','eta','bet')
      jags.inits = function(){list(N = rep(50,nsite))}

      DEFINE JAGS MODEL

      ptm = proc.time()
      jmod = jags.model(file=modelFilename, data=jags.data, n.chains=2,
      inits=jags.inits, n.adapt=1000) # inits=jags.inits,
      update(jmod, n.iter=5000, by=5, progress.bar='text')
      post = coda.samples(jmod, jags.parms, n.iter=5000, thin=5)
      endtime = proc.time()-ptm
      endtime[3]/60
      plot(post)


      N-mixture model error message


      Sent from sourceforge.net because you indicated interest in
      https://sourceforge.net/p/mcmc-jags/discussion/610037/

      To unsubscribe from further messages, please visit
      https://sourceforge.net/auth/subscriptions/

      --

      Krzysztof Sakrejda

      Organismic and Evolutionary Biology
      University of Massachusetts, Amherst
      319 Morrill Science Center South
      611 N. Pleasant Street
      Amherst, MA 01003

      work #: 413-325-6555
      email: sakrejda@cns.umass.edu


       
  • Dan Gwinn

    Dan Gwinn - 2014-08-12

    Thanks for that. I'm not really up on the inner workings of different samplers. I'm giving it a shot in OpenBUGS now.

    d

     
  • Matt Denwood

    Matt Denwood - 2014-08-13

    Last time I had to use a spatially autocorrelated model, I used car.normal in GeoBUGS (http://mathstat.helsinki.fi/openbugs/Manuals/GeoBUGS/Manual.html)

    By the way, WinBUGS (and possibly OpenBUGS) does allow directed cycles in the code even though it's technically not a DAG, which I always thought was strange...

    Cheers,

    Matt

     

Log in to post a comment.