R/clust_sim_SI.R

Defines functions clust_sim_SI

Documented in clust_sim_SI

#' clust_sim_SI
#'
#' clust_sim_SI simulates the spread of an infectious disease on a social
#' network using the Susceptible-Infected compartmental model in parallel
#' for a given number of iterations.
#'
#' @param network_el an edgelist file generated by igraph
#' @param beta a value from 0 to 1 indicating per contract probability of
#'   pathogen transmission
#' @param intxn_per_day the per capita interaction rate
#' @param days the maximum number of days for which your simulation will run
#' @param iters the number of times to replicate simulations
#' @param free_threads the number of computing threads to be left unused by
#'   simulations (e.g. for doing other tasks while simulations are running.)
#'
#' @return a matrix with the same number of columns as argument 'iters', where
#'   each column represents an iteration, and each row represents 1: number of
#'   days simulation went for, 2: final number of susceptible individuals,
#'   3: final number of infected individuals, in that order.
#' @export
#'
#' @examples
clust_sim_SI <- function(network_el, beta, intxn_per_day, days, iters, free_threads = 1) {

  if (.Platform$OS.type == "unix") {
    cl.type <- "FORK"
  } else {
    cl.type <- "PSOCK"
  }

  cl <- parallel::makeCluster((parallel::detectCores() - free_threads), type = cl.type)

  parallel::clusterExport(cl = cl, varlist = c("network_el", "sim_SI", "beta", "intxn_per_day", "days", "iters"), envir = environment())

  sim_SI.out <- pbapply::pblapply(c(1:length(network_el)), function(x,b,i,d,it) { print(replicate(it, (sim_SI(network_el[[x]],b,i,d)))) }, b = beta, i = intxn_per_day, d = days, it = iters, cl = cl)

  parallel::stopCluster(cl); return(sim_SI.out)

}
collinmmccabe/enss documentation built on May 5, 2024, 6:23 a.m.