Skip to contents

The run_multiple function allows running multiple simulations at once. When available, users can take advantage of parallel computing to speed up the process.

Usage

run_multiple(
  m,
  ndays,
  nsims,
  seed = sample.int(10000, 1),
  saver = make_saver(),
  reset = TRUE,
  verbose = TRUE,
  nthreads = 1L
)

run_multiple_get_results(
  m,
  nthreads = min(2L, parallel::detectCores()),
  freader = NULL,
  ...
)

make_saver(..., fn = "")

Arguments

m, ndays, seed

See run.

nsims

Integer. Number of replicats

saver

An object of class epiworld_saver.

reset

When TRUE (default,) resets the simulation.

verbose

When TRUE (default,) prints a progress bar.

nthreads

Integer. Number of threads (passed to parallel::makeCluster()).

freader

A function to read the files. If NULL (default,) uses utils::read.table.

...

Additional arguments passed to freader.

fn

A file name pattern.

Value

  • In the case of make_saver, an list of class epiworld_saver.

  • The run_multiple function runs a specified number of simulations and returns a model object of class epiworld_model.

  • The run_multiple_get_results function returns a named list with the data specified by make_saver.

Details

Currently, the following elements can be saved:

  • total_hist History of the model (total numbers per time).

  • virus_info Information about viruses.

  • virus_hist Changes in viruses.

  • tool_info Information about tools.

  • tool_hist Changes in tools.

  • transmission Transmission events.

  • transition Transition matrices.

  • reproductive Reproductive number.

  • generation Estimation of generation time.

An alternative to using the default utils::read.table function is to use data.table::fread from the data.table package. This can be done by specifying freader = data.table::fread and passing additional arguments (e.g., nThread = 2L) via .... This can significantly speed up the reading process, especially for large datasets.

If the model does not have, for example, tools, then the corresponding data frame will be empty (0 rows). A warning will be issued in this case when trying to retrieve or plot the results.

Examples

model_sir <- ModelSIRCONN(
  name = "COVID-19",
  prevalence = 0.01,
  n = 1000,
  contact_rate = 2,
  transmission_rate = 0.9, recovery_rate = 0.1
)

# Generating a saver
saver <- make_saver("total_hist", "reproductive")

# Running and printing
run_multiple(model_sir, ndays = 100, nsims = 50, saver = saver, nthreads = 2)
#> Starting multiple runs (50) using 2 thread(s)
#> _________________________________________________________________________
#> _________________________________________________________________________
#> ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| done.

# Retrieving the results
ans <- run_multiple_get_results(model_sir, nthreads = 2)

head(ans$total_hist)
#>   sim_num date nviruses       state counts
#> 1       1    0        1 Susceptible    990
#> 2       1    0        1    Infected     10
#> 3       1    0        1   Recovered      0
#> 4       1    1        1 Susceptible    970
#> 5       1    1        1    Infected     28
#> 6       1    1        1   Recovered      2
head(ans$reproductive)
#>   sim_num virus_id    virus source source_exposure_date rt
#> 1       1        0 COVID-19    415                   10  0
#> 2       1        0 COVID-19    132                   10  0
#> 3       1        0 COVID-19    953                    9  0
#> 4       1        0 COVID-19    313                    9  0
#> 5       1        0 COVID-19    920                    8  0
#> 6       1        0 COVID-19    694                    8  0

# Plotting
multi_sir <- ans$total_hist
multi_sir <- multi_sir[multi_sir$date <= 20, ]
plot(multi_sir)