Code Listing L4.
R code synopsis of parallel execution of model replications for all latent class models.
1 replications <- 1:1000 2 load(“lcaMasterList.RData”) #contains lcalist 3 4 library(doSNOW); library(foreach) 5 njobs <- 50; clusterobj <- makeSOCKcluster(njobs); registerDoSNOW(clusterobj) 6 7 fmm <- foreach(m=iter(lcalist), .inorder=FALSE, .multicombine=TRUE, 8 .packages=c(“MplusAutomation”)) %dopar% { 9 10 repResults <- list() 11 for (i in 1:min(length(fmm_sim), max(replications))) { 12 #write syntax for this replication to a temporary directory 13 cat(getSyntax(m$fc, m$nc, m$nv, m$ss, m$sep, m$np, dataSection=dataSection, 14 propMeans=propMeans, means=means, output=outputSection, 15 savedata=savedata), file=file.path(tempdir(), “individual_rep.inp”)) 16 17 runModels(tempdir(), recursive=FALSE, logFile=NULL) 18 repResults[[i]] <- readModels(file.path(tempdir(), “individual_rep.out”)) 19 } 20 21 cleanupTempFiles(m) #remove various files used by Mplus 22 return(repResults) 23 } 24 25 stopCluster(clusterobj) |
Note. Full R code for all steps in the pipeline is located in the MplusAutomation Github repository: https://github.com/michaelhallquist/MplusAutomation/tree/master/examples/monte_carlo.