drizopoulos / GLMMadaptive

GLMMs with adaptive Gaussian quadrature
https://drizopoulos.github.io/GLMMadaptive/
59 stars 14 forks source link

marginal_coefs.MixMod Let user toggle off/on parallel eg use ifelse parLapply or vanilla base::lapply #24

Closed statsccpr closed 4 years ago

statsccpr commented 4 years ago

Hi this package has been helpful to prototype ideas. Thank you for your work.

In general your parallel implementation using parLapply() to compute the standard errors of marginalized coefs is handy.

But there are situations where the user would want to run this part in serial.

https://github.com/drizopoulos/GLMMadaptive/blob/6e4ed447ad6a203deaa99ad4163f139a4ab9eb3b/R/methods.R#L624

Can you add an argument 'lgl_par' in the marginal_coefs.MixMod function scope that would toggle this portion of the code to something like the below


if(lgl_par==TRUE){
# or 
# if(cores!=1){
  cl <- parallel::makeCluster(cores)
  res <- parallel::parLapply(cl, blocks, cluster_compute_marg_coefs, tht = tht,
                             list_thetas = list_thetas, V = V, XX = X, Z = Z, 
                             X_zi = X_zi, Z_zi = Z_zi, M = M,
                             object = object, compute_marg_coefs = compute_marg_coefs,
                             chol_transf = chol_transf, link_fun = link_fun, seed = seed)
  parallel::stopCluster(cl)
} else {
  res <- base::lapply(#cl,
    blocks, cluster_compute_marg_coefs, tht = tht,
    list_thetas = list_thetas, V = V, XX = X, Z = Z, 
    X_zi = X_zi, Z_zi = Z_zi, M = M,
    object = object, compute_marg_coefs = compute_marg_coefs,
    chol_transf = chol_transf, link_fun = link_fun, seed = seed)
}
out$var_betas <- var(do.call("rbind", res))

Currently, without the ifelse parLapply or lapply, When I set marginal_coefs(cores=1,...) your function is still internally making a cluster and using parLapply. This is interfereing (opening unnecessary cores) if marginal_coefs() is part of a larger for loop, where the outter loop is parallelized at the top level

# outer parallel simulations where s iterations are parallelized
for s in 1:5000{
...
marginal_coefs(cores=1,...)
...
}
drizopoulos commented 4 years ago

The new version resolves this.

statsccpr commented 4 years ago

Thanks @drizopoulos this upgrade has sped up our simulation experiments a lot