Hyolim Kang
Hyolim Kang

Reputation: 3

Rjags jags model compiling error when using for loop

I am using a Rjags package to run MCMC. I have binomial dataset and I tried to run a "for loop" function in order to generate parameters for multiple datasets from different authors in a combined data. I specified jags model and uninformative priors for each parameter that I want to get posteriors, but I kept getting an error message like this;


jcode <- "model{ 
    for (i in 1:3){
    n.pos[i] ~ dbinom(seropos_est[i],N[i]) #fit to binomial data
    seropos_est[i] = 1-exp(-lambdaS1*age[i]) #catalytic model
    }
 for (i in 4:7) {
    n.pos[i] ~ dbinom(seropos_est[i],N[i]) #fit to binomial data
    seropos_est[i] = 1-exp(-lambdaS2*age[i]) #catalytic model
    }
 for (i in 8:11) {
    n.pos[i] ~ dbinom(seropos_est[i],N[i]) #fit to binomial data
    seropos_est[i] = 1-exp(-lambdaS3*age[i]) #catalytic model
    }
  #priors 
    lambdaS1 ~ dnorm(0,1) #uninformative prior
    lambdaS2 ~ dnorm(0,1) #uninformative prior
    lambdaS3 ~ dnorm(0,1) #uninformative prior
 }"
 

Parameter vector paramVector <- c("lambdaS1", "lambdaS2", "lambdaS3")

`

mcmc.length=50000
jdat = list(n.pos= df_chik$N.pos,
            N=df_chik$N,
            age=df_chik$agemid)
jmod = jags.model(textConnection(jcode), data=jdat, n.chains=4, n.adapt=15000)
jpos = coda.samples(jmod, paramVector, n.iter=mcmc.length)

`Error message

Compiling model graph
   Resolving undeclared variables
   Allocating nodes
Graph information:
   Observed stochastic nodes: 11
   Unobserved stochastic nodes: 3
   Total graph size: 74

Initializing model
Deleting model

This is an error message that I am kept getting. I would appreciate if anyone can help me out with this!

Upvotes: 0

Views: 249

Answers (1)

Matt Denwood
Matt Denwood

Reputation: 2583

The text you show under “Error message” i.e. this text:

Compiling model graph
   Resolving undeclared variables
   Allocating nodes
Graph information:
   Observed stochastic nodes: 11
   Unobserved stochastic nodes: 3
   Total graph size: 74

Initializing model
Deleting model

... is not an error, but the expected output of rjags. But I suspect that you have not copied the real error message, which is probably something along the lines of "invalid parent values for node n.pos[1]". The reason for that is that for seropos_est[] you have relationships of the form:

seropos_est[i] = 1-exp(-lambdaS1*age[i])

Where lambdaS1 is an unconstrained variable. Therefore, the result of exp(-lambdaS1*age[i]) can be above 1, which means that seropos_est[i] can be negative, which is invalid for a probability parameter. In fact, given the normal prior for lambdaS1 the model will initialise that variable with a value of zero, meaning that seropos_est[i] is initialised to zero, which is also invalid if any of your n.pos are greater than zero. You therefore need to re-specify your model to constrain seropos_est to valid parameter space, possibly by changing the prior for lambdaS1 etc (presuming that age is positive).

Also, you have in your code:

lambdaS1 ~ dnorm(0,1) #uninformative prior

But this is certainly not uninformative. In any case, there is really no such thing as an 'uninformative prior' - all priors contain some information, by definition. The best you can do is a 'minimally informative prior' or 'non-informative prior', which is why this terminology is generally recommended rather than the misleading word 'uninformative'.

For future, it would help us to help you if your question contained a minimal reproducible example, so that we can run the model and see exactly what you see. In this case all that is really missing is access to the data.

Hope that helps,

Matt

Upvotes: 0

Related Questions