model
{
  K <-1000

  for(i in 1:n)
	{	   	
    random[i] <- inprod(alpha[EUID[i],],z[i,])

    for(j in 1:q)
    {		
      # 1: mean
      logit(ph1[i,j,1])  <- inprod(b[,j], xmu.1[i,]) +random[i]*rid[1]
      cloglog(ph1[i,j,2])<- inprod(b[,j], xmu.1[i,]) +random[i]*rid[1]
      probit(ph1[i,j,3]) <- inprod(b[,j], xmu.1[i,]) +random[i]*rid[1]
      mu[i,j]<- inprod(ph1[i,j, ],link[1,])

      # 2: sum  
      log(den[i,j]) <- inprod(d[,j],  xsum.1[i,])+ random[i]*rid[2]
  
      s1[i,j]<- den[i,j]*mu[i,j]
      s2[i,j]<- den[i,j]*(1-mu[i,j])  
  
      ll[i,j]<-  (s1[i,j]-1)*log(y[i,j])+(s2[i,j]-1)*log(1-y[i,j])+
                  loggam(s1[i,j]+s2[i,j])-loggam(s1[i,j])-loggam(s2[i,j])
      trick[i,j] <- K-ll[i,j]  
      zero[i,j] ~ dpois(trick[i,j])    

      ypred[i,j] <- mu[i,j]
      # phi[i,j] <- den[i,j]
    }
  }
                                                          
  for(j in 1:q)
  {		
    tmp1[j] ~ dnorm(0, hyper[j,1,1])
    b[1,j]<- tmp1[j]
    for(i in 1:(p.xmu-1))
  	{
      ###   diffuse normal ###
  		b.tmp[i,j,1] ~ dnorm(0.0, hyper[j,1,2]) 
      
      ###   L1 (larsso) ### 
  		b.tmp[i,j,2] ~ dnorm(0.0,taub.L1[i,j]);  
      taub.L1[i,j] <- 1/sigmab.L1[i,j]; 	        
      sigmab.L1[i,j] ~ dexp(hyper[j,1,3]); 
  		
      ###   L2  (ridge) ###
      b.tmp[i,j,3] ~ dnorm(0.0, taub.L2[j]); 
      
  		###   ARD  ###
  		b.tmp[i,j,4] ~ dnorm(0.0,taub.ARD[i,j]);       
      taub.ARD[i,j] ~ dgamma(hyper[j,1,5], hyper[j,1,5]); 	      
      
      b[i+1,j] <- inprod(b.tmp[i,j, ],prior1[1, ,j])   	
   	}
    taub.L2[j] ~ dgamma(hyper[j,1,4],hyper[j,1,4]);  # L2 (ridge)
   

    tmp2[j] ~ dnorm(0, hyper[j,2,1])
    d[1,j] <- tmp2[j]
  	for(i in 1:(p.xsum-1))
  	{
  		d.tmp[i,j,1] ~ dnorm(0, hyper[j,2,2]) 
      
  		d.tmp[i,j,2] ~ dnorm(0.0,taud.L1[i,j]);  
      taud.L1[i,j] <- 1/sigmad.L1[i,j]; 	        
      sigmad.L1[i,j] ~ dexp(hyper[3]); 

      d.tmp[i,j,3] ~ dnorm(0.0,taud.L2[j]);
  		
  		d.tmp[i,j,4] ~ dnorm(0.0,taud.ARD[i,j]);       
      taud.ARD[i,j] ~ dgamma(hyper[j,2,5], hyper[j,2,5]); 
      	          
      d[i+1,j] <- inprod(d.tmp[i,j, ],prior1[2, ,j])   	
   	}
    taud.L2[j] ~ dgamma(hyper[j,2,4],hyper[j,2,4]);      
  }


  for(i in 1:qz){ mu0[i] <- 0 }
  for(i in 1:nEU){ alpha[i,1:qz] ~ dmnorm(mu0,invSigma) }
  invSigma <- inverse(Sigma)
  Sigma <- Sigma.VC + Sigma.UN
  

  ######################### 1. VC #####################################################
  # if the zdummy is coded in a nested way, then the nested variance structure
  # (banded Compound symmetry) is just a special case of VC
  # in other words, whether CS or not is determined by how zdummy is coded.
  #####################################################################################
  for(i in 1:(qz-1)){
    for(j in (i+1):qz){ 
      Sigma.VC[i,j] <- 0
      Sigma.VC[j,i] <- 0} }
  
  for(i in 1:nz0){   
    sigma.VC1[i] ~ dunif(0, hyper2)       ##### uniform #####   
    
    t[i] ~ dt(0, pow(hyper2,-2), 1);      ##### half_Cauchy #####
    sigma.VC2[i] <- abs(t[i])
    
    for(j in (cumm[i]+1):(cumm[i]+m[i])) {
      Sigma.VC[j,j] <- pow(sigma.VC1[i],2)*prior2[1,1]+ 
                       pow(sigma.VC2[i],2)*prior2[1,2]    
    }  
  }   

  ######################### 2. UN (unstructed) #################################### 
  # used when nz0 = qz, such as continuous random variable with random coefficient
  #################################################################################
  # prior dist'n for rho1 and rho2 are from -1 to 1, while rho3 is contrained to ensure
  # positive definite of correlation matrix R
  rho1 ~ dunif(-0.999,0.999)
  rho2 ~ dunif(-0.999,0.999)
  l <- rho1*rho2 - sqrt( (1-pow(rho1,2))*(1-pow(rho2,2)))
  u <- rho1*rho2 + sqrt( (1-pow(rho1,2))*(1-pow(rho2,2)))
  rho3 ~ dunif(l, u)
  
  R[1,1] <- 1
  R[1,2] <- 0
  R[1,3] <- 0
  R[2,1] <- rho1
  R[2,2] <- sqrt(1-pow(rho1,2))
  R[2,3] <- 0
  R[3,1] <- rho2
  R[3,2] <- (rho3-rho1*rho2)/R[2,2]
  tmp <- abs(1-rho1^2-rho2^2-rho3^2+2*rho1*rho2*rho3)
  R[3,3] <- sqrt(tmp+0.0001)/R[2,2]

  ### uniform
  for(i in 1:qz){ 
    scale1[i] ~ dunif(0, hyper2)  
    for(j in 1:qz){ 
      V1[i,j] <- scale1[i]*equals(i,j)   
    }
  }
  V1R <- V1%*%R[1:qz,1:qz]
  Sigma.UN1 <- V1R%*%t(V1R)

  ###  half t    
  for(i in 1:qz){                        
    scale2[i] ~ dt(0, pow(hyper2,-2), 1);    
    for(j in 1:qz){ 
      V2[i,j] <- abs(scale2[i])*equals(i,j) 
    }
  }
  V2R <- V2%*%R[1:qz,1:qz]
  Sigma.UN2 <- V2R%*%t(V2R)

  Sigma.UN <- Sigma.UN1*prior2[2,1] +Sigma.UN2*prior2[2,2]   

}
