Menu

an undefined ancestor node or a directed cycle

Jun Xu
2013-12-27
2013-12-29
  • Jun Xu

    Jun Xu - 2013-12-27

    Dear all,

    I am kind of new to JAGS, and I am trying to estimate a random intercept logit model. But somehow I am still getting this error message after debug:

    Error in jags.model("model.txt", data = dataList, n.chains = nChains, :
    RUNTIME ERROR:
    Compilation error on line 4.
    Unable to resolve node mu[1]
    This may be due to an undefined ancestor node or a directed cycle in the graph

    I believe I've defined all parameters....Here are r and rjags codes to estimate a simplified debuggable version of the model. Also the group variable countryR has been sorted and recoded into 1-33 in Stata before data being read into R.

    require(foreign)
    require(rjags)         
    
    modelstring = "
    model {
      for( i in 1 : nData ) {
        hlthprc2[i] ~ dbern(mu[i])
        logit(mu[i]) <- b0[countryR[i]] + b1*age[i]
      }
    
      b[1] ~ dnorm(0 , 1.0E-12 )
    
      for ( k in 1:nGroup) {
        b0[k] ~ dnorm(b0.mean, b0.tau)
      } 
    
      b0.mean ~ dnorm(0, 1.0E-4) 
      b0.tau ~ dgamma(1.0E-3, 1.0E-3)
    
    }
    " 
    writeLines(modelstring,con="model.txt")
    
    dataSource = read.dta("data/issp2007Bayes10pct.dta", convert.factor=FALSE)
    
    dataMat = as.matrix(dataSource)
    nData = NROW(dataMat)
    
    hlthprc2 = as.matrix(dataMat[,1])
    age = as.matrix(dataMat[,2])
    countryR = as.matrix(dataMat[,13])
    nGroup = length(unique(dataSource$countryR))
    
    dataList = list('hlthprc2' = as.vector( hlthprc2 ) , 
                    'age' = as.vector(age),         
                    'countryR' = as.vector(countryR),
                    'nGroup' = nGroup,
                    'nData' = nData
                    )
    
    parameters = c( "b0" , "b1")  
    adaptSteps = 1000              
    burnInSteps = 2000            
    nChains = 3                   
    numSavedSteps=50000           
    thinSteps=1                   
    nPerChain = ceiling( ( numSavedSteps * thinSteps ) / nChains ) # Steps per chain.
    # Create, initialize, and adapt the model:
    jagsModel = jags.model( "model.txt" , data=dataList , 
                            n.chains=nChains , n.adapt=adaptSteps )
    # Burn-in:
    cat( "Burning in the MCMC chain...\n" )
    update( jagsModel , n.iter=burnInSteps )
    # The saved MCMC chain:
    cat( "Sampling final MCMC chain...\n" )
    codaSamples = coda.samples( jagsModel , variable.names=parameters , 
                                n.iter=nPerChain , thin=thinSteps )
    mcmcChain = as.matrix( codaSamples )
    
     

    Last edit: Jun Xu 2013-12-28
    • Jun Xu

      Jun Xu - 2013-12-29

      so careless of me. Here b[1] should be b1.

       

Log in to post a comment.