model_parallel {SPARSEMODr}R Documentation

Parallelized implementation of the SPARSE-MOD models

Description

The function uses R-level parallelization to speed up the generation of stochastic realizations of the SPARSEMODr models and to combine output data into a read-to-use data frame. This is the preferred method to run model_interface.

Usage

  model_parallel(..., input_realz_seeds = 1:2, control)

Arguments

...

Universal model arguments passed to model_interface.

input_realz_seeds

An integer vector of user-specified random seeds to generate the stochastic realizations of the model. The number of realizations will be equal to the length of this vector.

control

Either a covid19_control or a seir_control named list data object.

Details

Relies on future_lapply to run stochastic realizations of the SPARSEMODr model in parallel.

Value

A data frame that combines the two named lists of model_interface.

Author(s)

Joseph Mihaljevic, joseph.mihaljevic@nau.edu; Toby Hocking, toby.hocking@r-project.org

See Also

future_lapply, model_interface, time_windows, covid19_control, seir_control

Examples

## See vignettes for more detailed work-ups.


##########################################
# Required for run:
require(lubridate)

## Using supplied example data:

# Read in the example data:
ex_dir <- system.file(
  "extdata", "sparsemodr_example.Rdata", package="SPARSEMODr", mustWork=TRUE)
load(ex_dir)
n_pop <- length(dat_list[["pop_N"]])

# Set up realizations:
realz_seeds <- 1:2
n_realz <- length(realz_seeds)

# START FUTURE PLAN FOR PARALLELIZATION
future::plan("multisession")

# Set up time windows (see time_windows for other ways to do this)
input_beta <-           c(   0.3,   0.3,  0.08,  0.08,  0.15)
input_dist_phi <-     c(   200,   200,    20,   150,   150)
input_m <-              c( 0.002, 0.002, 0.002,  0.02,  0.02)
input_imm_frac <-       c(   0.0,   0.0,   0.0,  0.02,  0.02)
# Window intervals
start_dates = c(mdy("1-1-20"),  mdy("2-1-20"),  mdy("2-16-20"), mdy("3-11-20"),  mdy("3-22-20"))
end_dates =   c(mdy("1-31-20"), mdy("2-15-20"), mdy("3-10-20"), mdy("3-21-20"), mdy("5-1-20"))


# User creates the time_windows object here
tw <- time_windows(beta = input_beta,
                   dist_phi = input_dist_phi,
                   m = input_m,
                   imm_frac = input_imm_frac,
                   start_dates = start_dates,
                   end_dates = end_dates)

# Randomly generate initial conditions for
# EXPOSED class:
E_pops <- vector("numeric", length = n_pop)
n_initial_E <- 40
# (more exposed in larger populations)
these_E <- sample.int(n_pop,
                     size = n_initial_E,
                     replace = TRUE,
                     prob = dat_list$pop_N)
for(i in 1:n_initial_E){
  E_pops[these_E[i]] <- E_pops[these_E[i]] + 1
}

# Population sizes
N_pops <- as.integer(dat_list[["pop_N"]])

# Set up a function to use the dat_list

get_result <- function(input_realz_seeds, control = NULL){

    with(dat_list, SPARSEMODr::model_parallel(
        input_census_area = census_area,
        input_dist_mat = dist_vec,
        input_realz_seeds = input_realz_seeds,
        input_tw = tw,
        control = control)
    )
}

# User creates control list of parameters
covid19_control <- covid19_control(input_N_pops = N_pops,
                                   input_E_pops = E_pops)

covid_model_output <-
    get_result(
        input_realz_seeds = realz_seeds,
        control = covid19_control
    )

# Shut down parallel workers
future::plan("sequential")

[Package SPARSEMODr version 1.2.0 Index]