data {
      for (i in 1:N){
           zero[i] <- 0
      }
}

model{
      C <- 10000
      preci[1:(2*K+1),1:(2*K+1)] ~ dwish (R[1:(2*K+1),1:(2*K+1)], df)
      mu[1] ~ dnorm(mu_eta, preci_eta) # eta=mu[1]
      prev <- phi(mu[1])
      for (j in 1:K){
          mu[2*j] ~ dnorm(mu_alpha, preci_alpha) #alpha[j] = mu[2*j]
          mu[2*j+1] ~ dnorm(mu_beta, preci_beta) #beta[j] = mu[2*j+1]
          post.Se[j] <- phi(mu[2*j])
          post.Sp[j] <- phi(mu[2*j+1])
          ppv[j] <- post.Se[j]*prev/(post.Se[j]*prev+(1-post.Sp[j])*(1-prev))
          npv[j] <- post.Sp[j]*(1-prev)/(post.Sp[j]*(1-prev)+(1-post.Se[j])*prev)
          LRpos[j] <- post.Se[j]/(1-post.Sp[j])
          LRneg[j] <- (1-post.Se[j])/post.Sp[j]

      }


      for (j in 1:(2*K+1)){
          mu_ran[j] <- 0
      }
      ### number of studies ###
      for (i in 1:nstu){
          ran[i,1:(2*K+1)] ~ dmnorm(mu_ran[1:(2*K+1)], preci[1:(2*K+1),1:(2*K+1)])
      }

    ### N: number of rows of data ###
      for (i in 1:N){
          ### K candidate tests ###
          for (j in 2:(K+1)){
               h1k[i,j-1] <- delta[i,j]* y[i,j]*log(Se.stud[sid[i],j-1]) + delta[i,j]*(1-y[i,j])*log(1-Se.stud[sid[i],j-1])
               h2k[i,j-1] <- delta[i,j]*(1-y[i,j])*log(Sp.stud[sid[i],j-1]) + delta[i,j]*y[i,j]*log(1-Sp.stud[sid[i], j-1])
          }
          logpih1[i] <- log(pi[sid[i]])+sum(h1k[i,])
          logpih2[i] <- log(1-pi[sid[i]])+sum(h2k[i,])

          ### delta[i,1]: indicator for T_0 (gold standard) ###
          logLC[i] <- C - (n[i]*(delta[i,1]*y[i,1]*logpih1[i]+delta[i,1]*(1-y[i,1])*logpih2[i] + (1-delta[i,1])*log(exp(logpih1[i])+exp(logpih2[i]))) )
          zero[i] ~ dpois(logLC[i])
     }


     for (i in 1:nstu){
          pi[i] <- phi(mu[1]+ran[i,1])
          for (k in 1:K){
              Se.stud[i,k] <- phi(mu[2*k]+ran[i,2*k])
              Sp.stud[i,k] <- phi(mu[2*k+1]+ran[i,2*k+1])
           }
     }

      Cov <- inverse(preci)

}



