Skip to content

Commit ba03bbd

Browse files
committed
replicate multiple and iteration simulations
1 parent 949ba5a commit ba03bbd

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

episodes/superspreading-simulate.Rmd

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -487,16 +487,16 @@ The output data frame collects **infectees** as the observation unit:
487487

488488
## Iterate simulations
489489

490-
As before, we can configure the simulation of multiple chains by simply increasing the number of chains (e.g., from `n_chains = 1` to `n_chains = 1000`).
490+
As before, we can configure the simulation of multiple chains by simply increasing the number of chains (e.g., from `n_chains = 1` to `n_chains = 100`).
491491
However, if we need to assume that each initial case starts (being infectious) at a different time, this can only be configured in one simulation function.
492492
Thus, we need to **iterate** multiple times over one specific chain simulation configuration to increase the probability of simulating uncontrolled outbreak projections.
493493
The following table compares the alternatives:
494494

495495
| Simulation runs | Initial cases | Start time (`t0`) | Use |
496496
|---|---|---|---|
497497
| One | 1 | Same | `epichains::simulate_chains()` with `n_chains = 1` |
498-
| Multiple (1000, e.g.) | 1 | Same | `epichains::simulate_chains()` with `n_chains = 1000` |
499-
| Multiple (1000, e.g.) | More than one | Different | Iterate 1000 times using `purrr::map()` over `epichains::simulate()` |
498+
| Multiple (100, e.g.) | 1 | Same | `epichains::simulate_chains()` with `n_chains = 100` |
499+
| Multiple (100, e.g.) | More than one | Different | Iterate 100 times using `purrr::map()` over `epichains::simulate()` |
500500

501501
The key difference of the third configuration is the `t0` argument from `epichains::simulate_chains()`.
502502
The argument `t0` defines the start time of each initial case per chain.
@@ -523,7 +523,9 @@ To increase the probability of simulating uncontrolled outbreak projections, thi
523523

524524
::::::::::::
525525

526-
In this section we'll conviniently replicate the same simulation of one chain with the same starting time (`t0 = 0`), **but with 1000 replicates**, to showcase how to build up the iteration over `{epichains}` step by step.
526+
In this section we'll showcase how to build up the **iteration** over `{epichains}` step by step.
527+
We'll conviniently replicate the same simulation as before: 100 transmission chains with 1 initial case each starting at day 0 (`t0 = 0`).
528+
But, instead of using `n_chains = 100`, we'll iterate 100 times over the simulation of 1 transmission chain with 1 initial case each starting at day 0 (`n_chains = 1`).
527529

528530
We need to specify two additional elements:
529531

@@ -532,7 +534,7 @@ We need to specify two additional elements:
532534

533535
```{r}
534536
# Number of simulation runs
535-
number_simulations <- 1000
537+
number_simulations <- 100
536538
# Number of initial cases
537539
initial_cases <- 1
538540
```
@@ -566,14 +568,16 @@ The code chunk below
566568

567569
```r
568570
# steps:
569-
# - seq_len() creates a vector with sequence of numbers (simulation IDs from 1 to 1000) and
571+
# - purrr::map() will run 100 times function(sim).
572+
# - seq_len() creates a vector with sequence of numbers (simulation IDs from 1 to 100) and
570573
# - function(sim) iterates {epichains} to each simulation ID number, then
571574
# - dplyr::mutate() adds a column to the <epichains> output with the simulation ID number.
572575
# - purrr::list_rbind() combines all the list class outputs (for each simulation ID) into a single data frame.
573576
purrr::map(
574577
.x = seq_len(number_simulations),
575578
.f = function(sim) {
576-
epichains::simulate_chains(...) %>% dplyr::mutate(simulation_id = sim)
579+
epichains::simulate_chains(...) %>% # <-- {epichains}
580+
dplyr::mutate(simulation_id = sim)
577581
}
578582
) %>%
579583
purrr::list_rbind()
@@ -591,11 +595,8 @@ Now, we are prepared to use `purrr::map()` to repeatedly simulate from `simulate
591595
```{r}
592596
set.seed(33)
593597
simulated_chains_map <-
594-
# iterate one function across multiple numbers (simulation IDs)
595598
purrr::map(
596-
# vector of numbers (simulation IDs)
597599
.x = seq_len(number_simulations),
598-
# function to iterate to each simulation ID number
599600
.f = function(sim) {
600601
epichains::simulate_chains(
601602
n_chains = initial_cases,
@@ -605,14 +606,20 @@ simulated_chains_map <-
605606
size = mers_offspring["dispersion"],
606607
generation_time = function(x) generate(x = serial_interval, times = x)
607608
) %>%
608-
# creates a column with the simulation ID number
609609
dplyr::mutate(simulation_id = sim)
610610
}
611611
) %>%
612-
# combine list outputs (for each simulation ID) into a single data frame
613612
purrr::list_rbind()
614613
```
615614

615+
One limitation with the iteration output is that, in order to summarize the output, we need can not use the `summary(<epichains>)`.
616+
617+
```{r}
618+
simulated_chains_map %>%
619+
dplyr::count(simulation_id) %>%
620+
dplyr::pull(n)
621+
```
622+
616623
```{r,echo=FALSE,eval=FALSE}
617624
# view infectee number per simulation
618625
simulated_chains_map %>%
@@ -623,7 +630,9 @@ simulated_chains_map %>%
623630

624631
## Visualize multiple chains
625632

626-
We will use a multiple simulation **without** iteration for this section:
633+
To increase the probability of simulating uncontrolled outbreak projections given an overdispersed offspring distribution, let's simulate **1000 transmission chains** with 1 initial case each starting at day 0.
634+
635+
We will create a multiple simulation **without** iteration for this section:
627636

628637
```{r}
629638
set.seed(33)

0 commit comments

Comments
 (0)