Skip to content

Commit 74bb961

Browse files
committed
differences for PR #124
1 parent 0ab1f1f commit 74bb961

4 files changed

Lines changed: 39 additions & 16 deletions

File tree

.DS_Store

-6 KB
Binary file not shown.
25.1 KB
Loading

md5sum.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"index.md" "32bc80d6f4816435cc0e01540cb2a513" "site/built/index.md" "2025-11-11"
66
"links.md" "8184cf4149eafbf03ce8da8ff0778c14" "site/built/links.md" "2025-11-11"
77
"episodes/contact-matrices.Rmd" "19856620d33f9b7f4e8ee312460494f1" "site/built/contact-matrices.md" "2025-11-11"
8-
"episodes/simulating-transmission.Rmd" "f6936ef9183fad8bf630e07208ddee4f" "site/built/simulating-transmission.md" "2025-11-16"
8+
"episodes/simulating-transmission.Rmd" "3e3ecf82148896a33189045d704f9606" "site/built/simulating-transmission.md" "2025-11-20"
99
"episodes/model-choices.Rmd" "aa195e66455fb6a97b4930fd08c08001" "site/built/model-choices.md" "2025-11-11"
1010
"episodes/modelling-interventions.Rmd" "4ddf2a3e860dc11ba7a29ad20228eb3a" "site/built/modelling-interventions.md" "2025-11-11"
1111
"episodes/compare-interventions.Rmd" "2ef6697bbad9bcfb843ab9d50469123b" "site/built/compare-interventions.md" "2025-11-11"

simulating-transmission.md

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Install packages if their are not already installed
4343

4444
```r
4545
if (!base::require("pak")) install.packages("pak")
46-
pak::pak(c("epiverse-trace/epidemics", "socialmixr", "tidyverse"))
46+
pak::pak(c("epiverse-trace/epidemics", "socialmixr", "scales", "tidyverse"))
4747
```
4848

4949
If you have any error message,
@@ -178,10 +178,10 @@ contact_data <- socialmixr::contact_matrix(
178178
)
179179

180180
# prepare contact matrix
181-
contact_matrix <- t(contact_data$matrix)
181+
socialcontact_matrix <- t(contact_data$matrix)
182182

183183
# print
184-
contact_matrix
184+
socialcontact_matrix
185185
```
186186

187187
``` output
@@ -192,6 +192,22 @@ contact.age.group [0,20) [20,40) 40+
192192
40+ 3.063895 4.599893 5.005571
193193
```
194194

195+
Remember that the matrix satisfies the `symmetric = TRUE` condition at the level of total number of contacts.
196+
197+
The total number of contacts between groups $i$ and $j$ is calculated as the mean number of contacts (`contact_data$matrix`) multiplied by the number of individuals in group $i$ (`contact_data$demography$population`)
198+
199+
200+
``` r
201+
contact_data$matrix * contact_data$demography$population
202+
```
203+
204+
``` output
205+
contact.age.group
206+
age.group [0,20) [20,40) 40+
207+
[0,20) 116672620 46177038 45343471
208+
[20,40) 46177038 80232531 76019216
209+
40+ 45343471 76019216 144967139
210+
```
195211

196212
:::::::::::::::::::::::::::::::::
197213
::::::::::::::::::::::::::::::::::::::::::::::::
@@ -202,7 +218,7 @@ The result is a square matrix with rows and columns for each age group. Contact
202218

203219
### Normalisation
204220

205-
In `{epidemics}` the contact matrix normalisation happens within the function call, so we don't need to normalise the contact matrix before we pass it to `population()` (see section 3. Population Structure). For details on normalisation, see the tutorial on [Contact matrices](../episodes/contact-matrices.md).
221+
In `{epidemics}` the contact matrix normalisation happens within the function call, so we don't need to normalise the contact matrix before we pass it to `epidemics::population()` (see section 3. Population Structure). For details on normalisation, see the tutorial on [Contact matrices](../episodes/contact-matrices.md).
206222

207223
::::::::::::::::::::::::::::::::::::::::::::::::
208224

@@ -227,12 +243,16 @@ The initial conditions in the first age category are $S(0)=1-\frac{1}{1,000,000}
227243

228244

229245
``` r
246+
# 1 in 1,000,000 is equivalent to 1e-6
230247
initial_i <- 1e-6
231248
initial_conditions_inf <- c(
232249
S = 1 - initial_i, E = 0, I = initial_i, R = 0, V = 0
233250
)
234251
```
235252

253+
Note that R uses scientific `e` notation where `e` tells you to multiple the base number by 10 raised to the power shown ([DataKwery, 2020](https://www.datakwery.com/post/2020-07-11-scientific-notation-in-r/)).
254+
The expression $1 \times 10^{-6}$ is equivalent to `1e-6`.
255+
236256
For the age categories that are free from infection, the initial conditions are $S(0)=1$, $E(0) =0$, $I(0)=0$, $R(0)=0$. We specify this as follows,
237257

238258

@@ -246,15 +266,15 @@ We combine the three initial conditions vectors into one matrix,
246266

247267

248268
``` r
249-
# combine the initial conditions
269+
# combine the initial conditions into a matrix class object
250270
initial_conditions <- rbind(
251-
initial_conditions_inf, # age group 1
271+
initial_conditions_inf, # age group 1 (only group with infectious)
252272
initial_conditions_free, # age group 2
253273
initial_conditions_free # age group 3
254274
)
255275

256276
# use contact matrix to assign age group names
257-
rownames(initial_conditions) <- rownames(contact_matrix)
277+
rownames(initial_conditions) <- rownames(socialcontact_matrix)
258278
initial_conditions
259279
```
260280

@@ -273,8 +293,11 @@ The population object requires a vector containing the demographic structure of
273293

274294

275295
``` r
296+
# extract the demography vector
276297
demography_vector <- contact_data$demography$population
277-
names(demography_vector) <- rownames(contact_matrix)
298+
299+
# use contact matrix to assign age group names
300+
names(demography_vector) <- rownames(socialcontact_matrix)
278301
demography_vector
279302
```
280303

@@ -283,15 +306,15 @@ demography_vector
283306
14799290 16526302 28961159
284307
```
285308

286-
To create our population object, from the `{epidemics}` package we call the function `population()` specifying a name, the contact matrix, the demography vector and the initial conditions.
309+
To create our population object, from the `{epidemics}` package we call the function `epidemics::population()` specifying a name, the contact matrix, the demography vector and the initial conditions.
287310

288311

289312
``` r
290313
library(epidemics)
291314

292-
uk_population <- population(
315+
uk_population <- epidemics::population(
293316
name = "UK",
294-
contact_matrix = contact_matrix,
317+
contact_matrix = socialcontact_matrix,
295318
demography_vector = demography_vector,
296319
initial_conditions = initial_conditions
297320
)
@@ -371,12 +394,12 @@ For models that are described by [differential equations](../learners/reference.
371394
An _ODE solver_ is the software used to find numerical solutions to differential equations. If interested on how a system of differential equations is solved in `{epidemics}`, we suggest you to read the section on [ODE systems and models](https://epiverse-trace.github.io/epidemics/articles/design-principles.html#ode-systems-and-models) at the "Design principles" vignette.
372395
::::::::::::::::::::::::::::::::::::::::::::::::
373396

374-
Now we are ready to run our model using `model_default()` from the `{epidemics}` package.
397+
Now we are ready to run our model using `epidemics::model_default()` from the `{epidemics}` package.
375398

376399
Let's specify `time_end=600` to run the model for 600 days.
377400

378401
``` r
379-
output <- model_default(
402+
output <- epidemics::model_default(
380403
# population
381404
population = uk_population,
382405
# rates
@@ -503,7 +526,7 @@ newinfections_bygroup %>%
503526
theme_bw()
504527
```
505528

506-
<img src="fig/simulating-transmission-rendered-unnamed-chunk-6-1.png" style="display: block; margin: auto;" />
529+
<img src="fig/simulating-transmission-rendered-unnamed-chunk-7-1.png" style="display: block; margin: auto;" />
507530

508531
:::::::::::::::::::::::
509532

@@ -547,7 +570,7 @@ beta <- r_samples / infectious_period
547570

548571

549572
``` r
550-
output_samples <- model_default(
573+
output_samples <- epidemics::model_default(
551574
population = uk_population,
552575
transmission_rate = beta,
553576
infectiousness_rate = infectiousness_rate,

0 commit comments

Comments
 (0)