You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: episodes/superspreading-simulate.Rmd
+22-13Lines changed: 22 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -487,16 +487,16 @@ The output data frame collects **infectees** as the observation unit:
487
487
488
488
## Iterate simulations
489
489
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`).
491
491
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.
492
492
Thus, we need to **iterate** multiple times over one specific chain simulation configuration to increase the probability of simulating uncontrolled outbreak projections.
493
493
The following table compares the alternatives:
494
494
495
495
| Simulation runs | Initial cases | Start time (`t0`) | Use |
496
496
|---|---|---|---|
497
497
| 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()`|
500
500
501
501
The key difference of the third configuration is the `t0` argument from `epichains::simulate_chains()`.
502
502
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
523
523
524
524
::::::::::::
525
525
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`).
527
529
528
530
We need to specify two additional elements:
529
531
@@ -532,7 +534,7 @@ We need to specify two additional elements:
532
534
533
535
```{r}
534
536
# Number of simulation runs
535
-
number_simulations <- 1000
537
+
number_simulations <- 100
536
538
# Number of initial cases
537
539
initial_cases <- 1
538
540
```
@@ -566,14 +568,16 @@ The code chunk below
566
568
567
569
```r
568
570
# 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
570
573
# - function(sim) iterates {epichains} to each simulation ID number, then
571
574
# - dplyr::mutate() adds a column to the <epichains> output with the simulation ID number.
572
575
# - purrr::list_rbind() combines all the list class outputs (for each simulation ID) into a single data frame.
@@ -591,11 +595,8 @@ Now, we are prepared to use `purrr::map()` to repeatedly simulate from `simulate
591
595
```{r}
592
596
set.seed(33)
593
597
simulated_chains_map <-
594
-
# iterate one function across multiple numbers (simulation IDs)
595
598
purrr::map(
596
-
# vector of numbers (simulation IDs)
597
599
.x = seq_len(number_simulations),
598
-
# function to iterate to each simulation ID number
599
600
.f = function(sim) {
600
601
epichains::simulate_chains(
601
602
n_chains = initial_cases,
@@ -605,14 +606,20 @@ simulated_chains_map <-
605
606
size = mers_offspring["dispersion"],
606
607
generation_time = function(x) generate(x = serial_interval, times = x)
607
608
) %>%
608
-
# creates a column with the simulation ID number
609
609
dplyr::mutate(simulation_id = sim)
610
610
}
611
611
) %>%
612
-
# combine list outputs (for each simulation ID) into a single data frame
613
612
purrr::list_rbind()
614
613
```
615
614
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
+
616
623
```{r,echo=FALSE,eval=FALSE}
617
624
# view infectee number per simulation
618
625
simulated_chains_map %>%
@@ -623,7 +630,9 @@ simulated_chains_map %>%
623
630
624
631
## Visualize multiple chains
625
632
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:
0 commit comments