1+ # nolint start
2+
3+ # load packages
4+ library(epidemics )
5+ library(socialmixr )
6+ library(tidyverse )
7+
8+ # load survey data
9+ survey_data <- socialmixr :: polymod
10+
11+ # generate contact matrix
12+ cm_results <- socialmixr :: contact_matrix(
13+ survey = survey_data ,
14+ countries = " United Kingdom" ,
15+ age.limits = c(0 , 15 , 65 ),
16+ symmetric = TRUE
17+ )
18+
19+ # transpose contact matrix
20+ cm_matrix <- t(cm_results $ matrix )
21+
22+ # prepare the demography vector
23+ demography_vector <- cm_results $ demography $ population
24+ names(demography_vector ) <- rownames(cm_matrix )
25+
26+ # initial conditions: one in every 1 million is infected
27+ initial_i <- 1e-6
28+ initial_conditions <- c(
29+ S = 1 - initial_i ,
30+ E = 0 ,
31+ I = initial_i ,
32+ R = 0 ,
33+ V = 0
34+ )
35+
36+ # build for all age groups
37+ initial_conditions <- base :: rbind(
38+ initial_conditions ,
39+ initial_conditions ,
40+ initial_conditions
41+ )
42+ rownames(initial_conditions ) <- rownames(cm_matrix )
43+
44+ # prepare the population to model as affected by the epidemic
45+ uk_population <- epidemics :: population(
46+ name = " UK" ,
47+ contact_matrix = cm_matrix ,
48+ demography_vector = demography_vector ,
49+ initial_conditions = initial_conditions
50+ )
51+
52+ # time periods
53+ preinfectious_period <- 4.0
54+ infectious_period <- 5.5
55+ basic_reproduction <- 2.7
56+
57+ # rates
58+ infectiousness_rate <- 1.0 / preinfectious_period
59+ recovery_rate <- 1.0 / infectious_period
60+ transmission_rate <- basic_reproduction * recovery_rate
61+
62+ # run baseline simulation with no intervention
63+ output_baseline <- epidemics :: model_default(
64+ population = uk_population ,
65+ transmission_rate = transmission_rate ,
66+ infectiousness_rate = infectiousness_rate ,
67+ recovery_rate = recovery_rate ,
68+ time_end = 300 , increment = 1.0
69+ )
70+
71+ output_baseline
72+
73+ # challenge ------------------------
74+
75+ # Run this visualization of the baseline model using ggplot2
76+ # Then share with the tutor
77+
78+ output_baseline %> %
79+ filter(compartment == " infectious" ) %> %
80+ ggplot(aes(
81+ x = time ,
82+ y = value ,
83+ linetype = demography_group ,
84+ colour = compartment
85+ )) +
86+ geom_line()
87+
88+ # ----------------------------------
89+
90+ # nolint end
0 commit comments