# delta is the indicator for the test
# y is the test result
# N is the total number of studies
# K is the total number of tests
# logh1 is the loglikehood for the disease subject
# logh2 is the loglikehood for the non-disease subject
# From R: delta <- cbind(dat$delta0,dat$delta1,dat$delta2) y <- cbind(dat$T0,dat$T1,dat$T2) n <- dat$n


###### Bug model for data analysis ########### Author : Qinshu  Year:2015  #####

# Model 3 Full Model

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

model {
       C <- 10000
       df1 <- K+2
		   df2 <- K+1
       preci1[1:(K+1),1:(K+1)] ~ dwish(R1[1:(K+1),1:(K+1)], df1)
       preci2[1:K,1:K] ~ dwish(R2[1:K,1:K], df2)

       for (i in 1:K){
          for (j in (i+1):K){
              e[i,j] <- 0
              e[j,i] <- 0
          }
          mu2[i] ~ dnorm(0,0.1)
          logl[i] ~ dnorm(eta,xi_preci)
          e[i,i] <- exp(logl[i])
          mu20[i] <- mu2[i]/e[i,i]

       }

		   for (i in 1:(K+1)){
		      for (j in (i+1):(K+1)){
              c[i,j] <- 0
              c[j,i] <- 0
          }
		      mu1[i] ~ dnorm(0,0.1)
          logl2[i] ~ dnorm(eta,xi_preci)
          c[i,i] <- exp(logl2[i])
          mu10[i] <- mu1[i]/c[i,i]

       }

       post.pi <- phi(-mu1[1])

       for (k in 1:K){
            beta[k] ~ dunif(-5,5)
        }

       for( i in 1:K){
          post.se[i] <- phi(-(mu1[i+1]-mu2[i]/2)/exp(beta[i]/2))
          post.sp[i] <- phi((mu1[i+1]+mu2[i]/2)/exp(-beta[i]/2))
          ppv[i] <- post.se[i]*post.pi/(post.se[i]*post.pi+(1-post.sp[i])*(1-post.pi))
          npv[i] <- post.sp[i]*(1-post.pi)/(post.sp[i]*(1-post.pi)+(1-post.se[i])*post.pi)
          LRpos[i] <- post.se[i]/(1-post.sp[i])
          LRneg[i] <- (1-post.se[i])/post.sp[i]
        }

  for (i in 1:nstu){
            theta1[i,1:(K+1)]~ dmnorm(mu10[1:(K+1)], preci1[1:(K+1),1:(K+1)])
            alpha1[i,1:K]~ dmnorm(mu20[1:K], preci2[1:K,1:K])
            for (j in 1:K){
               alpha[i,j] <- alpha1[i,j]* e[j,j]
            }
            for (j in 1:(K+1)){
               theta[i,j] <- theta1[i,j]*c[j,j]
            }
        }

        gamma0 ~ dnorm(mu_gamma,preci_gamma)
        for (i in 1:nstu){
            pi[i]    <- phi(-theta[i,1])
            for (k in 2:(K+1)){
               stud.se[i,k-1] <- phi(-(theta[i,k]-alpha[i,k-1]/2)/exp(beta[k-1]/2))
               stud.sp[i,k-1] <- phi((theta[i,k]+alpha[i,k-1]/2)/exp(-beta[k-1]/2))
	             logit(p[i,k-1]) <- gamma0 + gamma1[k-1]*logit(stud.se[i,k-1])+gamma2[k-1]*logit(stud.sp[i,k-1])
               M[i,k-1] ~ dbern(p[i,k-1])
            }

          }

       for (i in 1:N){
          for (k in 2:(K+1)){
            h1[i,k-1] <- delta[i,k]*y[i,k]*log(stud.se[sid[i],k-1])+delta[i,k]*(1-y[i,k])*log(1-stud.se[sid[i],k-1])             #Partial log Likelihood for a disease subject on test k in study i
            h2[i,k-1] <- delta[i,k]*(1-y[i,k])*log(stud.sp[sid[i],k-1])+delta[i,k]*y[i,k]*log(1-stud.sp[sid[i],k-1])             #Partial log Likelihood for a non-disease subject on test k in study i
            }

            logh1[i] <- log(pi[sid[i]])+sum(h1[i,])     #log Likelihood for a disease subject in study i
            logh2[i] <- log(1-pi[sid[i]])+sum(h2[i,])     #log likelihood for a non-disease subject in study i
            logh3[i] <- log(exp(logh1[i])+exp(logh2[i]))
            #log likelihood for a subject in study i with unknown disease status
            logL[i]  <- n[i]*(delta[i,1]*(y[i,1]*logh1[i]+(1-y[i,1])*logh2[i])+(1-delta[i,1])*logh3[i])                         #log likelihood for a subject in study i regardless of the disease status
            logLC[i] <- -logL[i]+C
            zero[i] ~ dpois(logLC[i])
          }


        Sigma2 <- e%*%inverse(preci2)%*%e
        var21 <- Sigma2[1,1]
        var22 <- Sigma2[2,2]
        rho212 <- Sigma2[1,2]/sqrt(var21*var22)

        Sigma1 <- c%*%inverse(preci1)%*%c
        var11 <- Sigma1[1,1]
        var12 <- Sigma1[2,2]
        var13 <- Sigma1[3,3]
        rho112 <- Sigma1[1,2]/sqrt(var11*var12)
        rho123 <- Sigma1[2,3]/sqrt(var12*var13)
        rho113 <- Sigma1[1,3]/sqrt(var11*var13)


			  # For calculating DIC value:
			  loglik_dic <- -2*sum(logLC[])

 }
