Skip to content

Commit 58a4d7e

Browse files
committed
include isolation of set 3
1 parent 8de2b84 commit 58a4d7e

File tree

4 files changed

+228
-198
lines changed

4 files changed

+228
-198
lines changed

instructors/03-practical-tutors.qmd

Lines changed: 2 additions & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -243,87 +243,10 @@ Write your answers to the questions above:
243243

244244
##### Set 1 (sample)
245245

246-
```{r}
247-
#| warning: false
248-
#| eval: false
246+
```{r, file = "fig/03-practical-instructor-1.R", eval = FALSE}
249247
250-
# Load packages -----------------------------------------------------------
251-
library(epicontacts)
252-
library(fitdistrplus)
253-
library(tidyverse)
254-
255-
256-
# Read linelist and contacts ----------------------------------------------
257-
dat_contacts <- readr::read_rds(
258-
"https://epiverse-trace.github.io/tutorials-middle/data/set-01-contacts.rds" #<DIFFERENT PER GROUP>
259-
)
260-
261-
dat_linelist <- readr::read_rds(
262-
"https://epiverse-trace.github.io/tutorials-middle/data/set-01-linelist.rds" #<DIFFERENT PER GROUP>
263-
)
264-
265-
266-
# Create an epicontacts object -------------------------------------------
267-
epi_contacts <-
268-
epicontacts::make_epicontacts(
269-
linelist = dat_linelist,
270-
contacts = dat_contacts,
271-
directed = TRUE
272-
)
273-
274-
epi_contacts
275-
276-
# visualize the contact network
277-
contact_network <- epicontacts::vis_epicontacts(epi_contacts)
278-
279-
contact_network
280-
281-
282-
# Count secondary cases per subject in contacts and linelist --------
283-
secondary_cases <- epicontacts::get_degree(
284-
x = epi_contacts,
285-
type = "out",
286-
only_linelist = TRUE
287-
)
288-
289-
# plot the histogram of secondary cases
290-
individual_reproduction_num <- secondary_cases %>%
291-
enframe() %>%
292-
ggplot(aes(value)) +
293-
geom_histogram(binwidth = 1) +
294-
labs(
295-
x = "Number of secondary cases",
296-
y = "Frequency"
297-
)
298-
299-
individual_reproduction_num
300-
301-
302-
# Fit a negative binomial distribution -----------------------------------
303-
offspring_fit <- secondary_cases %>%
304-
fitdistrplus::fitdist(distr = "nbinom")
305-
306-
offspring_fit
307-
308-
309-
# Estimate proportion of new cases from a cluster of secondary cases -----
310-
311-
# Set seed for random number generator
312-
set.seed(33)
313-
314-
# Estimate the proportion of new cases originating from
315-
# a transmission cluster of at least 5, 10, or 25 cases
316-
proportion_cases_by_cluster_size <-
317-
superspreading::proportion_cluster_size(
318-
R = offspring_fit$estimate["mu"],
319-
k = offspring_fit$estimate["size"],
320-
cluster_size = c(5, 10, 25)
321-
)
322-
323-
proportion_cases_by_cluster_size
324248
```
325249

326-
327250
#### Outputs
328251

329252
Group 1
@@ -556,100 +479,10 @@ Write your answers to the questions above:
556479

557480
##### Set 1 (sample)
558481

559-
```{r}
560-
#| warning: false
561-
#| eval: false
562-
563-
# Load packages -----------------------------------------------------------
564-
library(epiparameter)
565-
library(epichains)
566-
library(tidyverse)
567-
568-
569-
# Set input parameters ---------------------------------------------------
570-
known_basic_reproduction_number <- 0.8 #<DIFFERENT PER GROUP>
571-
known_dispersion <- 0.01 #<DIFFERENT PER GROUP>
572-
chain_to_observe <- 957 #<DIFFERENT PER GROUP>
573-
482+
```{r, file = "fig/03-practical-instructor-2.R", eval = FALSE}
574483
575-
# Set iteration parameters -----------------------------------------------
576-
577-
# Create generation time as <epiparameter> object
578-
generation_time <- epiparameter::epiparameter(
579-
disease = "disease x",
580-
epi_name = "generation time",
581-
prob_distribution = "gamma",
582-
summary_stats = list(mean = 3, sd = 1)
583-
)
584-
585-
586-
# Simulate multiple chains -----------------------------------------------
587-
# run set.seed() and epichains::simulate_chains() together, in the same run
588-
589-
# Set seed for random number generator
590-
set.seed(33)
591-
592-
multiple_chains <- epichains::simulate_chains(
593-
# simulation controls
594-
n_chains = 1000, # number of chains to simulate
595-
statistic = "size",
596-
stat_threshold = 500, # stopping criteria
597-
# offspring
598-
offspring_dist = rnbinom,
599-
mu = known_basic_reproduction_number,
600-
size = known_dispersion,
601-
# generation
602-
generation_time = function(x) generate(x = generation_time, times = x)
603-
)
604-
605-
multiple_chains
606-
607-
608-
# Explore suggested chain ------------------------------------------------
609-
multiple_chains %>%
610-
# use data.frame output from <epichains> object
611-
as_tibble() %>%
612-
filter(chain == chain_to_observe) %>%
613-
print(n = Inf)
614-
615-
616-
# visualize ---------------------------------------------------------------
617-
618-
# daily aggregate of cases
619-
aggregate_chains <- multiple_chains %>%
620-
as_tibble() %>%
621-
# count the daily number of cases in each chain
622-
mutate(day = ceiling(time)) %>%
623-
count(chain, day, name = "cases") %>%
624-
# calculate the cumulative number of cases for each chain
625-
group_by(chain) %>%
626-
mutate(cumulative_cases = cumsum(cases)) %>%
627-
ungroup()
628-
629-
# Visualize transmission chains by cumulative cases
630-
aggregate_chains %>%
631-
# create grouped chain trajectories
632-
ggplot(aes(x = day, y = cumulative_cases, group = chain)) +
633-
geom_line(color = "black", alpha = 0.25, show.legend = FALSE) +
634-
# define a 100-case threshold
635-
geom_hline(aes(yintercept = 100), lty = 2) +
636-
labs(x = "Day", y = "Cumulative cases")
637-
638-
# count chains over 100 cases
639-
aggregate_chains %>%
640-
filter(cumulative_cases >= 100) %>%
641-
count(chain)
642-
# distribution of size of chains
643-
aggregate_chains %>%
644-
filter(cumulative_cases >= 100) %>%
645-
skimr::skim(cumulative_cases)
646-
# distribution of lenght of chains
647-
aggregate_chains %>%
648-
filter(cumulative_cases >= 100) %>%
649-
skimr::skim(day)
650484
```
651485

652-
653486
#### Outputs
654487

655488
Group 1
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# nolint start
2+
3+
# Practical 3
4+
# Activity 1
5+
6+
# Load packages -----------------------------------------------------------
7+
library(epicontacts)
8+
library(fitdistrplus)
9+
library(tidyverse)
10+
11+
12+
# Read linelist and contacts ----------------------------------------------
13+
dat_contacts <- readr::read_rds(
14+
"https://epiverse-trace.github.io/tutorials-middle/data/set-01-contacts.rds" #<DIFFERENT PER GROUP>
15+
)
16+
17+
dat_linelist <- readr::read_rds(
18+
"https://epiverse-trace.github.io/tutorials-middle/data/set-01-linelist.rds" #<DIFFERENT PER GROUP>
19+
)
20+
21+
22+
# Create an epicontacts object -------------------------------------------
23+
epi_contacts <- epicontacts::make_epicontacts(
24+
linelist = dat_linelist,
25+
contacts = dat_contacts,
26+
directed = TRUE
27+
)
28+
29+
# Print output
30+
epi_contacts
31+
32+
# Visualize the contact network
33+
contact_network <- epicontacts::vis_epicontacts(epi_contacts)
34+
35+
# Print output
36+
contact_network
37+
38+
39+
# Count secondary cases per subject in contacts and linelist --------------
40+
secondary_cases <- epicontacts::get_degree(
41+
x = epi_contacts,
42+
type = "out",
43+
only_linelist = TRUE
44+
)
45+
46+
# Plot the histogram of secondary cases
47+
individual_reproduction_num <- secondary_cases %>%
48+
enframe() %>%
49+
ggplot(aes(value)) +
50+
geom_histogram(binwidth = 1) +
51+
labs(
52+
x = "Number of secondary cases",
53+
y = "Frequency"
54+
)
55+
56+
# Print output
57+
individual_reproduction_num
58+
59+
60+
# Fit a negative binomial distribution -----------------------------------
61+
offspring_fit <- secondary_cases %>%
62+
fitdistrplus::fitdist(distr = "nbinom")
63+
64+
# Print output
65+
offspring_fit
66+
67+
68+
# Estimate proportion of new cases from a cluster of secondary cases ------
69+
70+
# Set seed for random number generator
71+
set.seed(33)
72+
73+
# Estimate the proportion of new cases originating from
74+
# a transmission cluster of at least 5, 10, or 25 cases
75+
proportion_cases_by_cluster_size <-
76+
superspreading::proportion_cluster_size(
77+
R = offspring_fit$estimate["mu"],
78+
k = offspring_fit$estimate["size"],
79+
cluster_size = c(5, 10, 25)
80+
)
81+
82+
# Print output
83+
proportion_cases_by_cluster_size
84+
85+
# nolint end
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# nolint start
2+
3+
# Practical 3
4+
# Activity 2
5+
6+
# Load packages -----------------------------------------------------------
7+
library(epiparameter)
8+
library(epichains)
9+
library(tidyverse)
10+
11+
12+
# Set input parameters ---------------------------------------------------
13+
known_basic_reproduction_number <- 0.8
14+
known_dispersion <- 0.01
15+
chain_to_observe <- 957
16+
17+
18+
# Set iteration parameters -----------------------------------------------
19+
20+
# Create generation time as an <epiparameter> object
21+
generation_time <- epiparameter::epiparameter(
22+
disease = "disease x",
23+
epi_name = "generation time",
24+
prob_distribution = "gamma",
25+
summary_stats = list(mean = 3, sd = 1)
26+
)
27+
28+
29+
# Simulate multiple chains -----------------------------------------------
30+
# Run set.seed() and epichains::simulate_chains() together, in the same run
31+
32+
# Set seed for random number generator
33+
set.seed(33)
34+
35+
multiple_chains <- epichains::simulate_chains(
36+
# Simulation controls
37+
n_chains = 1000, # Number of chains to simulate
38+
statistic = "size",
39+
stat_threshold = 500, # Stopping criteria
40+
# Offspring
41+
offspring_dist = rnbinom,
42+
mu = known_basic_reproduction_number,
43+
size = known_dispersion,
44+
# Generation
45+
generation_time = function(x) generate(x = generation_time, times = x)
46+
)
47+
48+
multiple_chains
49+
50+
51+
# Explore suggested chain ------------------------------------------------
52+
multiple_chains %>%
53+
# Use data.frame output from <epichains> object
54+
as_tibble() %>%
55+
filter(chain == chain_to_observe) %>%
56+
print(n = Inf)
57+
58+
59+
# Visualize --------------------------------------------------------------
60+
61+
# Daily aggregate of cases
62+
aggregate_chains <- multiple_chains %>%
63+
as_tibble() %>%
64+
# Count the daily number of cases in each chain
65+
mutate(day = ceiling(time)) %>%
66+
count(chain, day, name = "cases") %>%
67+
# Calculate the cumulative number of cases for each chain
68+
group_by(chain) %>%
69+
mutate(cumulative_cases = cumsum(cases)) %>%
70+
ungroup()
71+
72+
# Visualize transmission chains by cumulative cases
73+
aggregate_chains %>%
74+
# Create grouped chain trajectories
75+
ggplot(aes(x = day, y = cumulative_cases, group = chain)) +
76+
geom_line(color = "black", alpha = 0.25, show.legend = FALSE) +
77+
# Define a 100-case threshold
78+
geom_hline(aes(yintercept = 100), lty = 2) +
79+
labs(x = "Day", y = "Cumulative cases")
80+
81+
# count chains over 100 cases
82+
aggregate_chains %>%
83+
filter(cumulative_cases >= 100) %>%
84+
count(chain)
85+
# distribution of size of chains
86+
aggregate_chains %>%
87+
filter(cumulative_cases >= 100) %>%
88+
skimr::skim(cumulative_cases)
89+
# distribution of lenght of chains
90+
aggregate_chains %>%
91+
filter(cumulative_cases >= 100) %>%
92+
skimr::skim(day)
93+
94+
# nolint end

0 commit comments

Comments
 (0)