1

I'm trying to make a bayesian mixture model using rjags. This is an attempt to map a dose-response relationship for experiments conducted in 19 labs. As such, the model that I produced has intercepts for all the labs. I want to cluster the lab effects using mixture modelling but my code does not work. Here is a copy of my model followed by the error :-

mod_string2 <- "
model{
# Likelihood
  for(i in 1:n){
    Y[i]   ~ dnorm(mu[i],inv.var)
    mu[i] <- a[lab[i]] + b[1]*ld1[i] + b[2]*ld2[i] + b[3]*sqld1[i] + b[4]*sqld2[i] + b[5]*lbody[i] + b[6]*B[i]*ld1[i] + b[7]*C[i]*ld1[i] + b[8]*D[i]*ld1[i] + b[9]*B[i]*ld2[i] + b[10]*C[i]*ld2[i] + b[11]*D[i]*ld2[i]
    a[lab[i]] ~ dnorm(muOfClust[clust[lab[i]]], tau)
    clust[i] ~ dcat( pClust[1:Nclust] )
  }

  # Prior for labs (intercepts)

  for (clustIdx in 1: Nclust) {
    muOfClust[clustIdx] ~ dnorm( 0 , 1/100000 )
  }

  pClust[1:Nclust] ~ ddirch(onesRepNclust) # so (pi1,pi2) follow Dir(1,1) which implies pi1 follows Beta(1,1)

  tau ~ dgamma(0.01 , 0.01)

  # Prior for beta
  for(j in 1:11){
    b[j] ~ dnorm(0,0.0001)
  }

  # Prior for the inverse variance
  inv.var ~ dgamma(0.01, 0.01)
  sigma <- 1/sqrt(inv.var)
}
"

My error is :-

Error in jags.model(textConnection(mod_string2), data = d2) : RUNTIME ERROR: Compilation error on line 7. Attempt to redefine node a[3]

What am I doing wrong?

Altamash Rafiq
  • 349
  • 1
  • 2
  • 10
  • You only have N = `Nclust` `a` parameters right? So you can't have a 1:n loop that defines the distribution of the `a` parameters, the same `a` element ends up being defined multiple times. It should be possible to define `a` in a `1:Nclust` loop, and then just look up the right `a` in the `1:n` loop. – Marius Dec 14 '17 at 01:14
  • No. The code as it is right now was intended to produce the 19 lab intercepts as well as cluster them. So there are 19 a parameters while Nclust = 2 because I believe they fall into 2 clusters. – Altamash Rafiq Dec 14 '17 at 01:27

0 Answers0