R/execute.R

Defines functions execute_experiment

Documented in execute_experiment

#' Execution of a Gaussian graphical model experiment
#'
#' @name execute-ggm-experiment
#'
#' @rdname execute_ggm
#'
#' @param p Vector of dimensions to test.
#' @param d Vector of densities to test.
#' @param r Number of replications for the experiment.
#' @param N Vector of number of samples to generate by the experiment.
#' @param ename Experiment name, used for storing the results.
#' @param emethod Function containing the atomic experiment to be executed.
#' As such, it must require at least two arguments, `p` and `d`.
#' @param ... Additional parameters for `emethod`.
#'
#' @details Function [execute_experiment()] executes `r` times the experiment 
#' `emethod` for each value of `p` and `d`, and stores its result as an `rds`
#' object in a newly created directory for each repetition. It runs in parallel 
#' the `r` replications of the experiment, up to `parallel::detectCores() - 2`
#' threads.
#'
#' @return Nothing
#' 
#' @export
execute_experiment <- function(p, d, r, N, ename, emethod, ...) {

	n_cores <- min(r, parallel::detectCores() - 2)
	cl <- parallel::makeCluster(n_cores, outfile = "")
	doParallel::registerDoParallel(cl)
	
	repetition <- 0 # For avoiding R CMD check NOTE
	iter <- foreach::foreach(repetition = seq(r), .combine = rbind)
	foreach::"%dopar%"(iter, {
		dname <- paste0(ename, "_r", repetition)
		dir.create(dname, showWarnings = FALSE)
  	
		for (i in seq_along(p)) {
			for (j in seq_along(d)) {
				result <- emethod(p = p[i], d = d[j], N = N[i], ...)
				saveRDS(result, file = paste0(dname, "/", p[i], "_", d[j], ".rds")) 
			}
		}
	})
	
	parallel::stopCluster(cl)
}
irenecrsn/ggmexp documentation built on Feb. 1, 2020, 4:20 a.m.