Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions instructors/05-miscellanea-tutors.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,14 @@ Room 3

::: {.content-visible unless-format="docx"}

## Code

### Activity 2

```{r, file = "fig/05-miscellanea-instructor-2.R", eval = FALSE}

```

# Continue your learning path

<!-- Suggest learners to Epiverse-TRACE documentation or external resources --->
Expand Down
155 changes: 155 additions & 0 deletions instructors/files/05-miscellanea-tutors.md
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,161 @@ Use assumptions assigned to your room.
| 2 | $3.0$ | Germany | {0, 20, 40, 60} | 1 | 1 |
| 3 | $0.9$ | Netherlands | {0, 30, 50, 70} | 1 | 1 |

## Code

### Activity 2

``` r
# nolint start

# Practical 2 -miscellaneous on final size of heterogeneous population
# Activity 1

room_number <- 1

# Load packages ----------------------------------------------------------
library(finalsize)
library(tidyverse)
library(socialmixr)

# Declare the value of the given R_0 ---------------------------------------
r0 <- 1.8

# load the polymod survey ----------------------------------------
polymod <- socialmixr::polymod

# Extract data for the country and age groups assigned to your room--------

# get your country polymod data
contact_data <- socialmixr::contact_matrix(
polymod,
countries = "Italy",
age_limits = c(0,30,50,80),
symmetric = TRUE
)

# view the elements of the contact data list
# the contact matrix
contact_data$matrix

# the demography data
contact_data$demography

# get the contact matrix and demography data
# contact_matrix <- contact_data$matrix
demography_vector <- contact_data$demography$population
demography_data <- contact_data$demography


# Transpose and normalize contact matrix---------------------------------------

# scale the contact matrix so the largest eigenvalue is 1.0
# this is to ensure that the overall epidemic dynamics correctly reflect
# the assumed value of R0
contact_matrix <- t(contact_data$matrix)
scaling_factor <- 1 / max(eigen(contact_matrix)$values)
normalised_matrix <- contact_matrix * scaling_factor

normalised_matrix

# divide each row of the contact matrix by the corresponding demography
# this reflects the assumption that each individual in group {j} make contacts
# at random with individuals in group {i}
divided_matrix <- normalised_matrix/demography_vector

# Create susceptibility, and demography-susceptibility distribution matrices---
n_demo_grps <- length(demography_vector)

# Number of susceptible groups
n_susc_groups <- 1
# susceptibility level
susc_guess <- 1

# Declare susceptibility matrix

susc_uniform <- matrix(
data = susc_guess,
nrow = n_demo_grps,
ncol = n_susc_groups
)

# Declare demography-susceptibility distribution matrix
p_susc_uniform <- matrix(
data = 1,
nrow = n_demo_grps,
ncol = n_susc_groups
)

# Calculate the final size -----------------------------------------------------

final_size_data <- final_size(
r0 = r0,
contact_matrix = divided_matrix,
demography_vector = demography_vector,
susceptibility = susc_uniform,
p_susceptibility = p_susc_uniform
)

# View the output data frame
final_size_data

# Visualize the proportion infected in each demographic group-------------------

# order demographic groups as factors
final_size_data$demo_grp <- factor(
final_size_data$demo_grp,
levels = demography_data$age.group
)
# plot data
ggplot(final_size_data) +
geom_col(
aes(
demo_grp, p_infected
),
colour = "black", fill = "grey"
) +
scale_y_continuous(
labels = scales::percent,
limits = c(0, 1)
) +
expand_limits(
x = c(0.5, nrow(final_size_data) + 0.5)
) +
theme_classic() +
coord_cartesian(
expand = FALSE
) +
labs(
x = "Age group",
y = "% Infected"
)
# Visualize the total number infected in each demographic group----------------
# prepare demography data
demography_data <- contact_data$demography

# merge final size counts with demography vector
final_size_data <- merge(
final_size_data,
demography_data,
by.x = "demo_grp",
by.y = "age.group"
)

# reset age group order
final_size_data$demo_grp <- factor(
final_size_data$demo_grp,
levels = contact_data$demography$age.group
)

# multiply counts with proportion infected
final_size_data$n_infected <- final_size_data$p_infected *
final_size_data$population

final_size_data

# nolint end
```

# Continue your learning path

<!-- Suggest learners to Epiverse-TRACE documentation or external resources --->
Expand Down
Loading