Search code examples
rjagsrjags

rjags model negative binomial likelihood and gamma prior


I read in my data. I make the model string. I hand it JAGS. I get "Error in node y[1] - Node inconsistent with parents".

Y=read.table("data.txt",header=T)
Y=Y$Y


model_string <- "model{

# Likelihood:
for( i in 1 : N ) {
y[i] ~ dnegbin( l , r )
}

# Prior:
r ~ dgamma(1,1)
l ~ dgamma(.1,.1)
}"

model <- jags.model(textConnection(model_string), 
                    data = list(y=Y,N=200))

First off all, I have no clue if my model is right. I cannot find even basic documentation for JAGS. I'm actually ashamed to admit it, because this should be as simple as an internet search, but I cannot find any document to tell me 1) how a JAGS model is set up or 2) what kinds of functions/distribution/parameters are available in JAGS. I only got this far because I found someone doing a similar model. If anyone knows of a JAGS wiki or documentation, that would be great.

Edit: If someone could even just tell me what the parameters for dnegbin are that would be a huge help. When I plug in random numbers for l and r in dnegbin(l,r) it 'works' as in it draws numbers for l and r, but I have no clue if it means anything.


Solution

  • You can find some info about dnegbin in the JAGS user manual.

    The first parameter of dnegbin must be between and 0 and 1. You can assign e.g. a uniform distribution:

    library(rjags)
    
    model_string <- "model{
    
    # Likelihood:
    for( i in 1 : N ) {
    y[i] ~ dnegbin( l , r )
    }
    
    # Prior:
    r ~ dgamma(1,1)
    l ~ dunif(0,1)
    }"
    
    y <- rpois(200, 10)
    model <- jags.model(textConnection(model_string), 
                        data = list(y=y, N=length(y)))
    

    You also have to be sure that the values of y are non-negative integers.