#################################################################################
Readme file for the R code of 
"A Bayesian nonparametric model for spatially distributed multivariate binary data 
with application to a multidrug-resistant tuberculosis (MDR-TB) study"
Author: Jian Kang, Nanhua Zhang, Ran Shi
#################################################################################

Requirement:
package 'MCMCpack', 'pscl', 'MASS', 'msm' and 'ggplot2'

Source files:
'PostMCMC.R': functions for post MCMC analysis
'UpdateFunctions.R': functions for the Gibbs sampling algorithm
'BayesSpatMultiBinaryModel.R': a function wrapper for implementing the algorithm
'DataGenCaseI.R': generating the data example for simulation Case I as described in the paper
'DataGenCaseII.R': generating the data example for simulation Case II as described in the paper


Data files:
'SimuCaseI.RData': data file for simulation Case I with n=800
'SimuCaseII.RData': data file for simulation Case II with n=800
'fulldemo.RData': data file for analysis after running our MCMC algorithm based on Case II


Example implementation:
'RunDataExScript.R'

1. This is the R script demonstrating how to use the functions provided.
2. MCMCsetup contains the parameters required for MCMC update: chain length, gap (thining parameter) and burn-in
3. InitialVal contains the initial guesses for the MCMC chains. Most notations agree with those in the paper. 
   Execptions: Sigma is a scalar, which is the variance for z
               sigma and sigam0 are the variances for alpha and alpha0
4. Priors contains the prior specifications for the hyper-priors 
5. ObsVal contains the observed values: binary outcomes, covariates, eigen-decomposition for the GP
6. 'BayesSpatMultiBinaryModel(MCMCsetup, InitialVal, Priors, ObsVal)' returns the MCMC samples for beta, alpha and eta
7. 'CovrCoef<-PosteriorCovrCoef(MCMCSamples,  MCMCsetup)' provides the posterior mean, median and credible intervals for 
   the covariate coefficient
8. 'SptlSurf<-PosteriorSptlSurf(MCMCSamples, MCMCsetup, phi)' provides the posterior mean, median and credible intervals for
   the spatial effects
9. 'SptlCorr<-PosteriorSptlCorr(MCMCSamples, MCMCsetup, phi, w)' returns the posterior outcome correlations
10. The fuction 'SptlPlot(x, y, z)' can be used to draw the spatial effects as well as correlations.
    x: coordinates at x-axis, y: coordinates at y-axis, z: spatial effects
11. By loading 'SimuCaseI.RData' or 'SimuCaseII.RData', you can implement all the analysis provided above
