0

I'm currently trying to develop a model in JAGS, but I unfortunately keep getting the following error:

Error in jags.model("ref_model.txt", data = ref.data.jags, inits = inits3, : RUNTIME ERROR: Compilation error on line 26. Unknown variable mu.fine Either supply values for this variable with the data or define it on the left hand side of a relation.

This happens when I run the following code:

# Function that generates the initial values for MCMC:
inits <- function()
{
 list(beta0=rnorm(1),
      beta1=rnorm(1),
      beta2=rnorm(1),
      beta3=rnorm(1),
      beta4=rnorm(1),
      beta5=rnorm(1),
      beta6=rnorm(1))
}
inits3 <- list(inits(), inits(), inits())

# Parameters that will be monitored:
params <- c("beta0", # intercept
          "beta1", "beta2", "beta3", # slopes
          "beta4", "beta5", "beta6",
          "pred.fine") # fine-grain predictions

# Model compilation:
jm <- jags.model("ref_model.txt",
               data = ref.data.jags,
               inits = inits3,
               n.chains = 3,
               n.adapt = 1000)

The following is what is in the file titled "ref_model.txt", I run this in R using Rjags in R

# Model definition for JAGS:
cat("
model
{
  # priors
  beta0 ~ dnorm(0, 0.01)
  beta1 ~ dnorm(0, 0.1)
  beta2 ~ dnorm(0, 0.1)
  beta3 ~ dnorm(0, 0.1)
  beta4 ~ dnorm(0, 0.1)
  beta5 ~ dnorm(0, 0.1)
  beta6 ~ dnorm(0, 0.1)

  # fitting the model to the fine-grain reference dataset
  # (600 well surveyed cells)
    for (j in 1:N.ref)
    {
      # Eq. 1 (see Methods):
      log(lambda.fine[j]) <- beta0 +
                             beta1 * NPP.ref[j] +
                             beta2 * LC.ref[j] +
                             beta3 * PW.ref[j] +
                             beta4 * HFP.ref[j] +
                             beta5 * PS.ref[j] +
                             beta6 * T.ref[j]
      # Eq. 2 (see Methods):
      S.ref[j] ~ dpois(mu.fine[j])
    }

  # predicting in the complete fine-grain dataset
  # (all 6238 fine-grain grid cells)
    for (i in 1:N.fine)
    {
      log(pred.fine[i]) <- beta0 +
                           beta1 * NPP[i] +
                           beta2 * LC[i] +
                           beta3 * PW[i] +
                           beta4 * HFP[i] +
                           beta5 * PS[i] +
                           beta6 * T[i]
    }
}
 ", file="ref_model.txt")

I'm a little confused as to why the error is occurring, If anyone can advise how I can address this problem, I would greatly appreciate it.

Camilo
  • 1

1 Answers1

2

This error is occurring because mu.fine only occurs on the right hand of an equation and not the left. In another way, it looks like S.ref depends on mu.fine, but you have not told JAGS what mu.fine is (there are no values for it). Assuming that lambda.fine is the linear predictor and S.ref is your dependent variable you could change

S.ref[j] ~ dpois(mu.fine[j])

to

S.ref[j] ~ dpois(lambda.fine[j])

and then this error would not occur.

mfidino
  • 3,030
  • 1
  • 9
  • 13