@@ -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
329252Group 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
655488Group 1
0 commit comments