This Github repository contains the code used to implement and run age-stratified, metapopulation compartmental models using odin.dust. These models are used to analyse the dynamics of measles outbreaks in England between 2010 and 2019. The figures generated by the scripts can be found in the Output folder. The R folder contains all the scripts and functions.
The code presented in this repository should be used to reproduce the analysis (using simulated data) and figures presented in Robert A, Suffel AM, Kucharski AJ, Long-term waning of vaccine-induced immunity to measles in England: a mathematical modelling study, The Lancet Public Health, Volume 9, Issue 10, e766 - e775 (doi: 10.1016/S2468-2667(24)00181-6).
Clone/download this project onto your machine.
The following R packages are required to run the code:
* dplyr
* socialmixr
* odin.dust
* mcstate
* tidyr
* ggplot2
* data.table
* excel.link
* patchwork
and can be installed in R by running:
install.packages(c("dplyr", "socialmixr", "tidyr", "ggplot2", "data.table", "excel.link", "patchwork"))
install.packages(c("mcstate", "odin.dust"), repos = c("https://mrc-ide.r-universe.dev", "https://cloud.r-project.org"))
The code contains three scripts to 1) fit the different scenarios to case data, 2) Generate stochastic simulations from the parameter fits, and 3) Generate the figures.
Firstly, to fit the different models, run the following command:
source(“R/generate_fit_model.R”)
By default, the runtime is about 12 hours per scenario (4 scenarios in total) on a standard laptop with a 3.0 GHz processor and 32 GB RAM. To reduce the runtime, change the number of steps (parameter n_steps
) in the first line of R/generate_fit_model.R
. The original case data cannot be shared publicly, so these fits will be run on a simulated dataset.The fits will be saved in the Output
folder, in the subfolder corresponding to each scenario, and labeled no.RDS
for models without waning, since_vax.RDS
for models with waning since vaccination, and since_eli.RDS
for models with waning since elimination. The files available on the repository were generated on the actual case data.
To generate stochastic simulation sets for each scenario based on the parameter estimates, run the following command:
source(“R/generate_outbreak_all_samples.R”)
Similarly, the runtime is 5 hours per scenario, reduce the number of samples (parameter n_samples
) in the first line of R/generate_outbreak_all_samples.R
. The simulations will be saved in the Output
folder, in the subfolder corresponding to each scenario, and labeled sim_no.RDS
for models without waning, sim_since_vax.RDS
for models with waning since vaccination, and sim_since_eli.RDS
for models with waning since elimination. The files available on the repository were generated from parameter estimates using the actual case data.
Finally, to generate the figures describing the parameter estimates and simulations for each scenario, run the following command:
source(“R/generate_figures.R”)
The overall runtime does not exceed 15 minutes on a standard laptop with a 3.0 GHz processor and 32 GB RAM. All files are saved in the Output
folder, in the subfolder figures
of the folder corresponding to each scenario.
The output folder contains four subfolders (one for each scenario):
Output/cprd_degree.csv
: Files and figures using CPRD coverage data, and estimating the spatial kernel.Output/cprd_degree_sec.csv
: Files and figures using CPRD coverage data, estimating the spatial kernel, with a risk of leak from vaccinated compartments constant with age.Output/cover_degree.csv
: Files and figures using COVER coverage data, and estimating the spatial kernel.Output/cprd_fixed.csv
: Files and figures using CPRD coverage data, with a fixed spatial kernel.
Each folder contains three files with the parameter estimates and posterior distribution, computed when fitting the model to measles case data reported in England between 2010 and 2019:
no.rds
: model without waning.since_vax.rds
: model with waning by age.since_eli.rds
: model with waning by age, since elimination (1990).
Each file is a list, the parameter estimates are contained in the element pars
of the list. The log-posterior, log-prior, and log-likelihood scores and contained in the element probabilities
of the list.
Each folder also contains three files with the simulation set generated from the parameter estimates.
sim_no.rds
: simulation from the model without waning.sim_since_vax.rds
: simulation from the model with waning by age.sim_since_eli.rds
: simulation from the model with waning by age, since elimination (1990). Each file contains 5,000 simulations. Each file is a 3-d array (dimension 324500010), containing the number of cases by stratification (i.e. region, age and vaccination status: 324 combinations in total), by simulation (5,000 simulations per set), and by year (10 years between 2010 and 2019). The first element of the array therefore contains the number of unvaccinated cases, in region 1, in the youngest age group, in 2010, in the first simulation.
Finally, after running the script R/generate_figures.R
, a sub-subfolder figures
is created in each scenario subfolder, and contains the figures describing the parameter estimates and simulations.
The analysis uses various datasets. The Data
folder contains the following files:
Data/regional_population.csv
: Number of inhabitants per regions and age groups from census dataData/coverage_cover_extrapol.csv
: Vaccine coverage (first and second dose) at 1, 2, 3, 4, and 5 years old, from 2005 to 2019, from UKHSA's COVER data. Coverage at 3 and 4 was extrapolated using age-stratified CPRD data.Data/coverage_cprd_extrapol.csv
: Vaccine coverage (first and second dose) at 1, 2, 3, 4, and 5 years old, from 2005 to 2019, from CPRD data. Coverage for 2015-2019 was extrapolated using the variations in coverage in the COVER dataData/risk_assessment_ukhsa.csv
: Vaccine coverage across England and in London, from UKHSA's risk assessment for measles resurgence (https://assets.publishing.service.gov.uk/government/uploads/system/uploads/attachment_data/file/1170146/risk-assessment-for-measles-resurgence-in-the-UK-2023.pdf).Data/sim_data.RDS
: Simulated case datasets, as actual 2010-2020 case data in England cannot be shared.
To re-create the simulated dataset Data/sim_data.RDS
, run the following command:
source(“R/generate_simulated_data.R”)
Directory structure:
├── Data
│ ├── coverage_cover_extrapol.csv
│ ├── coverage_cprd_extrapol.csv
│ ├── regional_population.csv
│ ├── risk_assessment_ukhsa.csv
│ └── sim_data.RDS
├── Output
│ ├── cprd_degree
│ │ ├── *.RDS
│ │ ├── sim_*.RDS
│ │ └── figure
│ ├── cprd_degree_sec
│ │ ├── *.RDS
│ │ ├── sim_*.RDS
│ │ └── figure
│ ├── cover_degree
│ │ ├── *.RDS
│ │ ├── sim_*.RDS
│ │ └── figure
│ └── cprd_fixed
│ ├── *.RDS
│ ├── sim_*.RDS
│ └── figure
├── R
│ ├── function_create_params.R
│ ├── function_generate_outbreak.R
│ ├── function_import_data.R
│ ├── function_model.R
│ ├── function_plot_simulations.R
│ ├── generate_figures.R
│ ├── generate_fit_model.R
│ ├── generate_outbreak_all_samples.R
│ ├── generate_simulated_data.R
│ ├── import_library.R
│ └── model_odin_dust.R
└── README.md
The R file contains various scripts and function files:
import_library.R
: list all libraries and local files that must be imported.generate_fit_model.R
: Script to fit the model to the simulated data (for each scenario and waning status).generate_outbreak_all_samples.R
: Script to generate stochastic simulations from the model fits (for each scenario and waning status).generate_figures.R
: Script to generate and save figures describing the stochastic simulations and model fits.model_odin_dust.R
: Define the odin.dust model, stratified by age group and region.function_import_data.R
: Contains functions to import the population, vaccine, contact matrix, number of importations, and case data; compute the annual number of births, the initial states, the vaccine coverage per year.function_create_params.R
: Contains function to createpmcmc_parameters
object, initial values of parameters and proposal matrix.function_model.R
: Contains functions to create control, filter, and particle_filter_data objects needed for odin.dust ; and the functionrun_model
to run the odin.dust model.function_generate_outbreak.R
: Contains functions to generate stochastic outbreaks from parameter estimates.function_plot_simulations.R
: Contains functions to visualise parameter fits and stochastic simulationsgenerate_simulated_data.R
: Script to generate the simulated datasetData/sim_data.RDS