macpan2

Tools for building and calibrating compartmental models of infectious disease.

https://github.com/canmod/macpan2

Science Score: 77.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
    Found CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
    Found 3 DOI reference(s) in README
  • Academic publication links
    Links to: zenodo.org
  • Committers with academic emails
    1 of 15 committers (6.7%) from academic institutions
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (13.4%) to scientific vocabulary

Keywords

compartmental-models epidemiology forecasting mixed-effects model-fitting optimization simulation simulation-modeling
Last synced: 6 months ago · JSON representation ·

Repository

Tools for building and calibrating compartmental models of infectious disease.

Basic Info
Statistics
  • Stars: 6
  • Watchers: 4
  • Forks: 2
  • Open Issues: 98
  • Releases: 7
Topics
compartmental-models epidemiology forecasting mixed-effects model-fitting optimization simulation simulation-modeling
Created about 4 years ago · Last pushed 6 months ago
Metadata Files
Readme Changelog Contributing License Code of conduct Citation

README.Rmd

---
output: github_document
bibliography: inst/references.bib
link-citations: TRUE
---

```{r opts, echo = FALSE}
knitr::opts_chunk$set(
  fig.path = "man/figures/"
)
```

```{r packages, echo = FALSE, eval = TRUE, message=FALSE, warning=FALSE, error=FALSE}
library(macpan2)
library(ggplot2)
library(dplyr)
theme_bw = function() ggplot2::theme_bw(base_size = 18)
```



[![macpan2 status badge](https://canmod.r-universe.dev/badges/macpan2)](https://canmod.r-universe.dev/macpan2)
[![R-CMD-check](https://github.com/canmod/macpan2/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/canmod/macpan2/actions/workflows/R-CMD-check.yaml)
[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.14895172.svg)](https://doi.org/10.5281/zenodo.14895172)
[![test coverage](https://byob.yarr.is/canmod/macpan2/coverage)](https://github.com/canmod/macpan2/actions/workflows/test-coverage.yaml)
[![commit activity](https://img.shields.io/github/commit-activity/m/canmod/macpan2)](https://github.com/canmod/macpan2/commits)
[![contributors](https://img.shields.io/github/contributors/canmod/macpan2)](https://github.com/canmod/macpan2/graphs/contributors)

![](man/figures/mp.png)

`macpan2` is a software platform for building and calibrating compartmental models of infectious disease. It supports flexible model specification and fast parameter calibration, making it easier for modellers to respond to emerging public health threats. Developed through collaboration with the [Public Health Agency of Canada (PHAC)](https://www.canada.ca/en/public-health.html), `macpan2` is being used to support responses to diseases such as mpox [@milwid2023mpox], measles [@phac2024measles, [interactive measles model](https://wzmli.shinyapps.io/two_pop_measles_shiny/)], and COVID-19 [@simmons2025cost;@miranda2024strategies], and internal work on [pandemic preparedness](https://phac-nml-phrsd.github.io/EPACmodel/).

The precursor to `macpan2` was [McMasterPandemic](https://github.com/mac-theobio/McMasterPandemic), which was developed to provide [forecasts](https://mac-theobio.github.io/covid-19) and insights to Canadian public health agencies throughout the COVID-19 pandemic [@phac_modelling_group]. Much was [learned](https://canmod.github.io/macpan-book/index.html#vision-and-direction) about developing general purpose compartmental modelling software during this experience, but the pressure to deliver regular forecasts made it difficult to focus on the software itself. The goal of this `macpan2` project is to re-imagine `McMasterPandemic`, building it from the ground up with architectural and technological decisions that address the many lessons that we learned from COVID-19 about software.



## Documentation

* [Package website](https://canmod.github.io/macpan2/)
* [Package reference and function documentation](https://canmod.github.io/macpan2/reference)
* [Quick-start guide](https://canmod.github.io/macpan2/articles/quickstart)
* [Articles describing the package](https://canmod.github.io/macpan2/articles)
* [Example starter models](https://canmod.github.io/macpan2/articles/example_models.html)
* [Frequently asked questions](https://canmod.github.io/macpan2/articles/FAQs)
* [`TMB` engine](https://canmod.github.io/macpan2/articles/cpp_side) [specification document]
* [Project history and trajectory](https://canmod.net/misc/pyRC) [slides]
* [Instructional videos](https://drive.google.com/drive/folders/1NEQf2sy6QLxMiiWBN5Yn9U7wiPMs1jiS)
* [Workshop notes](https://canmod.github.io/macpan-workshop)
* [Publications and reports](#publications-and-reports) [using `macpan2` and `McMasterPandemic`]
* [Visual `macpan2` model builder](https://canmod.github.io/macpan2-model-builder/) [proof of concept]

## Installation

Here is the recommended way to install `macpan2` (from within an R session):

```r
repos = c('https://canmod.r-universe.dev', 'https://cloud.r-project.org')
install.packages('macpan2', repos = repos)
```

To get the latest development version of `macpan2`, or if the above command fails for some reason, try:
```r
remotes::install_github("canmod/macpan2")
```

This command requires the `remotes` package and assumes that your R environment is set up to build packages that include compiled C++ code (e.g. see details for [Windows](https://cran.r-project.org/bin/windows/base/howto-R-devel.html) or [MacOS](https://mac.r-project.org/tools/) [you probably only need Xcode from this page]).

Many `macpan2` workflows also use these four popular packages:
```r
install.packages(c("dplyr", "ggplot2", "tidyr", "broom.mixed"))
```

The `Rgraphviz` package is useful for plotting flow diagrams of models (see `?dot_layout`). To install it:

```r
if (!require("BiocManager", quietly = TRUE))
    install.packages("BiocManager")
BiocManager::install("Rgraphviz")
```

## Reproducibility

The [r-universe](https://r-universe.dev), which we use to distribute `macpan2`, suggests two approaches for projects in production that need to keep track of specific versions of `macpan2`: [snapshots](https://ropensci.org/blog/2023/05/31/runiverse-snapshots/) or [`renv`](https://ropensci.org/blog/2022/01/06/runiverse-renv/).

To take the first approach, snapshots of `macpan2` (and its dependency `oor`) can be obtained using the following download link.
```
https://canmod.r-universe.dev/api/snapshot/zip?packages=macpan2,macpan2helpers,oor
```

Please see [this documentation](https://canmod.r-universe.dev/apis) for instructions on customizing this download link.

The benefit of the first approach is that it doesn't require users to be able to compile C++ code, whereas the second does. The benefit of the second approach is that it can be used to manage dependencies on all packages in your workflows. It might be possible to combine the two approaches to get the best of both worlds, but this isn't tested.

## Hello World

 The following code specifies an [SI model](https://github.com/canmod/macpan2/blob/main/inst/starter_models/si/README.md), which is the simplest model of epidemiological transmission.
```{r hello-world}
library(macpan2)
si = mp_tmb_model_spec(
    before = S ~ 1 - I
  , during = mp_per_capita_flow(
        from      = "S"         ## compartment from which individuals flow
      , to        = "I"         ## compartment to which individuals flow
      , rate      = "beta * I"  ## expression giving _per-capita_ flow rate
      , flow_name = "infection" ## name for _absolute_ flow rate = beta * I * S
    )
  , default = list(I = 0.01, beta = 0.2)
)
print(si)
```

See [this article](https://canmod.github.io/macpan2/articles/example_models.html) for more example models with documentation.

Simulating from this model requires choosing the number of time-steps to run and the model outputs to generate. Syntax for simulating `macpan2` models is [designed to combine with standard data prep and plotting tools in R](https://canmod.github.io/macpan2/articles/quickstart.html#processing-results), as we demonstrate with the following code.
```{r plot-tmb-si, fig.height=3, dpi=600}
library(ggplot2)
library(dplyr)
(si
 |> mp_simulator(time_steps = 50, outputs = c("I", "infection"))
 |> mp_trajectory()
 |> mutate(quantity = case_match(matrix
    , "I" ~ "Prevalence"
    , "infection" ~ "Incidence"
  ))
 |> ggplot() 
 + geom_line(aes(time, value)) 
 + facet_wrap(~ quantity, scales = "free")
 + theme_bw()
)
```


## Workshops

We have delivered several `macpan2` workshops. Here are the [current workshop
notes](https://canmod.github.io/macpan-workshop/).

```{r workshops, echo = FALSE, message = FALSE}
library(readr); library(knitr)
read_csv("misc/build/workshops.csv") |> kable()
```

On June 16-27, 2025, the [International Clinics on Infectious Disease Dynamics and Data (ICI3D) Program](https://www.ici3d.org/) included labs using `macpan2` to [specify simple compartmental models and simulate from them](https://github.com/ICI3D/RTutorials/blob/master/mpLabCompartmental.R) and [specify an HIV model and fit it to data](https://github.com/ICI3D/RTutorials/blob/master/mpLabFitting.R).

## Product Management

The [project board](https://github.com/orgs/canmod/projects/2/views/6) tracks the details of bugs, tasks, and feature development.

## Publications and Reports

Owner

  • Name: Canadian Network for Modelling Infectious Diseases (CANMOD)
  • Login: canmod
  • Kind: organization
  • Email: data@canmod.net
  • Location: Canada

Strengthening the community of people who are making and using infectious disease models to improve public health both inside and outside of Canada

Citation (CITATION.cff)

cff-version: 1.2.0
message: "If you use macpan2 in your research, please cite it using the following reference."
title: "macpan2: Fast and Flexible Compartmental Modelling"
authors:
  - family-names: "Walker"
    given-names: "Steve"
    email: "swalk@mcmaster.ca"
    roles: ["creator", "author"]
  - family-names: "Guan"
    given-names: "Weiguang"
    roles: ["author"]
  - family-names: "Freeman"
    given-names: "Jen"
    roles: ["author"]
  - family-names: "Bolker"
    given-names: "Ben"
    roles: ["author"]
  - family-names: "Flynn-Primrose"
    given-names: "Darren"
    roles: ["author"]
  - family-names: "Zhao"
    given-names: "Kevin"
    roles: ["contributor"]
  - family-names: "Papst"
    given-names: "Irena"
    roles: ["contributor"]
  - family-names: "Li"
    given-names: "Michael"
    roles: ["contributor"]
url: "https://github.com/canmod/macpan2"
repository-code: "https://github.com/canmod/macpan2"
license: "GPL-3.0"
keywords:
  - epidemiological modeling
  - infectious disease dynamics
  - compartmental models
description: "Fast and flexible compartmental modelling with Template Model Builder."
references:
  - type: "software"
    title: "macpan2: Fast and Flexible Compartmental Modelling"
    authors:
      - family-names: "Walker"
        given-names: "Steve"
        email: "swalk@mcmaster.ca"
        roles: ["creator", "author"]
      - family-names: "Guan"
        given-names: "Weiguang"
        roles: ["author"]
      - family-names: "Freeman"
        given-names: "Jen"
        roles: ["author"]
      - family-names: "Bolker"
        given-names: "Ben"
        roles: ["author"]
      - family-names: "Flynn-Primrose"
        given-names: "Darren"
        roles: ["author"]
      - family-names: "Zhao"
        given-names: "Kevin"
        roles: ["contributor"]
      - family-names: "Papst"
        given-names: "Irena"
        roles: ["contributor"]
      - family-names: "Li"
        given-names: "Michael"
        roles: ["contributor"]
    publisher: "Zenodo"
    doi: "10.5281/zenodo.14796217"

GitHub Events

Total
  • Create event: 29
  • Commit comment event: 3
  • Release event: 2
  • Issues event: 126
  • Watch event: 5
  • Delete event: 6
  • Issue comment event: 209
  • Push event: 468
  • Pull request review comment event: 1
  • Pull request review event: 2
  • Pull request event: 50
  • Fork event: 2
Last Year
  • Create event: 29
  • Commit comment event: 3
  • Release event: 2
  • Issues event: 126
  • Watch event: 5
  • Delete event: 6
  • Issue comment event: 209
  • Push event: 468
  • Pull request review comment event: 1
  • Pull request review event: 2
  • Pull request event: 50
  • Fork event: 2

Committers

Last synced: about 2 years ago

All Time
  • Total Commits: 1,004
  • Total Committers: 15
  • Avg Commits per committer: 66.933
  • Development Distribution Score (DDS): 0.306
Past Year
  • Commits: 767
  • Committers: 13
  • Avg Commits per committer: 59.0
  • Development Distribution Score (DDS): 0.299
Top Committers
Name Email Commits
stevencarlislewalker s****r@g****m 697
Ben Bolker b****r@g****m 77
Flynn-Primrose D****P@u****a 69
Weiguang Guan g****w@s****a 56
Irena Papst i****t@p****a 36
Jennifer Freeman j****n@g****m 30
Irena Papst p****a@g****m 14
Maya Earn m****n@g****m 13
Flynn-Primrose d****e@o****m 5
jfree-man 9****n 2
BYOB b****b@y****s 1
Mikael Jagan j****n@m****a 1
Steve Walker s****r@u****a 1
Darren Flynn-Primrose 7****e 1
Kevinozoid 6****d 1
Committer Domains (Top 20 + Academic)

Issues and Pull Requests

Last synced: 6 months ago

All Time
  • Total issues: 196
  • Total pull requests: 52
  • Average time to close issues: 4 months
  • Average time to close pull requests: 13 days
  • Total issue authors: 9
  • Total pull request authors: 5
  • Average comments per issue: 1.92
  • Average comments per pull request: 0.69
  • Merged pull requests: 37
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 68
  • Pull requests: 27
  • Average time to close issues: 19 days
  • Average time to close pull requests: 7 days
  • Issue authors: 6
  • Pull request authors: 3
  • Average comments per issue: 1.79
  • Average comments per pull request: 0.22
  • Merged pull requests: 15
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • stevencarlislewalker (119)
  • bbolker (42)
  • papsti (18)
  • dushoff (13)
  • jfree-man (10)
  • d31v1vi5 (4)
  • jaganmn (4)
  • RichardSichengZhao (3)
  • GregForkutza (1)
  • mayaearn (1)
Pull Request Authors
  • stevencarlislewalker (47)
  • bbolker (16)
  • papsti (4)
  • Flynn-Primrose (3)
  • jfree-man (2)
  • Kevinozoid (1)
Top Labels
Issue Labels
documentation (24) engine adaptor (18) model definition files (16) engine (15) engine adaptor constructors (14) model space operations (11) enhancement (11) calibration (10) model library (8) bug (8) v2-breaking-change (4) performance (3) infrastructure (3) utilities (2) question (1) breaking-change (1) ensemble forecasting (1)
Pull Request Labels
documentation (3) engine (2) engine adaptor (2) model definition files (2) engine adaptor constructors (2) model space operations (2) v2-breaking-change (1)

Dependencies

.github/workflows/R-CMD-check.yaml actions
  • actions/checkout v3 composite
  • r-lib/actions/check-r-package v2 composite
  • r-lib/actions/setup-pandoc v2 composite
  • r-lib/actions/setup-r v2 composite
  • r-lib/actions/setup-r-dependencies v2 composite
DESCRIPTION cran
  • knitr * suggests
  • rmarkdown * suggests
.github/workflows/pkgdown.yaml actions
  • JamesIves/github-pages-deploy-action v4.4.1 composite
  • actions/checkout v3 composite
  • r-lib/actions/setup-pandoc v2 composite
  • r-lib/actions/setup-r v2 composite
  • r-lib/actions/setup-r-dependencies v2 composite
.github/workflows/test-coverage.yaml actions
  • RubbaBoy/BYOB v1.3.0 composite
  • actions/checkout v3 composite
  • actions/upload-artifact v3 composite
  • r-lib/actions/setup-r v2 composite
  • r-lib/actions/setup-r-dependencies v2 composite