1: source("GLocalVI.R") # Load the GLocal DP VI function
2: # Load the necessary library for parallel execution
3: if (!require(parallel)) install.packages("parallel", dependencies = TRUE); suppressPackageStartupMessages(library(parallel))
4: # Define the number of cores to use
5: numberOfCores <- detectCores() - 1  # Use one less than the total cores
6: # Run the function in parallel
7: numReplicate = 20
8: 
9: out <- mclapply(1:numReplicate, function(i) {
10:   GLocalVI(X.local = X.local, X.global = X.global,
11:            H = 30,
12:            L = 30,
13:            maxIter = 500,
14:            epsilon = 1e-5)}
15:   , mc.cores = numberOfCores)
16: 
17: 
18: ELBO = 0
19: Time = 0
20: for(i in 1:length(out)){
21:   ELBO[i] = out[[i]]$ELBO_val_conv
22:   Time[i] = out[[i]]$Time.taken
23: }
24: 
25: best_outputs = out[[which.max(ELBO)]]
26: 
27: est.k_jt = list()
28: for(j in 1:J){
29:   est.k_jt[[j]] = apply(best_outputs$RHO_jtk[[j]], 1, which.max)
30: }
31: 
32: est.t_ji = list()
33: for(j in 1:J){
34:   est.t_ji[[j]] = apply(best_outputs$XI_jil[[j]], 1, which.max)
35: }
