How to create heterogeneous susceptibility matrices?

Published

December 3, 2024

Ingredients

Conditions:

  • Create the two susceptibility matrices needed for the finalsize package.
  • The population is divided into five age groups: 0 – 4, 5 – 17, 18 – 39, 40 – 64, and 65 and over.
  • An immunization campaign generated within age group variation in susceptibility.
  • The immunization effect for all age groups is 25%.
  • The infection susceptibility between age group differs in the following way:
    • 20% in age group 0 – 4,
    • 50% in age group 5 – 17,
    • 60% in age group 18 – 39,
    • 90% in age group 40 – 64,
    • 100% in age group of 65 and over,
  • The percentage of the unimmunized population in all age groups is 60%.

Steps in code

# Load required packages
library(tidyverse)

# Define the immunization effect
immunization_effect <- 0.25

# Create the susceptibility matrix
susceptibility <- 
  tribble(
    ~age_group, ~unimmunised,
       "[0,5)",          0.2,
      "[5,18)",          0.5,
     "[18,40)",          0.6,
     "[40,65)",          0.9,
         "65+",            1
  ) %>%
  mutate(immunised = unimmunised * (1 - immunization_effect)) %>%
  column_to_rownames(var = "age_group") %>%
  as.matrix()

susceptibility
#>         unimmunised immunised
#> [0,5)           0.2     0.150
#> [5,18)          0.5     0.375
#> [18,40)         0.6     0.450
#> [40,65)         0.9     0.675
#> 65+             1.0     0.750

# Create the demography-in-susceptibility matrix
p_susceptibility <- 
  tribble(
    ~age_group, ~unimmunised,
       "[0,5)",          0.6,
      "[5,18)",          0.6,
     "[18,40)",          0.6,
     "[40,65)",          0.6,
         "65+",          0.6
  ) %>%
  mutate(immunised = 1 - unimmunised) %>%
  column_to_rownames(var = "age_group") %>%
  as.matrix()

p_susceptibility
#>         unimmunised immunised
#> [0,5)           0.6       0.4
#> [5,18)          0.6       0.4
#> [18,40)         0.6       0.4
#> [40,65)         0.6       0.4
#> 65+             0.6       0.4

Steps in detail

  • The tidyverse package is loaded to create matrices from data frame operations.
  • The immunization_effect contains the 0.25 effect from the immunization campaign.
  • The susceptibility object is a matrix that contains the susceptibility or probability of infection. Each row-column element in the matrix represents the susceptibility of individuals in demographic groups (rows) and susceptibility groups (columns).
  • The tribble() function creates a tibble with a row-by-row layout. The “age_group” column contains all age groups, and the “unimmunized” column has its corresponding susceptibility values.
  • The mutate() function creates the “immunized” column by multiplying the susceptibility values from the “unimmunized” column times the new susceptibility after the immunization_effect.
  • The column_to_rownames() function creates data frame row names from the explicit column “age_group”.
  • The as.matrix() function turns the data frame into a matrix.
  • The p_susceptibility object is a matrix that contains the proportion or probability that individuals in a demographic group are also in a specific susceptibility group. Each row-column element in the matrix represents the proportion of each demographic group (rows) that falls into the susceptibility group (columns). Consequently, each of its rows must always sum to 1.0.
  • The tribble() function creates a tibble with a row-by-row layout. The “age_group” column contains all age groups, and the “unimmunized” column proportion of the unimmunized population in all age groups equals 0.6.
  • The mutate() function creates the “immunized” column by getting the complement proportion of the unimmunized”.

Please note that the code assumes the necessary packages are already installed. If they are not, you can install them using first the install.packages("pak") function and then the pak::pak() function for both packages in CRAN or GitHub before loading them with library().