# Simulate multivariate compositional data with ZI and misclassification 
simulate_MicroMiss <- function( N = 500, T. = 250, C. = 250, P_eta = 2, P_gamma = 2, total = 10000, at_risk_prob = 0.5,
                                beta_effects = c(1,-1), theta0 = 0.01, missclass = c( 0.75, 0.85, 0.95 ),
                                missclass_prob = c( 0.20, 0.20, 0.60), active_cov = 25 ){
library(mvtnorm)
  
# Set beta_eta_it
logit_at_risk_prob <- log( at_risk_prob ) - log( 1 - at_risk_prob ) 
beta_eta_true <- array( logit_at_risk_prob, dim = c( T., P_eta )) 
for( t in 1:(active_cov) ){  
  beta_eta_true[ t, -1 ] <- sample( c( beta_effects ), ( P_eta - 1 ), replace = T )
} 

# Set zero-inflation indicators 
zeta_true <- matrix( 0, N, T. )  
prob_occ <- matrix( 0, N, T. )  
X_eta <- as.matrix( cbind( 1, rmvnorm(N, rep(0,P_eta - 1), diag(P_eta - 1)) ), nrow = N, ncol = P_eta )  
X_gamma <- as.matrix( cbind( 1, rmvnorm(N, rep(0,P_gamma - 1), diag(P_gamma - 1)) ), nrow = N, ncol = P_gamma )  

# Simulate zetas
for( i in 1:N ){
  for( t in 1:T. ){
    thisrow <- exp( X_eta[ i, ]%*%beta_eta_true[ t, ] )
    prob_occ[ i, t ] <- thisrow/( 1 + thisrow )
    zeta_true[ i, t ] <-  rbinom( 1, 1, prob_occ[ i, t ] ) 
  }
  if( sum( zeta_true[ i, ] ) == 0 ){ zeta_true[ i,  sample( 1:ncol( zeta_true ), 1) ] <- 1 }
} 

# Get true classification 
# Add overdispersion 
# Hold probabilities 
true_prob <- matrix( 0, N, T. )

# Hold latent counts  
true_Z <- matrix( 0, N, T. ) 

beta_gamma_true <- array( 0, dim = c( T., P_gamma )) 
for( t in 1:(active_cov) ){ # Only half are active
  beta_gamma_true[ t, -1 ] <- sample( c( beta_effects ), ( P_gamma - 1 ), replace = T )
} 


for( i in 1:N ){
  thisrow2 <- exp( X_gamma[ i, ]%*%t( beta_gamma_true ) )*zeta_true[ i, ]
  true_prob[ i, ] <-  thisrow2/sum( thisrow2 )
  true_Z[ i, ] <- dirmult::simPop( J = 1, n = total, pi = true_prob[ i, ], theta = theta0 )$data[ 1, ]
} 

# Get missclassification probability and indicator

# Generate observed classifications
# Get observation probability given true class
observe_prob <- matrix( 1 , T., C., byrow = T ) # matrix( rep( seq( 1:C.), T. ) , T., C., byrow = T ) 
missclass <- missclass*T./( 1 - missclass )
diag( observe_prob ) <- sample( missclass, T., prob = missclass_prob, replace = T ) 

classifications <- observe_prob/rowSums(observe_prob)
# diag( classifications )

# Hold observations
Y <- matrix( 0, N, C. )

for( i in 1:N ){
  for( t in 1:T. ){
    class_prob <- observe_prob[ t, ]/sum( observe_prob[ t, ] )
    Y[ i, ] <- Y[ i, ] + dirmult::simPop( J = 1, n = true_Z[ i, t ], pi = class_prob, theta = theta0 )$data[ 1, ]
  }
} 

data = list( Y = Y, true_Z = true_Z, classifications = classifications, true_prob = true_prob, beta_eta_true = beta_eta_true, beta_gamma_true = beta_gamma_true,
             N = N, T. = T., C. = C., X_eta = X_eta, X_gamma = X_gamma, P_eta = P_eta, P_gamma = P_gamma  )
return( data )
}
 