Git Product home page Git Product logo

cdcgov / ww-inference-model Goto Github PK

View Code? Open in Web Editor NEW
9.0 5.0 0.0 249 KB

An in-development R package and a Bayesian hierarchical model jointly fitting multiple "local" wastewater data streams and "global" case count data to produce nowcasts and forecasts of both observations

Home Page: https://cdcgov.github.io/ww-inference-model/

License: Apache License 2.0

R 81.45% Stan 18.55%
bayesian-inference forecasting hierarchical-models real-time-analytics wastewater-based-epidemiology cmdstanr stan

ww-inference-model's Introduction

wwinference: joint inference and forecasting from wastewater and epidemiological indicators

Caution

This project is a work-in-progress. Despite this project's early stage, all development is in public as part of the Center for Forecasting and Outbreak Analytics' goals around open development. Questions and suggestions are welcome through GitHub issues or a PR.

Overview

This project is an in-development R package, {wwinference} that estimates latent incident infections from wastewater concentration data and data on epidemiological indicators, with an initial assumed structure that the wastewater concentration data comes from subsets of the population contributing to the "global" epidemiological indicator data, such as hospital admissions. In brief, our model builds upon EpiNow2, a widely used R and Stan package for Bayesian epidemiological inference. We modify EpiNow2 to add model for the observed viral RNA concentration in wastewater, adding hierarchical structure to link the subpopulations represented by the osberved wastewater concentrations in each wastewater catchment area. See our Model Definition page for a mathematical description of the generative model, and the Getting Stated vignette to see an example of how to run the inference model on simulated data.

The intention is for {wwinference} to provide a user-friendly R-package interface for running forecasting models that use wastewater concentrations combined with other more traditional epidemiological signals such as cases or hospital admissions. It aims to be a re-implementation of the modeling components contained in the wastewater-informed-covid-forecasting project repository, with an emphasis here on making it easier for users to supply their own data.

We recommend reading the model definition to learn more about how the model is structured and running the "Getting Started" vignette for an example of how to fit the model to simulated data of COVID-19 hospital admissions and wastewater concentrations. This will help make clear the data requirements and how to structure this data to fit the model.

Project Admins

  • Kaitlyn Johnson (kaitejohnson)
  • Dylan Morris (dylanhmorris)
  • Sam Abbott (seabbs)
  • Damon Bayer (damonbayer)

Installing and running code

Install R

To run our code, you will need a working installation of R (version 4.3.0 or later). You can find instructions for installing R on the official R project website.

Install cmdstanr and CmdStan

We do inference from our models using CmdStan (version 2.35.0 or later) via its R interface cmdstanr (version 0.8.0 or later).

Open an R session and run the following command to install cmdstanr per that package's official installation guide.

install.packages("cmdstanr", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))

cmdstanr provides tools for installing CmdStan itself. First check that everything is properly configured by running:

cmdstanr::check_cmdstan_toolchain()

You should see the following:

The C++ toolchain required for CmdStan is setup properly!

If you do, you can then install CmdStan by running:

cmdstanr::install_cmdstan()

If installation succeeds, you should see a message like the following:

CmdStan path set to: {a path on your file system}

If you run into trouble, consult the official cmdstanr website for further installation guides and help.

Download this repository and install the project package (wwinference)

Once cmdstanr and CmdStan are installed, the next step is to download this repository and install the package, wwinference. The package provides tools for specifying and running the model, and installs other needed dependencies.

Once you have downloaded this repository, navigate to it within an R session and run the following:

install.packages('remotes')
remotes::install_local()

R dependencies

Installing the project package should take care of almost all dependencies installations. Confirm that package installation has succeeded by running the following within an R session:

library(wwinference)

Public Domain Standard Notice

This repository constitutes a work of the United States Government and is not subject to domestic copyright protection under 17 USC § 105. This repository is in the public domain within the United States, and copyright and related rights in the work worldwide are waived through the CC0 1.0 Universal public domain dedication. All contributions to this repository will be released under the CC0 dedication. By submitting a pull request you are agreeing to comply with this waiver of copyright interest.

License Standard Notice

The repository utilizes code licensed under the terms of the Apache Software License and therefore is licensed under ASL v2 or later.

This source code in this repository is free: you can redistribute it and/or modify it under the terms of the Apache Software License version 2, or (at your option) any later version.

This source code in this repository is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache Software License for more details.

You should have received a copy of the Apache Software License along with this program. If not, see http://www.apache.org/licenses/LICENSE-2.0.html

The source code forked from other open source projects will inherit its license.

Privacy Standard Notice

This repository contains only non-sensitive, publicly available data and information. All material and community participation is covered by the Disclaimer and Code of Conduct. For more information about CDC's privacy policy, please visit http://www.cdc.gov/other/privacy.html.

Contributing Standard Notice

Anyone is encouraged to contribute to the repository by forking and submitting a pull request. (If you are new to GitHub, you might start with a basic tutorial.) By contributing to this project, you grant a world-wide, royalty-free, perpetual, irrevocable, non-exclusive, transferable license to all users under the terms of the Apache Software License v2 or later.

All comments, messages, pull requests, and other submissions received through CDC including this GitHub page may be subject to applicable federal law, including but not limited to the Federal Records Act, and may be archived. Learn more at http://www.cdc.gov/other/privacy.html.

Records Management Standard Notice

This repository is not a source of government records, but is a copy to increase collaboration and collaborative potential. All government records will be published through the CDC web site.

Additional Standard Notices

Please refer to CDC's Template Repository for more information about contributing to this repository, public domain notices and disclaimers, and code of conduct.

ww-inference-model's People

Contributors

kaitejohnson avatar sswanikcdc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

ww-inference-model's Issues

Break up `generate_simulated_data.R` function into more modular functions

Goal

Currently one long linear function that steps through each component of the model to generate simulated hospital admissions and wastewater data. It is highly complex and hard to follow. It would be easier if each individual module called a function. That way, when you want to change how a module is handled, you can just write a new function for it.

Requirements

  • break down getting global and local infection dynamics, generating expected hospital admissions, and generating expected wastewater concentrations into separate modular functions
  • call these in the generate_simulated_data.R function

Consider constraining site-level R(t) deviations to sum to 0

Goal

Explore how this might work for the aspatial model, might improve identifiability

Context

Suggestion from @athowes to improve identifiability, in particular might be helpful when there are deviations that could be due to site/subpopulation level changes and also could be due to deviation in "central/global" $R(t)$
Suggests something like:
sum(subpop_level_errors[t]) ~ normal(0, <small_number> *<n_subpops>)

Requirements

  • Rmd to explore implementation of different model components and compare outputs

Out of scope for this PR

  • Idea of this PR is just to see how this impacts outputs/diagnostics/etc. Before adding this component to the model we would want to do some evaluation to ensure it improved performance (e.g. what we have in the pipeline repo for a single forecast date)

Add mermaid diagram to `model_definition.md`

Goal

Would be nice to have a little model diagram. Draft is here, but we need to figure out how to show that the infections from each subpopulation sum to the state infections.

The general flow of the model can be visualized as follows:

flowchart LR;
    GlobalR --> GlobalI;
    GlobalR --> LocalR;
    LocalR --> GlobalR;
    LocalR --> LocalIX;
    LocalR --> LocalIY;
    LocalIX --> LocalWWX;
    LocalIY --> LocalWWY;
    GlobalI --> Globalhosp;
Loading

Add an example of a pipeline with a config

Goal

We want to show an example of how someone would use this package in a production pipeline.

Requirements

  • a script that can be called from the command line with parsed args getting passed
  • an example config file containing those parsed args
  • a pre-processing script/set of functions from a common input dataset example
  • a post-processing script/set of functions

Add validation and checks for all input data

Goal

Want to make sure that the input data that a user provides for hospital admissions and wastewater datasets meets requirements needed for the model functions to work correctly. If not, we want to provide them with coherent error messaging to tell them how to modify it.

Requirements

Add hospital admissions only mode functionality

Goal

It is useful to compare the forecasts from the same model with and without the wastewater component included. Ideally, this doesn't require using the separate stan file and stan data object, but just turning off the wastewater component on the likelihood and making sure that the IHR is not time-varying

Requirements

  • implement a hospital admissions only renewal fit
  • demonstrate the fit to only hospital admissions data in the getting started vignette

Stan spatial functions.

Goal

We want to create STAN functions that are similar to the R functions for the spatial components of the model.

Context

The following model will be added as STAN functions :
Let $k$ represent a site, from sites $1$ to $n_k$. Let $R_k^u(t)$ be the unadjusted reproduction number at time $t$ at site $k$. So lets look at the combined "state" $R^u(t)$ that contains an AR(1) correlation structure on the first differences that is currently being used. So

$$ \log R^u(t) \sim \text{Normal}(\log R^u(t - 1) + \beta(\log R^u(t - 1) - \log R^u(t - 2)), \sigma_r), $$

or

$$ \log R^u(t) = \log R^u(t - 1) + \beta(\log R^u(t - 1) - \log R^u(t - 2)) + \sigma_r\text{Normal}(0,1). $$

Now let $\delta_k(t)$ be the time-varying subpopulation effect on $\log R^u(t)$. So we can look at the vector of all $\log R_k^u(t)$'s, so

$$ \begin{pmatrix} \log R_1^u(t)\\ \vdots\\ \log R_{n_{k}}^u(t)\\ \end{pmatrix} = \log R^u(t) \begin{pmatrix} 1\\ \vdots\\ 1\\ \end{pmatrix} + \vec{\delta}(t). $$

Where $\vec{\delta}(t) = \left(\delta_1(t),...,\delta_{n_k}(t)\right)^{T}$. We also have that $\vec{\delta}(t) = \varphi_{R(t)}\vec{\delta}(t - 1) + \vec{\epsilon}$, where $\vec{\epsilon}$ $\sim$ $\text{Normal}_{n_k}(0,\mathbf{\Sigma_{\epsilon}}).$

This adds a spatial correlation and also the current AR(1) temporal correlation to this $\vec{\delta}(\cdot)$ random variable. Like before, we have that $0 &lt; \varphi_{R(t)} &lt; 1$.

For $\mathbf{\Sigma_{\epsilon}}$ $=$ $\sigma_{\epsilon}^2\mathbf{\Omega}$, where $\Omega_{ij} \in \mathbf{\Omega}$ are build from the correlation functions. These include an exponential decay, Matern, spherical, rational quadratic, or, to return to the current model framework, independence correlation functions.

For simplicity, we will first assume an distance matrix is provided with the data or locations of sites where we can attain an distance matrix.

We need to make sure to take into account is the individuals that are not included in a site. So, currently, we will provide them with their own process. This separate process is as follows :

Lets denote the unadjusted reproduction number at time $t$ for individuals who are not represented by the actual sites as $R_{\text{aux}}^u(t)$.

$\log R_{\text{aux}}^u(t) = \log R^u(t) + \delta_{\text{aux}}(t),$

$\delta_{\text{aux}}(t) = \phi_{R(t)}\delta_{\text{aux}}(t - 1) + \epsilon_{\text{aux}},$

$\epsilon_{\text{aux}} \sim \text{Normal}(0,\text{ScalingFactor}\times\sigma_{\epsilon}),$

This process is very similar with the previous way site level Rt was calculated, but here we include the ScalingFactor to scale the variation of the spatial process. Also, if we have an independent structure, we will not have this ScalingFactor and will instead use the $n_k + 1$ approach currently being implemented.

Requirements

  • Add spatial_rt_process.stan
  • Add independence_corr_func.stan
  • Add exponential_corr_func.stan
  • Add aux_site_process.stan

Updating package data in spatial-ww branch.

Goal

We want to use the spatial process that was recently added into the spatial-ww branch to update the package data that is used in the vignette.

Requirements

  • Update data using the vignette_data.R file

Forward simulation spatially correlated unadjusted reproduction number

Goal

We want to update the generate_simulated_data.R function in the R folder to implement a spatially correlated unadjusted reproduction number into it. We will also add necessary functions to allow different correlation structures to be used.

Context

The current model used in the generate_simulated_data.R function is listed in the model definitions in the repo. The change we are making will be at the unadjusted reproduction level. We will add the spatial correlations here with the following model :

Let $k$ represent a site, from sites $1$ to $n_k$. Let $R_k^u(t)$ be the unadjusted reproduction number at time $t$ at site $k$. So lets look at the combined "state" $R^u(t)$ that contains an AR(1) correlation structure on the first differences that is currently being used. So

$$ \log R^u(t) \sim \text{Normal}(\log R^u(t - 1) + \beta(\log R^u(t - 1) - \log R^u(t - 2)), \sigma_r), $$

or

$$ \log R^u(t) = \log R^u(t - 1) + \beta(\log R^u(t - 1) - \log R^u(t - 2)) + \sigma_r\text{Normal}(0,1). $$

Now let $\delta_k(t)$ be the time-varying subpopulation effect on $\log R^u(t)$. So we can look at the vector of all $\log R_k^u(t)$'s, so

$$ \begin{pmatrix} \log R_1^u(t)\\ \vdots\\ \log R_{n_{k}}^u(t)\\ \end{pmatrix} = \log R^u(t) \begin{pmatrix} 1\\ \vdots\\ 1\\ \end{pmatrix} + \vec{\delta}(t). $$

Where $\vec{\delta}(t) = \left(\delta_1(t),...,\delta_{n_k}(t)\right)^{T}$. We also have that $\vec{\delta}(t) = \varphi_{R(t)}\vec{\delta}(t - 1) + \vec{\epsilon}$, where $\vec{\epsilon}$ $\sim$ $\text{Normal}_{n_k}(0,\mathbf{\Sigma_{\epsilon}}).$

This adds a spatial correlation and also the current AR(1) temporal correlation to this $\vec{\delta}(\cdot)$ random variable. Like before, we have that $0 &lt; \varphi_{R(t)} &lt; 1$.

For $\mathbf{\Sigma_{\epsilon}}$ $=$ $\sigma_{\epsilon}^2\mathbf{\Omega}$, where $\Omega_{ij} \in \mathbf{\Omega}$ are build from the correlation functions. These include an exponential decay, Matern, spherical, rational quadratic, or, to return to the current model framework, independence correlation functions.

For simplicity, we will first assume an adjacency matrix is provided with the data or locations of sites where we can attain an adjacency matrix.

We need to make sure to take into account is the individuals that are not included in a site. So, currently, we will provide them with their own process. This separate process is as follows :

Lets denote the unadjusted reproduction number at time $t$ for individuals who are not represented by the actual sites as $R_{\text{aux}}^u(t)$.

$\log R_{\text{aux}}^u(t) = \log R^u(t) + \delta_{\text{aux}}(t),$

$\delta_{\text{aux}}(t) = \phi_{R(t)}\delta_{\text{aux}}(t - 1) + \epsilon_{\text{aux}},$

$\epsilon_{\text{aux}} \sim \text{Normal}(0,\text{ScalingFactor}\times\sigma_{\epsilon}),$

$\text{ScalingFactor} \sim \text{Log-Normal}(\mu_{\text{ScalingFactor}},\sigma_{\text{ScalingFactor}}^2).$

This process is very similar with the previous way site level Rt was calculated, but here we include the ScalingFactor to scale the variation of the spatial process. Also, if we have an independent structure, we will not have this ScalingFactor and will instead use the $n_k + 1$ approach currently being implemented.

Requirements

  • Add function, named spatial_rt_process.R, to deal with the new spatial unadjusted reproduction number for a general correlation function.
  • Change generate_simulated_data.R for implementation of spatial unadjusted reproduction number, but will default to the independent unadjusted reproduction number.
  • Add R functions that create correlation matrices for the spatial_rt_process.R function.

Add testing for key functions

Goal

We created a bunch of new functions, and we should add a testing suite for them. The functions that need to be tested include:
preprocess_ww_data preprocess_hosp_data flag_ww_outliers, indicate_ww_exclusions in preprocessing.R get_draws_df in get_draws_df.R and the rest of the functions in utils.R.

Requirements

  • additional tests in tests folder for each of these functions

Port over model tests + add testing to package functions

Goal

We want to have a full testing suite for all of the functions in the wwinference package. Some of these, particularly for the stan model, are already written in https://github.com/CDCgov/wastewater-informed-covid-forecasting/tree/prod, and so we will want to port them over, and we also want to make new tests.

Requirements:

  • move over/modify if needed all existing tests
  • add tests for new functions e.g. pre and post processing --> made a new issue for this #27

Meta: v0.1

Goal

Tracking steps to a MVP modeling package. The goal is a minimum package that we can share with internal and external partners wanting to run the model on their own data and/or set up production pipelines.

Features

  • R package infrastructure #1
  • CI set up for testing suite
  • Minimum pre-processing and model fitting wrapper in a "getting started" vignette #5
  • Validate input data #4
  • Port over existing model tests
  • Add new tests for functions #27
  • Make sure every exported function has complete documentation
  • #3
  • Minimum post-processing to make it easy for the user to access: posterior draws of global $R(t)$, site-level $R(t)$, hospital admissions calibrated and forecasted, wastewater concentrations calibrated and forecasted #5
  • Updated README #7
  • Updated model definition #7

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.