Jennifer Jones
Jennifer Jones

Reputation: 11

Writing a hierarchical JAGS model for binomial data

I have binomial data that is split into several groups and data that corresponds to each data point, formatted like so:

df <- data.frame(bin=c(1,1,1,1,1,0,0,0,0,0), group1 = c(1,1,0,0,0,0,0,0,0,0), group2 = c(0,0,1,1,1,0,0,0,0,0), fullgroup = c(1,1,2,2,2,0,0,0,0,0), precip = c(4,5,7,1,7,4,6,8,4,1), temp = c(1,5,8,2,4,7,3,9,2,4) )

I am trying to use JAGs to create a hierarchical model that returns parameter estimates for the overall group's relationship to the data as well as the individual groups relationship to the data. I'm using a linear regression:

logit(p) = b0 + b1 * precip + b2 * temp

And would like to know: b0.overall, b1.overall, b2.overall, b0.group1, b1.group1, etc.

The model I have currently is:

model {
  for(i in 1:length(y)) {
    psi[i] <- b0[group[i]] + b1[group[i]]*a[i] + b2[group[i]]*b[i] 
    occurrence[i] <- 1/(1+exp(-psi[i])) #logit transformation
  }
  
  for (j in 1:n.group) {
    b0[j] ~ dnorm(mu0.alpha, tau0.alpha)
    b1[j] ~ dnorm(mu1.alpha, tau1.alpha)
    b2[j] ~ dnorm(mu1.alpha, tau1.alpha)
  }
  
  #priors
  mu0.alpha ~ dnorm(0, 0.00001)
  tau0.alpha ~ dgamma(0.001, 0.001)
  mu1.alpha ~ dnorm(0, 0.00001)
  tau1.alpha ~ dgamma(0.001, 0.001)
  mu2.alpha ~ dnorm(0, 0.00001)
  tau2.alpha ~ dgamma(0.001, 0.001)

With initial values and data formatted like:

mod.inits<-function(){
  list(  mu0.alpha = rnorm(1,0,2),
         tau0.alpha = rgamma(0.001, 0.001),
         mu1.alpha = rnorm(1,0,2),
         tau1.alpha = rgamma(0.001, 0.001),
         mu2.alpha = rnorm(1,0,2),
         tau2.alpha = rgamma(0.001, 0.001)
  )
}

data = list(
  y = bin,
  a = precip,
  b = temp,
  group = fullgroup,
  n.group = 2 
)

However, when I run the model:

ws=jags.model(model, data=data, n.chains = 3, n.adapt=30000)

I get the error:

Compilation error on line 13.
Attempt to redefine node b1[1]

I'm concerned that the issue is that in the group column there are several points that are in group '0' - they are absence points for all groups. How would I go about fixing this, or is something else causing this issue?

Upvotes: 1

Views: 89

Answers (0)

Related Questions