model {
  # Likelihood
  for(j in 1:J) {
    for(i in 1:N) {
      y[i,j] ~ dnorm(inprod(p[i,]*q[,j], s_mean[,j]+c_mean[,j]) / inprod(p[i,],q[,j]), 1/var_y[i,j])
      var_y[i,j] <- inprod(pow(p[i,]*q[,j],2),pow(s_sd[,j],2)+pow(c_sd[,j],2))/pow(inprod(p[i,],q[,j]),2) + pow(sigma[j],2)
    }

  }

  # Prior on sigma
  for(j in 1:J) { 
    sigma_raw[j] ~ dgamma(sigma_shape[j], sigma_rate[j])
    sigma[j] <- 0.001 + not_solo * sigma_raw[j] 
  }

  # Priors on c
  for(j in 1:J) {
    for (k in 1:K) {
      c_mean[k,j] <- c_mean_j[j]
      c_sd[k,j] <- c_sd_j[j]
    }
    c_mean_j[j] ~ dgamma(c_mean_est[j], 1)
    c_sd_j[j] ~ dgamma(c_sd_est[j], 1)
  }

}