# Use this code to analyze the data in the manuscript
set.seed(12202023)

# Import data for study (Available here: https://zenodo.org/records/569601)
path_data <- "< Your path to data >"
path_save <- "< Your path to where to save output >"
path_figures <- "< Your path to where to save figures >"

ob_baker_meta <- data.frame( data.table::fread( file = paste( path_data, "/nash_ob_baker_results/nash_ob_baker.metadata.txt", sep = ""), header = T ) )
ob_baker_otu_rdp <- data.frame( data.table::fread( file = paste( path_data, "/nash_ob_baker_results/RDP/nash_ob_baker.otu_table.100.denovo.rdp_assigned", sep = "") ) )
ob_baker_otu_denovo <- data.frame( data.table::fread( file = paste( path_data, "/nash_ob_baker_results/nash_ob_baker.otu_table.100.denovo", sep = "") ) )

# Keep Obese and Healthy groups only
OB_and_Healthy <- ob_baker_meta$SampleID[ ob_baker_meta$DiseaseState != "NASH" ]
ob_baker_meta <- ob_baker_meta[ ob_baker_meta$DiseaseState != "NASH", ]
which_rdp <- colnames(ob_baker_otu_rdp) %in% OB_and_Healthy
which_rdp[ 1 ] <- TRUE 
ob_baker_otu_rdp <- ob_baker_otu_rdp[, which_rdp ] 
which_denovo <- colnames(ob_baker_otu_denovo) %in% OB_and_Healthy
which_denovo[ 1 ] <- TRUE 
ob_baker_otu_denovo <- ob_baker_otu_denovo[, which_denovo ] 

# Parse the taxonomy
ob_baker_names <- matrix( NA, nrow( ob_baker_otu_denovo ), 8 )
for( i in 1:nrow( ob_baker_otu_denovo ) ){
 ob_baker_names[i,] <- phyloseq::parse_taxonomy_qiime( ob_baker_otu_rdp$V1[i] )
} 

# Drop samples with less than 100 reads
samples100 <- colSums( ob_baker_otu_denovo[, -c( 1 ) ] ) >= 100
ob_baker_otu_denovo <- ob_baker_otu_denovo[ , c( 1, which( samples100 ) + 1 ) ]

# Drop taxa with less than 10 reads
reads10 <- rowSums( ob_baker_otu_denovo[, -c( 1 ) ] ) >= 10 
ob_baker_otu_denovo <- ob_baker_otu_denovo[ reads10, ]

# Drop taxa with less than 1% nonzero reads
one_percent <- rowSums( ob_baker_otu_denovo[, -c( 1 ) ] != 0 )/( ncol( ob_baker_otu_denovo ) - 1 ) >= 0.01
ob_baker_otu_denovo <- ob_baker_otu_denovo[ one_percent, ] 

# Drop observations that are not labeled by genus
genus <- ob_baker_names[ ob_baker_names[ , 6] != "", c( 6, 8 ) ]
ob_baker_otu_denovo <- ob_baker_otu_denovo[ ob_baker_otu_denovo$OTU_ID %in% genus[ ,2 ], ]

# Collapse counts by genus 
colnames( genus ) <- c( "genus", "OTU_ID" )
ob_baker_otu_denovo_counts <- merge( genus, ob_baker_otu_denovo , by = "OTU_ID" )
ob_baker_otu_denovo_counts <- ob_baker_otu_denovo_counts[ , -1 ]
library(tidyverse)
ob_baker_otu_denovo_counts <- ob_baker_otu_denovo_counts %>% group_by( genus ) %>%  summarise(across(everything(), sum) )

## Set up data for analysis 
# Count matrix
Y <- as.matrix(  t( ob_baker_otu_denovo_counts[,-1] ) )

# Set dimensions
T. <- C. <- ncol( Y )
P_eta <- 2
P_gamma <- 2

# Make sure covariates and count data align
Y_obs <- gsub( "[[:punct:][:blank:]]+", "", rownames(Y) )
Y_obs <- as.matrix( Y_obs, ncol = 1 )
colnames( Y_obs ) <- "Library_Name_s"
Y <- cbind( Y_obs, Y )

colnames( ob_baker_meta )[ 1 ] <- "Library_Name_s"
X_hold <- ob_baker_meta[ , c( "Library_Name_s", "DiseaseState" ) ]
X_hold$Library_Name_s <- gsub( "[[:punct:][:blank:]]+", "", X_hold$Library_Name_s )

YX_merge <- merge( Y, X_hold, by  = "Library_Name_s" )   

X_eta <- as.matrix( YX_merge[ , "DiseaseState" ], ncol = 1 )  
X_eta <- as.numeric( as.factor( X_eta  ) ) - 1

X_eta <- cbind( 1, X_eta )
X_gamma <- X_eta

Y <- as.matrix(  YX_merge[ , -c( 1,99 ) ] ) 
Y <- apply( Y, 2, as.numeric )
N <- nrow( Y )

# Call package
library( MicroMiss ) 
 
# Set up parameters
iterations <- 10000
thin <- 5
samples <- floor( iterations/thin )
Z <- array( 0, dim = c( N, T., samples ) ) 
Zsum <- rowSums( Y )
alpha_it <- array( 0, dim = c( N, T., samples ) ) 
mu_i <- matrix( 0, N, samples )  
zeta_it <- array( 0, dim = c( N, T., samples ) )
omega_eta <- array( 0, dim = c( N, T., samples ) )
beta_eta <- array( 0, dim = c( T., P_eta, samples ) )
beta_gamma <- array( 0, dim = c( T., P_gamma, samples ) )
a_tc <- array( 0, dim = c( T., C., samples ) )
u_t <- rep( 1, T. )

# Set up priors 
nu <- matrix( 1, nrow = T., ncol = C. )
diag( nu ) <- 500
mu_beta_eta_prior <- matrix( 0, T., P_eta )
mu_beta_gamma_prior <- matrix( 0, T., P_gamma )
sigma2_beta_eta <- 1
sigma2_beta_gamma <- 1

# Initialize
Z[ , , 1 ] <- Y
alpha_it[ , , 1 ] <- Y
for( n in 1:nrow(  Y ) ){
  sum_Z <- sum(  Y[ n, ] )
  mu_i[ n, 1 ] <- rgamma( 1, sum_Z, sum_Z );
}
zeta_it[ , , 1 ] <- 1
omega_eta[ , , 1 ] <- 1
a_tc[ , , 1 ] <- 1 

# Run proposed model  
ptm_MicroMiss <- proc.time() 
output_ob_baker_nu500sig1 <- MicroMiss( iterations, thin, N, T., C., P_eta, P_gamma, Y, Z, Zsum, alpha_it, zeta_it, beta_eta, X_eta, 
                     omega_eta, beta_gamma, X_gamma, mu_i, a_tc, u_t, nu, mu_beta_eta_prior, mu_beta_gamma_prior, sigma2_beta_eta, sigma2_beta_gamma )
time_MicroMiss <- proc.time() - ptm_MicroMiss

names( output_ob_baker_nu500sig1 ) <- c( "Z", "alpha_t", "zeta", "beta_eta", "omega_eta", "a_tc", "beta_gamma", "mu" )

# Save or load
#save( output_ob_baker_nu500sig1 , file = paste( path_save, "output_ob_baker_nu500sig1.rda", sep = "" ) )
#load( file = paste( path_save, "output_ob_baker_nu500sig1.rda", sep = "" ) )

# Beta Eta  
post_beta_eta_MicroMiss <- apply( output_ob_baker_nu500sig1$beta_eta[ , , (floor(samples)/2 + 1):(floor(samples))  ] , c(1,2), mean )  
post_beta_eta_MicroMiss_upper <- apply( output_ob_baker_nu500sig1$beta_eta[ , , (floor(samples)/2 + 1):(floor(samples)) ] , c(1,2), function( x ){ quantile( x , 0.975 ) } )  
post_beta_eta_MicroMiss_lower <- apply( output_ob_baker_nu500sig1$beta_eta[ , , (floor(samples)/2 + 1):(floor(samples)) ] , c(1,2), function( x ){ quantile( x , 0.025 ) } ) 

sig_beta_eta_MicroMiss <- sign( post_beta_eta_MicroMiss_lower[,2] ) == sign( post_beta_eta_MicroMiss_upper[,2] ) 
 
# Beta Gamma  
post_beta_gamma_MicroMiss <- apply( output_ob_baker_nu500sig1$beta_gamma[ , , (floor(samples)/2 + 1):(floor(samples)) ] , c(1,2), mean )  
post_beta_gamma_MicroMiss_upper <- apply( output_ob_baker_nu500sig1$beta_gamma[ , , (floor(samples)/2 + 1):(floor(samples)) ] , c(1,2), function( x ){ quantile( x , 0.975 ) } )  
post_beta_gamma_MicroMiss_lower <- apply( output_ob_baker_nu500sig1$beta_gamma[ , , (floor(samples)/2 + 1):(floor(samples)) ] , c(1,2), function( x ){ quantile( x , 0.025 ) } ) 

sig_beta_gamma_MicroMiss <- sign( post_beta_gamma_MicroMiss_lower[,2] ) == sign( post_beta_gamma_MicroMiss_upper[,2] ) 
 
# Relative effect estimates  
MCMC_gamma_unit <- effects_all(output_ob_baker_nu500sig1$beta_gamma, X_gamma[c(3,3),], T., 2000,1 ) 

post_rel_MicroMiss <- apply( MCMC_gamma_unit[ , , (floor(samples)/2 + 1):(floor(samples)) ] , c(1,2), mean )  
post_rel_MicroMiss_upper <- apply( MCMC_gamma_unit[ , , (floor(samples)/2 + 1):(floor(samples)) ] , c(1,2), function( x ){ quantile( x , 0.975 ) } )  
post_rel_MicroMiss_lower <- apply( MCMC_gamma_unit[ , , (floor(samples)/2 + 1):(floor(samples)) ] , c(1,2), function( x ){ quantile( x , 0.025 ) } ) 

pos_MicroMiss <- post_rel_MicroMiss_upper > 1 & post_rel_MicroMiss_lower > 1  
neg_MicroMiss <- post_rel_MicroMiss_upper < 1 & post_rel_MicroMiss_lower < 1  

# Estimate relative abundances 
alpha_norm <- apply( output_ob_baker_nu500sig1$alpha_t[ , , (floor(samples)/2 + 1):(floor(samples)) ], c(1,3), sum ) 
psi <- output_ob_baker_nu500sig1$alpha_t[ , , (floor(samples)/2 + 1):(floor(samples)) ] 
for (i in 1:length((floor(samples)/2 + 1):(floor(samples))) ){
  psi[,,i] <- psi[,,i]/alpha_norm[,i]
}
post_psi <- apply( psi , c(1,2), mean )   
post_psi_upper <- apply( psi , c(1,2), function( x ){ quantile( x , 0.975 ) } )  
post_psi_lower <- apply( psi , c(1,2), function( x ){ quantile( x , 0.025 ) } )  

# Run ZIDM Model 
ptm_ZIDM <- proc.time() 
output_ZIDM <- MicroMiss::ZIDM( iterations, thin, N, T., P_eta, P_gamma, Y, Zsum, alpha_it, zeta_it, beta_eta, X_eta, 
                     omega_eta, beta_gamma, X_gamma, mu_i, mu_beta_eta_prior, mu_beta_gamma_prior, sigma2_beta_eta, sigma2_beta_gamma )
time_ZIDM <- proc.time() - ptm_ZIDM

names( output_ZIDM ) <- c( "alpha_t", "zeta", "beta_eta", "omega_eta", "beta_gamma")

# Save or load
#save( output_ZIDM , file = paste( path_save, "ob_baker_sig1_ZIDM.rda", sep = "" ) )
#load( file = paste( path_save, "ob_baker_sig1_ZIDM.rda", sep = "" ) )

# Beta Eta  
post_beta_eta_ZIDM <- apply( output_ZIDM$beta_eta[ , , (floor(samples)/2 + 1):(floor(samples)) ] , c(1,2), mean )  
post_beta_eta_ZIDM_upper <- apply( output_ZIDM$beta_eta[ , , (floor(samples)/2 + 1):(floor(samples)) ] , c(1,2), function( x ){ quantile( x , 0.975 ) } )  
post_beta_eta_ZIDM_lower <- apply( output_ZIDM$beta_eta[ , , (floor(samples)/2 + 1):(floor(samples)) ] , c(1,2), function( x ){ quantile( x , 0.025 ) } ) 

sig_beta_eta_ZIDM <- sign( post_beta_eta_ZIDM_lower[,2] ) == sign( post_beta_eta_ZIDM_upper[,2] ) 
 
# Beta Gamma  
post_beta_gamma_ZIDM <- apply( output_ZIDM$beta_gamma[ , , (floor(samples)/2 + 1):(floor(samples)) ] , c(1,2), mean )  
post_beta_gamma_ZIDM_upper <- apply( output_ZIDM$beta_gamma[ , , (floor(samples)/2 + 1):(floor(samples)) ] , c(1,2), function( x ){ quantile( x , 0.975 ) } )  
post_beta_gamma_ZIDM_lower <- apply( output_ZIDM$beta_gamma[ , , (floor(samples)/2 + 1):(floor(samples)) ] , c(1,2), function( x ){ quantile( x , 0.025 ) } ) 

sig_beta_gamma_ZIDM <- sign( post_beta_gamma_ZIDM_lower[,2] ) == sign( post_beta_gamma_ZIDM_upper[,2] ) 

# Relative effect estimate
MCMC_gamma_unit_ZIDM <- effects_all(output_ZIDM$beta_gamma, X_gamma[c(3,3),], T., 2000 , 1) 

post_rel_ZIDM <- apply( MCMC_gamma_unit_ZIDM[ , , (floor(samples)/2 + 1):(floor(samples)) ] , c(1,2), mean )  
post_rel_ZIDM_upper <- apply( MCMC_gamma_unit_ZIDM[ , , (floor(samples)/2 + 1):(floor(samples)) ] , c(1,2), function( x ){ quantile( x , 0.975 ) } )  
post_rel_ZIDM_lower <- apply( MCMC_gamma_unit_ZIDM[ , , (floor(samples)/2 + 1):(floor(samples)) ] , c(1,2), function( x ){ quantile( x , 0.025 ) } ) 

pos <- post_rel_ZIDM_upper > 1 & post_rel_ZIDM_lower > 1  
neg <- post_rel_ZIDM_upper < 1 & post_rel_ZIDM_lower < 1  

# Estimate Relative abundances 
alpha_norm_ZIDM <- apply( output_ZIDM$alpha_t[ , , (floor(samples)/2 + 1):(floor(samples)) ], c(1,3), sum ) 
psi_ZIDM <- output_ZIDM$alpha_t[ , , (floor(samples)/2 + 1):(floor(samples)) ] 
for (i in 1:length((floor(samples)/2 + 1):(floor(samples))) ){
  psi_ZIDM[,,i] <- psi_ZIDM[,,i]/alpha_norm_ZIDM[,i]
}
post_psi_ZIDM <- apply( psi_ZIDM , c(1,2), mean )   
post_psi_ZIDM_upper <- apply( psi_ZIDM , c(1,2), function( x ){ quantile( x , 0.975 ) } )  
post_psi_ZIDM_lower <- apply( psi_ZIDM , c(1,2), function( x ){ quantile( x , 0.025 ) } ) 

# Generate Stacked Bar by Family ( Figure 2 )
OB_psi <- data.frame( post_psi[ X_eta[,2] == 1, ] )
colnames( OB_psi ) <- ob_baker_otu_denovo_counts$genus 

Healthy_psi <- data.frame( post_psi[ X_eta[,2] == 0, ] ) 
colnames( Healthy_psi ) <- ob_baker_otu_denovo_counts$genus 

OB_psi$label <- "Obese"
Healthy_psi$label <- "Healthy"

data <- data.frame( rbind( OB_psi,Healthy_psi ) ) 
family <- numeric()
for( i in colnames( data )[ 1:97 ] ){
  family <- rbind( family, ob_baker_names[ which( gsub( "[[:punct:][:blank:]]+", "", i ) == gsub( "[[:punct:][:blank:]]+", "", ob_baker_names[,6] ))[1], 5:6 ] )
}
colnames( family ) <- c( "Family", "Genus" )   
data$id <- rownames( data )
data_long <- reshape2::melt( data , id.vars = c( "label", "id" ), variable.name = "RA" )
k <- merge( data_long, family, by.x = "RA", by.y = "Genus" )
family_data <- k %>% group_by( Family, label, id ) %>% summarise( value = sum( value ) )

family_data$Family <- ifelse( family_data$Family == "Clostridiaceae_1", "Clostridiaceae ", family_data$Family)
family_data$Family <- ifelse( family_data$Family == "Clostridiales_Incertae_Sedis_XI", "Clostridiales Incertae Sedis XI", family_data$Family)
family_data$Family <- ifelse( family_data$Family == "Clostridiales_Incertae_Sedis_XIII", "Clostridiales Incertae Sedis XIII", family_data$Family)
family_data$Family <- ifelse( family_data$Family == "Peptococcaceae_1", "Peptococcaceae", family_data$Family)
family_data$Family <- ifelse( family_data$Family == "Incertae_Sedis_XI", "Incertae Sedis XI", family_data$Family)

fig2 <- ggplot( family_data, aes(x = as.factor(id), y = value, fill = Family ))  +
  geom_bar(position = "fill",stat = "identity") + facet_grid(~label, scale="free_x", space="free") +
  theme_bw() + theme(axis.title.x=element_blank(), axis.text.x=element_blank(), axis.ticks.x=element_blank() ) +
  scale_y_continuous(limits = c(0,1), expand = expansion(mult = c(0, 0))) + ylab( "Relative Abundance" )

# Save
#ggsave( paste0( path_figures, "Figure2.png" ), plot = fig2 ) 

# Create dataframe for pi plot ( Figure 3 )
mean <- c( log( post_rel_MicroMiss[ 1, ] ), log( post_rel_ZIDM[ 1, ] ) )
upper <- c(  log(  post_rel_MicroMiss_upper[ 1, ] ), log(  post_rel_ZIDM_upper[ 1, ] ) )
lower <- c(  log(  post_rel_MicroMiss_lower[ 1, ] ), log(  post_rel_ZIDM_lower[ 1, ] ) )
label <- c(  ob_baker_otu_denovo_counts$genus,  ob_baker_otu_denovo_counts$genus )
sig <- c( c( pos_MicroMiss[ 1, ] | neg_MicroMiss[ 1, ]) , c( pos[ 1, ] | neg[ 1, ] ) )
Model <- c( rep( "MicroMiss", length( post_rel_MicroMiss[ 1, ] ) ), rep( "ZIDM", length( post_rel_MicroMiss[ 1, ] ) ) )

data <- data.frame( mean, upper, lower, label, Model, sig )  

fig3 <- ggplot( data, aes(y=mean, x=label, col = Model)) + 
  scale_color_manual("Model",values=c(  "#1A242F", "#A3A7AB"))+
  geom_point(position=position_dodge(0.8), size=1  ) +
  geom_errorbar(aes(ymin=upper, ymax=lower), position = position_dodge(0.8), width = 0.3  ) +
  theme_bw(base_size=16) + 
  theme(text = element_text(size=8), axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1) )  + ylab( expression(paste(log(pi[t](x[obesity]))), sep = "")) + xlab( "Genus")

# Save
#ggsave( paste0( path_figures, "Figure3.png" ), plot = fig3 ) 

# Create dataframe for Beta Eta plot ( Figure 4 )
mean <- c( post_beta_eta_MicroMiss[ ,2 ] , post_beta_eta_ZIDM[ ,2 ] )
upper <- c( post_beta_eta_MicroMiss_upper[ ,2 ] , post_beta_eta_ZIDM_upper[ ,2 ] )
lower <- c( post_beta_eta_MicroMiss_lower[ ,2 ] , post_beta_eta_ZIDM_lower[ ,2 ] )
label <- c( ob_baker_otu_denovo_counts$genus,  ob_baker_otu_denovo_counts$genus )
Model <- c( rep( "MicroMiss", length( post_beta_gamma_MicroMiss[ ,2 ] ) ), rep( "ZIDM", length( post_beta_gamma_MicroMiss[ ,2 ] ) ) )

data <- data.frame( mean, upper, lower, label, Model )  

fig4 <- ggplot( data, aes(y=mean, x=label, col = Model)) + 
  scale_color_manual("Model", values=c(  "#1A242F", "#A3A7AB"))+
  geom_point(position=position_dodge(0.8), size=1  ) +
  geom_errorbar(aes(ymin=upper, ymax=lower), position = position_dodge(0.8), width = 0.3  ) +
  theme_bw(base_size=16) + 
  theme(text = element_text(size=8), axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1) ) + ylab( expression(paste(beta[eta[t]][obesity] ))) + xlab("Genus")  

# Save
#ggsave( paste0( path_figures, "Figure4.png" ), plot = fig4 ) 

## Table 3
# Relative abundances by disease status table 
OB <-  colMeans( post_psi[ X_eta[,2] == 1, ] ) 
Healthy <-  colMeans( post_psi[ X_eta[,2] == 0, ] )    

# Relative abundances by disease status table for ZIDM
OB_ZIDM <-  colMeans( post_psi_ZIDM[ X_eta[,2] == 1, ] ) 
Healthy_ZIDM <-  colMeans( post_psi_ZIDM[ X_eta[,2] == 0, ] )   
RA <- cbind( Genus = ob_baker_otu_denovo_counts$genus, OB , Healthy, OB_ZIDM, Healthy_ZIDM )

# Make datasets for comparison
OB_psi <- data.frame( post_psi[ X_eta[,2] == 1, ] )
colnames( OB_psi ) <- ob_baker_otu_denovo_counts$genus 

Healthy_psi <- data.frame( post_psi[ X_eta[,2] == 0, ] ) 
colnames( Healthy_psi ) <- ob_baker_otu_denovo_counts$genus 

OB_psi$label <- "Obese"
Healthy_psi$label <- "Healthy"

data <- data.frame( rbind( OB_psi, Healthy_psi ) ) 
 
OB_psi_ZIDM <- data.frame( post_psi_ZIDM[ X_eta[,2] == 1, ] )
colnames( OB_psi_ZIDM ) <- ob_baker_otu_denovo_counts$genus 

Healthy_psi_ZIDM <- data.frame( post_psi_ZIDM[ X_eta[,2] == 0, ] ) 
colnames( Healthy_psi_ZIDM ) <- ob_baker_otu_denovo_counts$genus 

OB_psi_ZIDM$label <- "Obese"
Healthy_psi_ZIDM$label <- "Healthy"

data_ZIDM <- data.frame( rbind( OB_psi_ZIDM, Healthy_psi_ZIDM ) ) 

# CLR KW Test
library( compositions )
data_CLR <- clr( data[ ,-c(98) ] )
test_MicroMiss_CLR <- numeric()
for( i in 1:( ncol(data) - 1 )) {
  test_MicroMiss_CLR <- c( test_MicroMiss_CLR, kruskal.test(data_CLR[,i] ~ data$label )$p.value )
}

data_ZIDM_CLR <- clr( data_ZIDM[ ,-c(98) ] )
test_ZIDM_CLR <- numeric()
for( i in 1:( ncol(data_ZIDM) - 1 )) {
  test_ZIDM_CLR <- c( test_ZIDM_CLR, kruskal.test(data_ZIDM_CLR[,i] ~ data_ZIDM$label )$p.value )
}

RA <- data.frame( cbind( RA, MM_CLR_p_value = test_MicroMiss_CLR , ZM_CLR_p_value = test_ZIDM_CLR  ) )
RA_family <- merge( RA, family, by.x = "Genus", by.y = "Genus")
RA_family <- RA_family[ as.numeric( RA$OB ) > 0.01 | as.numeric( RA$Healthy ) > 0.01, ]

# Generate CLR table ( Supplementary Table S5 )
CLR_obese <- colMeans( data_CLR[ data$label == "Obese" ] )
CLR_healthy <- colMeans( data_CLR[ data$label == "Healthy" ] )
CLR_ZIDM_obese <- colMeans( data_ZIDM_CLR[ data$label == "Obese" ] )
CLR_ZIDM_healthy <- colMeans( data_ZIDM_CLR[ data$label == "Healthy" ] )

CLR_table <- data.frame( CLR_obese, CLR_healthy, CLR_ZIDM_obese, CLR_ZIDM_healthy, Genus = names( CLR_healthy ) ) 
CLR_table <- merge( CLR_table, family, by.x = "Genus", by.y = "Genus" )
 
CLR_table$CLR_obese <- round( as.numeric( CLR_table$CLR_obese ), 2 )
CLR_table$CLR_healthy <- round( as.numeric( CLR_table$CLR_healthy ), 2 )
CLR_table$CLR_ZIDM_healthy <- round( as.numeric( CLR_table$CLR_ZIDM_healthy ), 2 )
CLR_table$CLR_ZIDM_obese <- round( as.numeric( CLR_table$CLR_ZIDM_obese ), 2 )

# Plot histogram after CLR transformation for revision
library( gridExtra )
data_CLR_hist <- data
data_CLR_hist[ , -98 ] <- data_CLR[ , -98 ]
data_CLR_ZIDM_hist <- data_ZIDM
data_CLR_ZIDM_hist[ , -98 ] <- data_ZIDM_CLR[ , -98 ]

f1 <- ggplot( data_CLR_hist, aes(Ruminococcus, fill = label)) + geom_density(alpha = 0.2) + xlim( c(-10,10) ) + guides(fill=guide_legend(title="Disease Status")) + ggtitle("MicroMiss") + xlab("")
f2 <- ggplot( data_CLR_ZIDM_hist, aes(Ruminococcus, fill = label)) + geom_density(alpha = 0.2) + xlim( c(-10,10) ) + guides(fill=guide_legend(title="Disease Status")) + ggtitle("ZIDM")

# Save
#ggsave( paste0( path_figures, "fig_r1.png" ), arrangeGrob( f1, f2) ) 

# Create dataframe for Beta Gamma plot ( Supplementary Figure 1 )
mean <- c( post_beta_gamma_MicroMiss[ ,2 ] , post_beta_gamma_ZIDM[ ,2 ] )
upper <- c( post_beta_gamma_MicroMiss_upper[ ,2 ] , post_beta_gamma_ZIDM_upper[ ,2 ] )
lower <- c( post_beta_gamma_MicroMiss_lower[ ,2 ] , post_beta_gamma_ZIDM_lower[ ,2 ] )
label <- c( ob_baker_otu_denovo_counts$genus,  ob_baker_otu_denovo_counts$genus )
Model <- c( rep( "MicroMiss", length( post_beta_gamma_MicroMiss[ ,2 ] ) ), rep( "ZIDM", length( post_beta_gamma_MicroMiss[ ,2 ] ) ) )

data <- data.frame( mean, upper, lower, label, Model )  

sfig1 <- ggplot( data, aes(y=mean, x=label, col = Model)) + 
  scale_color_manual("Model",values=c( "#1A242F", "#A3A7AB"))+
  geom_point(position=position_dodge(0.8), size=1  ) +
  geom_errorbar(aes(ymin=upper, ymax=lower), position = position_dodge(0.8), width = 0.3  ) +
  theme_bw(base_size=16) + 
  theme(text = element_text(size=8), axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1) )  + ylab( expression(paste(beta[gamma[t]][obesity] ))) + xlab("Genus")

# Save
#ggsave( paste0( path_figures, "SuppFig1.png" ), plot = sfig1 ) 


# Convergence plots of multiplicative effect on relative abundance ( Supplementary Figure 2 )
set.seed(12202023)
conv <-  data.frame( cbind( id = c("Random_1","Random_2","Random_3","Random_4","Random_5"), MCMC_gamma_unit[ 1, sample(1:T., 5) ,] ) )

conv_long <- data.frame( reshape2::melt( conv,  id.vars = "id") ) 
conv_long$value <- log( as.numeric( conv_long$value ) )
conv_long$variable <- as.numeric( conv_long$variable )
conv_long$burnin <- c(rep(0,5000), rep(1,5000) ) 

sfig2 <- ggplot( conv_long, aes( y = value, x = variable, alpha = burnin ) ) + geom_line() + facet_grid(. ~ id) +  theme(text = element_text(size=8), axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1) ) + ylab( expression(paste(log(pi[t](x[obesity]))))) + xlab("MCMC Iteration") + theme(legend.position="none")

# Save
#ggsave( paste0( path_figures, "SuppFig2.png" ), plot = sfig2 ) 

###############################################################
### Evaluate phylogenetic tree-based prior on classification 
###############################################################

# Genus mapped to Family for those in analysis
FamilyGenus <- ob_baker_names[ ,c(5,6) ]
colnames( FamilyGenus ) <- c( "family", "genus" )
dataGenus <- ob_baker_otu_denovo_counts[ ,1 ]
FamilyGenus <- merge( dataGenus, FamilyGenus, by = "genus" )
FamilyGenus <- unique ( FamilyGenus )
FamilyGenus[ c(28, 29, 30,  31) , c(1,2) ] <- FamilyGenus[ c(29, 30, 31, 28) , c(1,2) ] # fix order

# Set prior accordingly
nu2 <- matrix( 1, ncol( Y ), ncol( Y ) )
for( i in 1:ncol(Y) ){
  for( j in 1:ncol(Y) ){
    if( FamilyGenus$family[ i ] == FamilyGenus$family[ j ] ){
      nu2[i,j] = 100
      nu2[j,i] = 100
      }
  }
}
diag( nu2 ) <- 500

# Run proposed model with tree-based prior 
ptm_MicroMissPhylo <- proc.time() 
output_ob_baker_nuPhylo_sig1 <- MicroMiss( iterations, thin, N, T., C., P_eta, P_gamma, Y, Z, Zsum, alpha_it, zeta_it, beta_eta, X_eta, 
                                        omega_eta, beta_gamma, X_gamma, mu_i, a_tc, u_t, nu2, mu_beta_eta_prior, mu_beta_gamma_prior, sigma2_beta_eta, sigma2_beta_gamma )
time_MicroMissPhylo <- proc.time() - ptm_MicroMissPhylo

names( output_ob_baker_nuPhylo_sig1 ) <- c( "Z", "alpha_t", "zeta", "beta_eta", "omega_eta", "a_tc", "beta_gamma", "mu" )

# Save or load
#save( output_ob_baker_nuPhylo_sig1 , file = paste( path_save, "output_ob_baker_nuPhylo_sig1.rda", sep = "" ) )
#load( file = paste( path_save, "output_ob_baker_nuPhylo_sig1.rda", sep = "" ) )

 
# Beta Eta  
post_beta_eta_MicroMissPhylo <- apply( output_ob_baker_nuPhylo_sig1$beta_eta[ , , (floor(samples)/2 + 1):(floor(samples))  ] , c(1,2), mean )  
post_beta_eta_MicroMissPhylo_upper <- apply( output_ob_baker_nuPhylo_sig1$beta_eta[ , , (floor(samples)/2 + 1):(floor(samples)) ] , c(1,2), function( x ){ quantile( x , 0.975 ) } )  
post_beta_eta_MicroMissPhylo_lower <- apply( output_ob_baker_nuPhylo_sig1$beta_eta[ , , (floor(samples)/2 + 1):(floor(samples)) ] , c(1,2), function( x ){ quantile( x , 0.025 ) } ) 

sig_beta_eta_MicroMissPhylo <- sign( post_beta_eta_MicroMissPhylo_lower[,2] ) == sign( post_beta_eta_MicroMissPhylo_upper[,2] ) 

# Beta Gamma  
post_beta_gamma_MicroMissPhylo <- apply( output_ob_baker_nuPhylo_sig1$beta_gamma[ , , (floor(samples)/2 + 1):(floor(samples)) ] , c(1,2), mean )  
post_beta_gamma_MicroMissPhylo_upper <- apply( output_ob_baker_nuPhylo_sig1$beta_gamma[ , , (floor(samples)/2 + 1):(floor(samples)) ] , c(1,2), function( x ){ quantile( x , 0.975 ) } )  
post_beta_gamma_MicroMissPhylo_lower <- apply( output_ob_baker_nuPhylo_sig1$beta_gamma[ , , (floor(samples)/2 + 1):(floor(samples)) ] , c(1,2), function( x ){ quantile( x , 0.025 ) } ) 

sig_beta_gamma_MicroMissPhylo <- sign( post_beta_gamma_MicroMissPhylo_lower[,2] ) == sign( post_beta_gamma_MicroMissPhylo_upper[,2] ) 

# Relative effect estimates  
MCMC_gamma_unit <- effects_all( output_ob_baker_nuPhylo_sig1$beta_gamma, X_gamma[ c(3,3), ], T., 2000, 1  ) 

post_rel_MicroMissPhylo <- apply( MCMC_gamma_unit[ , , (floor(samples)/2 + 1):(floor(samples)) ] , c(1,2), mean )  
post_rel_MicroMissPhylo_upper <- apply( MCMC_gamma_unit[ , , (floor(samples)/2 + 1):(floor(samples)) ] , c(1,2), function( x ){ quantile( x , 0.975 ) } )  
post_rel_MicroMissPhylo_lower <- apply( MCMC_gamma_unit[ , , (floor(samples)/2 + 1):(floor(samples)) ] , c(1,2), function( x ){ quantile( x , 0.025 ) } ) 

pos_MicroMissPhylo <- post_rel_MicroMissPhylo_upper > 1 & post_rel_MicroMissPhylo_lower > 1  
neg_MicroMissPhylo <- post_rel_MicroMissPhylo_upper < 1 & post_rel_MicroMissPhylo_lower < 1  

# Estimate relative abundances 
alpha_norm_MicroMissPhylo <- apply( output_ob_baker_nuPhylo_sig1$alpha_t[ , , (floor(samples)/2 + 1):(floor(samples)) ], c(1,3), sum ) 
psi_MicroMissPhylo <- output_ob_baker_nuPhylo_sig1$alpha_t[ , , (floor(samples)/2 + 1):(floor(samples)) ] 
for (i in 1:length((floor(samples)/2 + 1):(floor(samples))) ){
  psi_MicroMissPhylo[,,i] <- psi_MicroMissPhylo[,,i]/alpha_norm_MicroMissPhylo[,i]
}
post_psi_MicroMissPhylo <- apply( psi_MicroMissPhylo , c(1,2), mean )   
post_psi_MicroMissPhylo_upper <- apply( psi_MicroMissPhylo , c(1,2), function( x ){ quantile( x , 0.975 ) } )  
post_psi_MicroMissPhylo_lower <- apply( psi_MicroMissPhylo , c(1,2), function( x ){ quantile( x , 0.025 ) } )  


# Create dataframe for pi plot ( Supplementary Figure S3 )
mean <- c( log( post_rel_MicroMiss[ 1, ] ), log( post_rel_MicroMissPhylo[ 1, ] ) )
upper <- c(  log(  post_rel_MicroMiss_upper[ 1, ] ), log(  post_rel_MicroMissPhylo_upper[ 1, ] ) )
lower <- c(  log(  post_rel_MicroMiss_lower[ 1, ] ), log(  post_rel_MicroMissPhylo_lower[ 1, ] ) )
label <- c(  ob_baker_otu_denovo_counts$genus,  ob_baker_otu_denovo_counts$genus )
Model <- c( rep( "MicroMiss", length( post_rel_MicroMiss[ 1, ] ) ), rep( "MicroMissPhylo", length( post_rel_MicroMiss[ 1, ] ) ) )

data <- data.frame( mean, upper, lower, label, Model )  

sfig3 <- ggplot( data, aes(y=mean, x=label, col = Model)) + 
  scale_color_manual("Model",values=c( "#1A242F", "#A3A7AB")) +
  geom_point(position=position_dodge(0.8), size=1  ) +
  geom_errorbar(aes(ymin=upper, ymax=lower), position = position_dodge(0.8), width = 0.3  ) +
  theme_bw(base_size=16) + 
  theme(text = element_text(size=8), axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1) )  + ylab( expression(paste(log(pi[t](x[obesity]))))) + xlab("Genus")

# Save
#ggsave( paste0( path_figures, "SuppFig3.png" ), plot = sfig3 ) 


# Create dataframe for Beta Eta plot ( Supplementary Figure S4 )
mean <- c( post_beta_eta_MicroMiss[ ,2 ] , post_beta_eta_MicroMissPhylo[ ,2 ] )
upper <- c( post_beta_eta_MicroMiss_upper[ ,2 ] , post_beta_eta_MicroMissPhylo_upper[ ,2 ] )
lower <- c( post_beta_eta_MicroMiss_lower[ ,2 ] , post_beta_eta_MicroMissPhylo_lower[ ,2 ] )
label <- c( ob_baker_otu_denovo_counts$genus,  ob_baker_otu_denovo_counts$genus )
Model <- c( rep( "MicroMiss", length( post_beta_gamma_MicroMiss[ ,2 ] ) ), rep( "MicroMissPhylo", length( post_beta_gamma_MicroMiss[ ,2 ] ) ) )

data <- data.frame( mean, upper, lower, label, Model )  

sfig4 <- ggplot( data, aes(y=mean, x=label, col = Model)) + 
  scale_color_manual("Model",values=c( "#1A242F", "#A3A7AB"))+
  geom_point(position=position_dodge(0.8), size=1  ) +
  geom_errorbar(aes(ymin=upper, ymax=lower), position = position_dodge(0.8), width = 0.3  ) +
  theme_bw(base_size=16) + 
  theme(text = element_text(size=8), axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1) ) + ylab( expression(paste(beta[eta[t]][obesity] ))) + xlab("Genus")  

# Save
#ggsave( paste0( path_figures, "SuppFig4.png" ), plot = sfig4 ) 

###############################################################
### Evaluate impact of adding noise to observed data on results
###############################################################

# Set priors back to original analysis
nu <- matrix( 1, nrow = T., ncol = C. )
diag(nu) <- 500
mu_beta_eta_prior <- matrix( 0, T., P_eta )
mu_beta_gamma_prior <- matrix( 0, T., P_gamma )
sigma2_beta_eta <- 1
sigma2_beta_gamma <- 1

# Adjust Y using phylo tree
# Set prior accordingly
reclass_prob <- matrix( 1, ncol( Y ), ncol( Y ) )
for( i in 1:ncol(Y) ){
  for( j in 1:ncol(Y) ){
    if( FamilyGenus$family[ i ] == FamilyGenus$family[ j ] ){
      reclass_prob[ i, j ] = 50
      reclass_prob[ j, i ] = 50
    }
  }
}
diag( reclass_prob ) <- 10000
reclass_prob <- reclass_prob/rowSums( reclass_prob )
reclass_Y <- matrix( 0 , nrow(Y), ncol(Y) )

set.seed(1)
for( n in 1:nrow(Y)  ){
  for( t in 1:T. ){
    if( Y[ n, t ] > 0 ){
      x <- rmultinom( 1, Y[ n, t ], reclass_prob[ t, ] )  
      reclass_Y[ n, ] <-  reclass_Y[ n, ] + x
    }
  }
}

# Initialize
Z[ , , 1 ] <- reclass_Y
alpha_it[ , , 1 ] <- reclass_Y
for( n in 1:nrow(  reclass_Y ) ){
  sum_Z <- sum(  reclass_Y[ n, ] )
  mu_i[ n, 1 ] <- rgamma( 1, sum_Z, sum_Z );
}

ptm_MicroMissNoise1 <- proc.time() 
output_ob_baker_MicroMissNoise1_nu500sig1 <- MicroMiss( iterations, thin, N, T., C., P_eta, P_gamma, reclass_Y, Z, Zsum, alpha_it, zeta_it, beta_eta, X_eta, 
                                        omega_eta, beta_gamma, X_gamma, mu_i, a_tc, u_t, nu, mu_beta_eta_prior, mu_beta_gamma_prior, sigma2_beta_eta, sigma2_beta_gamma )
time_MicroMissNoise1 <- proc.time() - ptm_MicroMissNoise1

names( output_ob_baker_MicroMissNoise1_nu500sig1 ) <- c( "Z", "alpha_t", "zeta", "beta_eta", "omega_eta", "a_tc", "beta_gamma", "mu" )

# Save or load
#save( output_ob_baker_MicroMissNoise1_nu500sig1 , file = paste( path_save, "output_ob_baker_MicroMissNoise1_nu500sig1", sep = "" ) )
#load( file = paste( path_save, "output_ob_baker_MicroMissNoise1_nu500sig1.rda", sep = "" ) )
 
# Beta Eta MicroMissNoise1
post_beta_eta_MicroMissNoise1 <- apply( output_ob_baker_MicroMissNoise1_nu500sig1$beta_eta[ , , (floor(samples)/2 + 1):(floor(samples))  ] , c(1,2), mean )  
post_beta_eta_MicroMissNoise1_upper <- apply( output_ob_baker_MicroMissNoise1_nu500sig1$beta_eta[ , , (floor(samples)/2 + 1):(floor(samples)) ] , c(1,2), function( x ){ quantile( x , 0.975 ) } )  
post_beta_eta_MicroMissNoise1_lower <- apply( output_ob_baker_MicroMissNoise1_nu500sig1$beta_eta[ , , (floor(samples)/2 + 1):(floor(samples)) ] , c(1,2), function( x ){ quantile( x , 0.025 ) } ) 

sig_beta_eta_MicroMissNoise1 <- sign( post_beta_eta_MicroMissNoise1_lower[,2] ) == sign( post_beta_eta_MicroMissNoise1_upper[,2] )
 
# Beta Gamma MicroMissNoise1
post_beta_gamma_MicroMissNoise1 <- apply( output_ob_baker_MicroMissNoise1_nu500sig1$beta_gamma[ , , (floor(samples)/2 + 1):(floor(samples)) ] , c(1,2), mean )  
post_beta_gamma_MicroMissNoise1_upper <- apply( output_ob_baker_MicroMissNoise1_nu500sig1$beta_gamma[ , , (floor(samples)/2 + 1):(floor(samples)) ] , c(1,2), function( x ){ quantile( x , 0.975 ) } )  
post_beta_gamma_MicroMissNoise1_lower <- apply( output_ob_baker_MicroMissNoise1_nu500sig1$beta_gamma[ , , (floor(samples)/2 + 1):(floor(samples)) ] , c(1,2), function( x ){ quantile( x , 0.025 ) } ) 

sig_beta_gamma_MicroMissNoise1 <- sign( post_beta_gamma_MicroMissNoise1_lower[,2] ) == sign( post_beta_gamma_MicroMissNoise1_upper[,2] ) 

# Relative effect estimates  
MCMC_gamma_unit <- effects_all( output_ob_baker_MicroMissNoise1_nu500sig1$beta_gamma, X_gamma[ c(3,3), ], T., 2000,1 ) 

post_rel_MicroMissNoise1 <- apply( MCMC_gamma_unit[ , , (floor(samples)/2 + 1):(floor(samples)) ] , c(1,2), mean )  
post_rel_MicroMissNoise1_upper <- apply( MCMC_gamma_unit[ , , (floor(samples)/2 + 1):(floor(samples)) ] , c(1,2), function( x ){ quantile( x , 0.975 ) } )  
post_rel_MicroMissNoise1_lower <- apply( MCMC_gamma_unit[ , , (floor(samples)/2 + 1):(floor(samples)) ] , c(1,2), function( x ){ quantile( x , 0.025 ) } ) 

pos_MicroMissNoise1 <- post_rel_MicroMissNoise1_upper > 1 & post_rel_MicroMissNoise1_lower > 1  
neg_MicroMissNoise1 <- post_rel_MicroMissNoise1_upper < 1 & post_rel_MicroMissNoise1_lower < 1 

# Estimate relative abundances 
alpha_norm_MicroMissNoise1 <- apply( output_ob_baker_MicroMissNoise1_nu500sig1$alpha_t[ , , (floor(samples)/2 + 1):(floor(samples)) ], c(1,3), sum ) 
psi_MicroMissNoise1 <- output_ob_baker_MicroMissNoise1_nu500sig1$alpha_t[ , , (floor(samples)/2 + 1):(floor(samples)) ] 
for (i in 1:length((floor(samples)/2 + 1):(floor(samples))) ){
  psi_MicroMissNoise1[,,i] <- psi_MicroMissNoise1[,,i]/alpha_norm_MicroMissNoise1[,i]
}
post_psi_MicroMissNoise1 <- apply( psi_MicroMissNoise1 , c(1,2), mean )   
post_psi_MicroMissNoise1_upper <- apply( psi_MicroMissNoise1 , c(1,2), function( x ){ quantile( x , 0.975 ) } )  
post_psi_MicroMissNoise1_lower <- apply( psi_MicroMissNoise1 , c(1,2), function( x ){ quantile( x , 0.025 ) } )  

###### Run with more misclassification 
# Adjust Y using phylo tree
# Set prior accordingly
reclass_prob2 <- matrix( 1, ncol( Y ), ncol( Y ) )
for( i in 1:ncol(Y) ){
  for( j in 1:ncol(Y) ){
    if( FamilyGenus$family[ i ] == FamilyGenus$family[ j ] ){
      reclass_prob2[ i, j ] = 50
      reclass_prob2[ j, i ] = 50
    }
  }
}
diag( reclass_prob2 ) <- 1000 
reclass_prob2 <- reclass_prob2/rowSums( reclass_prob2 )
reclass_Y2 <- matrix( 0 , nrow(Y), ncol(Y) )

set.seed(1)
for( n in 1:nrow(Y)  ){
  for( t in 1:T. ){
    if( Y[ n, t ] > 0 ){
      x <- rmultinom( 1, Y[ n, t ], reclass_prob2[ t, ] )  
      reclass_Y2[ n, ] <-  reclass_Y2[ n, ] + x
    }
  }
}

# Initialize
Z[ , , 1 ] <- reclass_Y2
alpha_it[ , , 1 ] <- reclass_Y2
for( n in 1:nrow( reclass_Y2 ) ){
  sum_Z <- sum( reclass_Y2[ n, ] )
  mu_i[ n, 1 ] <- rgamma( 1, sum_Z, sum_Z );
}

ptm_MicroMissNoise2 <- proc.time() 
output_ob_baker_MicroMissNoise2_nu500sig1 <- MicroMiss( iterations, thin, N, T., C., P_eta, P_gamma, reclass_Y2, Z, Zsum, alpha_it, zeta_it, beta_eta, X_eta, 
                                                        omega_eta, beta_gamma, X_gamma, mu_i, a_tc, u_t, nu, mu_beta_eta_prior, mu_beta_gamma_prior, sigma2_beta_eta, sigma2_beta_gamma )
time_MicroMissNoise2 <- proc.time() - ptm_MicroMissNoise2

names( output_ob_baker_MicroMissNoise2_nu500sig1 ) <- c( "Z", "alpha_t", "zeta", "beta_eta", "omega_eta", "a_tc", "beta_gamma", "mu" )

# Save or load
#save( output_ob_baker_MicroMissNoise2_nu500sig1 , file = paste( path_save, "output_ob_baker_MicroMissNoise2_nu500sig1", sep = "" ) )
#load( file = paste( path_save, "output_ob_baker_MicroMissNoise2_nu500sig1.rda", sep = "" ) )

# Beta Eta MicroMissNoise2
post_beta_eta_MicroMissNoise2 <- apply( output_ob_baker_MicroMissNoise2_nu500sig1$beta_eta[ , , (floor(samples)/2 + 1):(floor(samples))  ] , c(1,2), mean )  
post_beta_eta_MicroMissNoise2_upper <- apply( output_ob_baker_MicroMissNoise2_nu500sig1$beta_eta[ , , (floor(samples)/2 + 1):(floor(samples)) ] , c(1,2), function( x ){ quantile( x , 0.975 ) } )  
post_beta_eta_MicroMissNoise2_lower <- apply( output_ob_baker_MicroMissNoise2_nu500sig1$beta_eta[ , , (floor(samples)/2 + 1):(floor(samples)) ] , c(1,2), function( x ){ quantile( x , 0.025 ) } ) 

sig_beta_eta_MicroMissNoise2 <- sign( post_beta_eta_MicroMissNoise2_lower[,2] ) == sign( post_beta_eta_MicroMissNoise2_upper[,2] )

# Beta Gamma MicroMissNoise2
post_beta_gamma_MicroMissNoise2 <- apply( output_ob_baker_MicroMissNoise2_nu500sig1$beta_gamma[ , , (floor(samples)/2 + 1):(floor(samples)) ] , c(1,2), mean )  
post_beta_gamma_MicroMissNoise2_upper <- apply( output_ob_baker_MicroMissNoise2_nu500sig1$beta_gamma[ , , (floor(samples)/2 + 1):(floor(samples)) ] , c(1,2), function( x ){ quantile( x , 0.975 ) } )  
post_beta_gamma_MicroMissNoise2_lower <- apply( output_ob_baker_MicroMissNoise2_nu500sig1$beta_gamma[ , , (floor(samples)/2 + 1):(floor(samples)) ] , c(1,2), function( x ){ quantile( x , 0.025 ) } ) 

sig_beta_gamma_MicroMissNoise2 <- sign( post_beta_gamma_MicroMissNoise2_lower[,2] ) == sign( post_beta_gamma_MicroMissNoise2_upper[,2] ) 

# Relative effect estimates  
MCMC_gamma_unit <- effects_all( output_ob_baker_MicroMissNoise2_nu500sig1$beta_gamma, X_gamma[c(3,3),], T., 2000,1 ) 

post_rel_MicroMissNoise2 <- apply( MCMC_gamma_unit[ , , (floor(samples)/2 + 1):(floor(samples)) ] , c(1,2), mean )  
post_rel_MicroMissNoise2_upper <- apply( MCMC_gamma_unit[ , , (floor(samples)/2 + 1):(floor(samples)) ] , c(1,2), function( x ){ quantile( x , 0.975 ) } )  
post_rel_MicroMissNoise2_lower <- apply( MCMC_gamma_unit[ , , (floor(samples)/2 + 1):(floor(samples)) ] , c(1,2), function( x ){ quantile( x , 0.025 ) } ) 

pos_MicroMissNoise2 <- post_rel_MicroMissNoise2_upper > 1 & post_rel_MicroMissNoise2_lower > 1  
neg_MicroMissNoise2 <- post_rel_MicroMissNoise2_upper < 1 & post_rel_MicroMissNoise2_lower < 1 

# Estimate relative abundances 
alpha_norm_MicroMissNoise2 <- apply( output_ob_baker_MicroMissNoise2_nu500sig1$alpha_t[ , , (floor(samples)/2 + 1):(floor(samples)) ], c(1,3), sum ) 
psi_MicroMissNoise2 <- output_ob_baker_MicroMissNoise2_nu500sig1$alpha_t[ , , (floor(samples)/2 + 1):(floor(samples)) ] 
for (i in 1:length((floor(samples)/2 + 1):(floor(samples))) ){
  psi_MicroMissNoise2[,,i] <- psi_MicroMissNoise2[,,i]/alpha_norm_MicroMissNoise2[,i]
}
post_psi_MicroMissNoise2 <- apply( psi_MicroMissNoise2 , c(1,2), mean )   
post_psi_MicroMissNoise2_upper <- apply( psi_MicroMissNoise2 , c(1,2), function( x ){ quantile( x , 0.975 ) } )  
post_psi_MicroMissNoise2_lower <- apply( psi_MicroMissNoise2 , c(1,2), function( x ){ quantile( x , 0.025 ) } )  


###### Run with even more misclassification 
# Adjust Y using phylo tree
# Set prior accordingly
reclass_prob3 <- matrix( 1, ncol( Y ), ncol( Y ) )
for( i in 1:ncol(Y) ){
  for( j in 1:ncol(Y) ){
    if( FamilyGenus$family[ i ] == FamilyGenus$family[ j ] ){
      reclass_prob3[i,j] = 50
      reclass_prob3[j,i] = 50
    }
  }
}
diag( reclass_prob3 ) <- 100 
reclass_prob3 <- reclass_prob3/rowSums( reclass_prob3 )
reclass_Y3 <- matrix( 0 , nrow(Y), ncol(Y) )

set.seed(1)
for( n in 1:nrow(Y)  ){
  for( t in 1:T. ){
    if( Y[ n, t ] > 0 ){
      x <- rmultinom( 1, Y[ n, t ], reclass_prob3[ t, ] )  
      reclass_Y3[ n, ] <-  reclass_Y3[ n, ] + x
    }
  }
}

# Initialize
Z[ , , 1 ] <- reclass_Y3
alpha_it[ , , 1 ] <- reclass_Y3
for( n in 1:nrow( reclass_Y3 ) ){
  sum_Z <- sum(  reclass_Y3[ n, ] )
  mu_i[ n, 1 ] <- rgamma( 1, sum_Z, sum_Z );
}

ptm_MicroMissNoise3 <- proc.time() 
output_ob_baker_MicroMissNoise3_nu500sig1 <- MicroMiss( iterations, thin, N, T., C., P_eta, P_gamma, reclass_Y3, Z, Zsum, alpha_it, zeta_it, beta_eta, X_eta, 
                                                        omega_eta, beta_gamma, X_gamma, mu_i, a_tc, u_t, nu, mu_beta_eta_prior, mu_beta_gamma_prior, sigma2_beta_eta, sigma2_beta_gamma )
time_MicroMissNoise3 <- proc.time() - ptm_MicroMissNoise3

names( output_ob_baker_MicroMissNoise3_nu500sig1 ) <- c( "Z", "alpha_t", "zeta", "beta_eta", "omega_eta", "a_tc", "beta_gamma", "mu" )

# Save or load
#save( output_ob_baker_MicroMissNoise3_nu500sig1 , file = paste( path_save, "output_ob_baker_MicroMissNoise3_nu500sig1", sep = "" ) )
#load( file = paste( path_save, "output_ob_baker_MicroMissNoise3_nu500sig1.rda", sep = "" ) )

# Beta Eta MicroMissNoise3
post_beta_eta_MicroMissNoise3 <- apply( output_ob_baker_MicroMissNoise3_nu500sig1$beta_eta[ , , (floor(samples)/2 + 1):(floor(samples))  ] , c(1,2), mean )  
post_beta_eta_MicroMissNoise3_upper <- apply( output_ob_baker_MicroMissNoise3_nu500sig1$beta_eta[ , , (floor(samples)/2 + 1):(floor(samples)) ] , c(1,2), function( x ){ quantile( x , 0.975 ) } )  
post_beta_eta_MicroMissNoise3_lower <- apply( output_ob_baker_MicroMissNoise3_nu500sig1$beta_eta[ , , (floor(samples)/2 + 1):(floor(samples)) ] , c(1,2), function( x ){ quantile( x , 0.025 ) } ) 

sig_beta_eta_MicroMissNoise3 <- sign( post_beta_eta_MicroMissNoise3_lower[,2] ) == sign( post_beta_eta_MicroMissNoise3_upper[,2] )

# Beta Gamma MicroMissNoise3
post_beta_gamma_MicroMissNoise3 <- apply( output_ob_baker_MicroMissNoise3_nu500sig1$beta_gamma[ , , (floor(samples)/2 + 1):(floor(samples)) ] , c(1,2), mean )  
post_beta_gamma_MicroMissNoise3_upper <- apply( output_ob_baker_MicroMissNoise3_nu500sig1$beta_gamma[ , , (floor(samples)/2 + 1):(floor(samples)) ] , c(1,2), function( x ){ quantile( x , 0.975 ) } )  
post_beta_gamma_MicroMissNoise3_lower <- apply( output_ob_baker_MicroMissNoise3_nu500sig1$beta_gamma[ , , (floor(samples)/2 + 1):(floor(samples)) ] , c(1,2), function( x ){ quantile( x , 0.025 ) } ) 

sig_beta_gamma_MicroMissNoise3 <- sign( post_beta_gamma_MicroMissNoise3_lower[,2] ) == sign( post_beta_gamma_MicroMissNoise3_upper[,2] ) 

# Relative effect estimates  
MCMC_gamma_unit <- effects_all( output_ob_baker_MicroMissNoise3_nu500sig1$beta_gamma, X_gamma[c(3,3),], T., 2000,1 ) 

post_rel_MicroMissNoise3 <- apply( MCMC_gamma_unit[ , , (floor(samples)/2 + 1):(floor(samples)) ] , c(1,2), mean )  
post_rel_MicroMissNoise3_upper <- apply( MCMC_gamma_unit[ , , (floor(samples)/2 + 1):(floor(samples)) ] , c(1,2), function( x ){ quantile( x , 0.975 ) } )  
post_rel_MicroMissNoise3_lower <- apply( MCMC_gamma_unit[ , , (floor(samples)/2 + 1):(floor(samples)) ] , c(1,2), function( x ){ quantile( x , 0.025 ) } ) 

pos_MicroMissNoise3 <- post_rel_MicroMissNoise3_upper > 1 & post_rel_MicroMissNoise3_lower > 1  
neg_MicroMissNoise3 <- post_rel_MicroMissNoise3_upper < 1 & post_rel_MicroMissNoise3_lower < 1 

# Estimate relative abundances 
alpha_norm_MicroMissNoise3 <- apply( output_ob_baker_MicroMissNoise3_nu500sig1$alpha_t[ , , (floor(samples)/2 + 1):(floor(samples)) ], c(1,3), sum ) 
psi_MicroMissNoise3 <- output_ob_baker_MicroMissNoise3_nu500sig1$alpha_t[ , , (floor(samples)/2 + 1):(floor(samples)) ] 
for (i in 1:length((floor(samples)/2 + 1):(floor(samples))) ){
  psi_MicroMissNoise3[,,i] <- psi_MicroMissNoise3[,,i]/alpha_norm_MicroMissNoise3[,i]
}
post_psi_MicroMissNoise3 <- apply( psi_MicroMissNoise3 , c(1,2), mean )   
post_psi_MicroMissNoise3_upper <- apply( psi_MicroMissNoise3 , c(1,2), function( x ){ quantile( x , 0.975 ) } )  
post_psi_MicroMissNoise3_lower <- apply( psi_MicroMissNoise3 , c(1,2), function( x ){ quantile( x , 0.025 ) } )  
 
# Create dataframe for pi plot ( Supplementary Figure S5 )
mean <- c( log( post_rel_MicroMiss[ 1, ] ), log( post_rel_MicroMissNoise1[ 1, ] ), log( post_rel_MicroMissNoise2[ 1, ] ),  log( post_rel_MicroMissNoise3[ 1, ] ) )
upper <- c(  log(  post_rel_MicroMiss_upper[ 1, ] ), log(  post_rel_MicroMissNoise1_upper[ 1, ] ), log(  post_rel_MicroMissNoise2_upper[ 1, ] ),  log(  post_rel_MicroMissNoise3_upper[ 1, ] ) )
lower <- c(  log(  post_rel_MicroMiss_lower[ 1, ] ), log(  post_rel_MicroMissNoise1_lower[ 1, ] ), log(  post_rel_MicroMissNoise2_lower[ 1, ] ), log(  post_rel_MicroMissNoise3_lower[ 1, ] ) )
label <- c(  ob_baker_otu_denovo_counts$genus,  ob_baker_otu_denovo_counts$genus,  ob_baker_otu_denovo_counts$genus,  ob_baker_otu_denovo_counts$genus )
Model <- c( rep( "Raw", length( post_beta_gamma_MicroMiss[ ,2 ] ) ), rep( "Small", length( post_beta_gamma_MicroMiss[ ,2 ] ) ), rep( "Medium", length( post_beta_gamma_MicroMiss[ ,2 ] ) ), rep( "Large", length( post_beta_gamma_MicroMiss[ ,2 ] ) ) )

data <- data.frame( mean, upper, lower, label, Model )  
data$Model <- as.factor( data$Model )
levels( data$Model ) <- c( "Raw", "Small", "Medium", "Large" )
 
sfig5 <- ggplot( data, aes(y=mean, x=label, col = Model)) + 
  scale_color_manual("Model",values=c( "#1A242F", "#A3A7AB",  "#970", "#A43")) +
  geom_point(position=position_dodge(0.8), size=0.5  ) +
  geom_errorbar(aes(ymin=upper, ymax=lower), position = position_dodge(0.8), width = 0.1, alpha = 0.75  ) +
  theme_bw(base_size=16) + 
  theme(text = element_text(size=8), axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1) )  + ylab( expression(paste(log(pi[t](x[obesity]))))) + xlab("Genus")

# Save
#ggsave( paste0( path_figures, "SuppFig5.png" ), plot = sfig5 ) 

# Create dataframe for Beta Eta plot ( Supplementary Figure S6 )
mean <- c( post_beta_eta_MicroMiss[ ,2 ] , post_beta_eta_MicroMissNoise1[ ,2 ], post_beta_eta_MicroMissNoise2[ ,2 ], post_beta_eta_MicroMissNoise3[ ,2 ] )
upper <- c( post_beta_eta_MicroMiss_upper[ ,2 ] , post_beta_eta_MicroMissNoise1_upper[ ,2 ] , post_beta_eta_MicroMissNoise2_upper[ ,2 ], post_beta_eta_MicroMissNoise3_upper[ ,2 ] )
lower <- c( post_beta_eta_MicroMiss_lower[ ,2 ] , post_beta_eta_MicroMissNoise1_lower[ ,2 ] , post_beta_eta_MicroMissNoise2_lower[ ,2 ], post_beta_eta_MicroMissNoise3_lower[ ,2 ])
label <- c( ob_baker_otu_denovo_counts$genus,  ob_baker_otu_denovo_counts$genus,  ob_baker_otu_denovo_counts$genus,  ob_baker_otu_denovo_counts$genus )
Model <- c( rep( "Raw", length( post_beta_gamma_MicroMiss[ ,2 ] ) ), rep( "Small", length( post_beta_gamma_MicroMiss[ ,2 ] ) ), rep( "Medium", length( post_beta_gamma_MicroMiss[ ,2 ] ) ), rep( "Large", length( post_beta_gamma_MicroMiss[ ,2 ] ) ) )

data <- data.frame( mean, upper, lower, label, Model )  
data$Model <- as.factor( data$Model )
levels( data$Model ) <- c( "Raw", "Small", "Medium", "Large" )

sfig6 <- ggplot( data, aes(y=mean, x=label, col = Model)) + 
  scale_color_manual("Model",values=c( "#1A242F", "#A3A7AB",  "#970", "#A43")) +
  geom_point(position=position_dodge(0.8), size=0.5  ) +
  geom_errorbar(aes(ymin=upper, ymax=lower), position = position_dodge(0.8), width = 0.1, alpha = 0.75  ) +
  theme_bw(base_size=16) + 
  theme(text = element_text(size=8), axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1) ) + ylab( expression(paste(beta[eta[t]][obesity] ))) + xlab("Genus")  

# Save
# ggsave( paste0( path_figures, "SuppFig6.png" ), plot = sfig6 ) 

####################################################################
# Run MicroMiss-bvs
set.seed(1)

# Set priors back to original analysis
nu <- matrix( 1, nrow = T., ncol = C. )
diag( nu ) <- 500
mu_beta_eta_prior <- matrix( 0, T., P_eta )
mu_beta_gamma_prior <- matrix( 0, T., P_gamma )
sigma2_beta_eta <- 1
sigma2_beta_gamma <- 1


# Prep for selection models
zeta_eta <- array( 0, dim = c( T., P_eta, samples ) )
zeta_eta[,1,] <- 1
zeta_gamma <- array( 0, dim = c( T., P_gamma, samples ) )
zeta_gamma[,1,] <- 1

a_zeta_gamma <- 1
a_zeta_eta <- 1
b_zeta_gamma <- 1
b_zeta_eta <- 1

ptm_MicroMissBVS <- proc.time() 
output_MMBVS <- MicroMissBVS( iterations, thin, N, T., C., P_eta, P_gamma, Y, Z, Zsum, alpha_it, zeta_it, beta_eta, zeta_eta, X_eta, 
                              omega_eta, beta_gamma, zeta_gamma, X_gamma, mu_i, a_tc, u_t, nu, mu_beta_eta_prior, mu_beta_gamma_prior, sigma2_beta_eta, sigma2_beta_gamma, a_zeta_gamma,  b_zeta_gamma, a_zeta_eta, b_zeta_eta  )
time_MicroMissBVS <- proc.time() - ptm_MicroMissBVS

names( output_MMBVS ) <- c( "Z", "alpha_t", "zeta", "beta_eta", "omega_eta", "a_tc", "beta_gamma", "mu", "zeta_eta", "zeta_gamma" )

# Save or load
#save( output_MMBVS , file = paste( path_save, "output_ob_baker_MMBVS_nu500sig1.rda", sep = "" ) )
#load( file = paste( path_save, "output_ob_baker_MMBVS_nu500sig1.rda", sep = "" ) )

# Selection 
MPPI_MMBVS_zeta_gamma <- apply( output_MMBVS$zeta_gamma [ ,, (floor(samples)/2 + 1):(floor(samples)) ], c(1,2), mean )
MPPI_MMBVS_zeta_eta <- apply( output_MMBVS$zeta_eta[ ,, (floor(samples)/2 + 1):(floor(samples)) ], c(1,2), mean )

# Run ZINB-bvs model 

library( IntegrativeBayes )
zvec <- c( rep( 1, ceiling( N/2 ) ), rep( 2, floor( N/2 ) ) )

Y_input <- Y.filter( Y, zvec = zvec, min.number = 0)[[2]] 

# estimate the size factor s from the count matrix Y:
s_input <- sizefactor.estimator( Y ) 

feature_remain <- which( Y.filter( Y, zvec = zvec, min.number = 0)[[1]] == 1)

iterations_IB <- 10000
burn.in <- 0.5
mu0.start <- 10

ptm_IB <- proc.time()
result_IB <- zinb_w_cov(Y_mat = Y,
                        z_vec = zvec, 
                        s_vec = s_input,
                        X_mat =  as.matrix( X_gamma[,-1], ncol = 1) ,
                        S = iterations_IB, burn_rate = burn.in,
                        mu0_mean = mu0.start, a_beta = 1, b_beta = 999, beta_lim = 10, tau_beta = sigma2_beta_gamma )
time_IB <- proc.time() - ptm_IB
selected_IB <- result_IB$delta_PPI > 0.5
ob_baker_otu_denovo_counts$genus[ selected_IB ]
   

