##### Instructions #############################################################

# Source this file in order to reproduce the value given in the Discussion about
# the comparison of operating characteristics return via MVN integration and via
# simulation.

#devtools::install_github("mjg211/multiarm")
library(multiarm)

##### Normally distributed outcomes: des_ma() and sim_ma() #####################

an_results         <- sim_results <- list()
diffs              <- numeric(1000)
message("Beginning computations...")
for (i in 1:1000) {
  set.seed(i)
  K_i              <- sample(2:4, 1)
  des_i            <- des_ma(K          = K_i,
                             alpha      = runif(1, 0, 0.1),
                             beta       = runif(1, 0, 0.2),
                             delta1     = runif(1, 1, 2),
                             delta0     = runif(1),
                             sigma      = rep(runif(1, max = 5), K_i + 1)*
                                            seq(1, sample(1:2, 1),
                                           length.out = K_i + 1),
                             ratio      = sample(c("A", "D", "E"), 1),
                             correction = sample(c("benjamini_hochberg",
                                                   "benjamini_yekutieli",
                                                   "bonferroni", "dunnett",
                                                   "hochberg",
                                                   "holm_bonferroni",
                                                   "holm_sidak", "none",
                                                   "sidak"), 1),
                             power      = sample(c("conjunctive", "disjunctive",
                                                   "marginal"), 1),
                             integer    = T)
  an_results[[i]]  <- des_i$opchar
  sim_results[[i]] <- sim_ma(des_i)$sim
  diffs[i]         <- max(abs(sim_results[[i]] - an_results[[i]]))
  if (i < 1000) {
    message("...", round(i/10, 3), "% complete...")
  } else {
    message("...100% complete.")
  }
  if (diffs[i] > 0.01) {
    message("WARNING: Maximal absolute difference greater than 0.01 for ",
            "scenario ", i)
  }
}
max(diffs)
