R/simulateParallel.R

Defines functions simulateParallel

library(foreach)
library(doParallel)
library(mirt)
library(TestGardener)
# dataGenerationModel is a WfdList or mirt object
# R is iteration, theta is true scores with length of sample size
# dataGenerationModelType can be optimal, graded och gpcm
# modelsToCompare is a vector with combinations of optimal and/or (graded or gpcm)
# thetas is a (sequence) vector of index values to fit models in each iteration
# optScr is list of score options, required when fitting optimal scores models
simulateParallel <- function(R, dataGenerationModel, thetas,
                     dataGenerationModelType="optimal",
                     modelsToCompare=list(c("gpcm", "EAP"), c("gpcm", "ML"), c("optimal", cycles=15)),
                     optScr=NULL,
                     usePrecalibrated=F,
                     preCalibratedOptimal=NULL,
                     preCalibratedParMod=NULL) {
    n <- length(thetas)
    # Matrices for storing result, R times no of methods rows and no of thetas columns+1

    # Kör R replications
    cl <- makeCluster(detectCores(logical = F))
    registerDoParallel(cl)
    res <- foreach(i = 1:R,
                   .packages = c('TestGardener', 'mirt'),
                   .export = "simulationIteration",
                   .combine = "rbind", .verbose = T) %dopar% {
        resMat <- simulationIteration(r, n, dataGenerationModel, thetas,
                                     dataGenerationModelType,
                                     modelsToCompare,
                                     optScr,
                                     usePrecalibrated,
                                     preCalibratedOptimal,
                                     preCalibratedParMod)
    }
    stopCluster(cl)

    return(list(truethetas = thetas, simRes = res))
}
joakimwallmark/PolyOptimalIRT documentation built on Dec. 21, 2021, 1:16 a.m.