Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sbfnk authored Nov 1, 2024
0 parents commit f9a2f80
Show file tree
Hide file tree
Showing 150 changed files with 9,310 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
^.*\.Rproj$
^\.Rproj\.user$
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.qmd linguist-language=RMarkdown
11 changes: 11 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates

version: 2
updates:
- package-ecosystem: "github-actions" # See documentation for possible values
directory: "/" # Location of package manifests
schedule:
interval: "weekly"
64 changes: 64 additions & 0 deletions .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Workflow derived from https://github.com/r-lib/actions/tree/master/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
workflow_dispatch:
schedule:
- cron: '0 0 1 * *' # Run at 00:00 on the 1st of every month
push:
branches: [main]
pull_request:
merge_group:

name: deploy

jobs:
deploy:
runs-on: ubuntu-latest
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
LANG: "en_US.UTF-8"
LC_ALL: "en_US.UTF-8"
steps:
- uses: actions/checkout@v4

- uses: quarto-dev/quarto-actions/setup@v2

- uses: r-lib/actions/setup-r@v2
with:
use-public-rspm: true
extra-repositories: 'https://stan-dev.r-universe.dev'

- name: Install dependencies
uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages:
any::gh,
any::knitr,
any::rmarkdown,
local::.,

- name: Install cmdstan
uses: epinowcast/actions/install-cmdstan@v1
with:
cmdstan-version: 'latest'
num-cores: 2

- name: Render site
run: quarto render

- name: Upload website
uses: actions/upload-artifact@v4
if: github.event_name == 'pull_request'
with:
name: site
retention-days: 5
path: _site

- name: Deploy to GitHub pages 🚀
if: github.ref_name == 'main'
uses: JamesIves/github-pages-deploy-action@v4.6.8
with:
branch: gh-pages
folder: _site
single-commit: true
clean: true
64 changes: 64 additions & 0 deletions .github/workflows/render-readme.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: render-readme

concurrency:
group: ${{ github.workflow }}-${{ github.head_ref }}
cancel-in-progress: true

on:
workflow_dispatch:
push:
branches:
- main

jobs:
render-readme:
runs-on: macos-latest
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
steps:
- name: Checkout repos
uses: actions/checkout@v4

- name: Setup R
uses: r-lib/actions/setup-r@v2
with:
use-public-rspm: true
extra-repositories: 'https://stan-dev.r-universe.dev'

- name: Setup pandoc
uses: r-lib/actions/setup-pandoc@v2

- name: Install dependencies
uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::rmarkdown, any::allcontributors

- name: Update contributors
if: github.ref == 'refs/heads/main'
run: allcontributors::add_contributors(format = "text", check_urls = FALSE)
shell: Rscript {0}

- name: Compile the readme
run: |
rmarkdown::render("README.Rmd")
shell: Rscript {0}

- name: Upload README.md as an artifact
uses: actions/upload-artifact@v4
with:
name: readme
path: README.md

- name: Create Pull Request
if: github.ref == 'refs/heads/main'
uses: peter-evans/create-pull-request@v7
with:
commit-message: "Automatic README update"
title: "Update README"
body: "This is an automated pull request to update the README."
branch: "update-readme-${{ github.run_number }}"
labels: "documentation"
add-paths: |
README.Rmd
README.md
token: ${{ secrets.GITHUB_TOKEN }}
16 changes: 16 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.Rproj.user
.Rhistory
.RData
.Ruserdata
_site
.DS_Store
*_files/
/.quarto/
inst/stan/simple-nowcast
inst/stan/*
!inst/stan/functions
!inst/stan/*.stan
*.html
*.rmarkdown
.vscode/
*pdf
Empty file added .nojekyll
Empty file.
42 changes: 42 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
Package: nfidd
Title: Material to support course on nowcasting and forecasting of infectious
disease dynamics
Version: 0.1.0.9000
Authors@R: c(
person(
given = "NFIDD course contributors",
family = "",
role = c("cre", "aut"),
email = "epiforecasts@gmail.com"
)
)
Description: Resources to support a short course on nowcasting and forecasting
of infectious disease dynamics.
License: MIT + file LICENSE
URL: https://github.com/nfidd/nfidd
BugReports: https://github.com/nfidd/nfidd/issues
Depends:
R (>= 4.2.0)
Imports:
dplyr,
tidyr
Suggests:
bayesplot,
cmdstanr,
ggplot2,
lubridate,
posterior,
purrr,
rmarkdown,
scoringutils,
tidybayes,
qra
Remotes:
epiforecasts/scoringutils@b370557,
epiforecasts/qra@v0.1.0
Additional_repositories:
https://stan-dev.r-universe.dev
Encoding: UTF-8
Language: en-GB
RoxygenNote: 7.3.2
LazyData: true
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 NFIDD contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
34 changes: 34 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Generated by roxygen2: do not edit by hand

export(add_delays)
export(censored_delay_pmf)
export(convolve_with_delay)
export(geometric_diff_ar)
export(geometric_random_walk)
export(make_daily_infections)
export(make_gen_time_pmf)
export(make_ip_pmf)
export(nfidd_cmdstan_model)
export(nfidd_load_stan_functions)
export(nfidd_stan_function_files)
export(nfidd_stan_functions)
export(nfidd_stan_models)
export(nfidd_stan_path)
export(renewal)
export(simulate_onsets)
importFrom(dplyr,count)
importFrom(dplyr,full_join)
importFrom(dplyr,if_else)
importFrom(dplyr,join_by)
importFrom(dplyr,left_join)
importFrom(dplyr,mutate)
importFrom(dplyr,n)
importFrom(dplyr,select)
importFrom(dplyr,tibble)
importFrom(dplyr,transmute)
importFrom(stats,rbinom)
importFrom(stats,rgamma)
importFrom(stats,rlnorm)
importFrom(stats,rpois)
importFrom(tidyr,expand)
importFrom(tidyr,replace_na)
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# nfidd 0.1.0.9000

In development version of the package and teaching material for teaching in Bangkok in November 2024.

# nfidd 0.1.0

Initial release of the `nfidd` package and teaching material for teaching in June 2024 in Stockholm.
37 changes: 37 additions & 0 deletions R/add_delays.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#' Simulate symptom onset and hospitalization times from infection times
#'
#' @param infection_times A data frame containing a column of infection times
#'
#' @return A data frame with columns for infection time, onset time, and
#' hospitalization time (with 70% of hospitalizations set to NA)
#'
#' @importFrom dplyr mutate n if_else
#' @importFrom stats rgamma rlnorm rbinom
#'
#' @export
#'
#' @examples
#' add_delays(infection_times)
add_delays <- function(infection_times) {
# Add random delays from appropriate probability distributions
df <- infection_times |>
mutate(
# Gamma distribution for onset times
onset_time = infection_time + rgamma(n(), shape = 5, rate = 1),
# Lognormal distribution for hospitalisation times
hosp_time = onset_time + rlnorm(n(), meanlog = 1.75, sdlog = 0.5)
)

# Set random 70% of hospitalization dates to NA (30% hospitalized)
df <- df |>
mutate(
hosp_time = if_else(
# use the binomial distribution for random binary outcomes
rbinom(n = n(), size = 1, prob = 0.3) == 1,
hosp_time,
NA_real_
)
)

return(df)
}
33 changes: 33 additions & 0 deletions R/censored-delay-pmf.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#' Discretise a Continuous Delay Distribution
#'
#' This function discretises a continuous delay distribution into a probability
#' mass function (PMF) over discrete days.
#'
#' @param rgen A function that generates random delays, e.g., `rgamma`,
#' `rlnorm`.
#' @param max The maximum delay.
#' @param n The number of replicates to simulate. Defaults to `1e+6`.
#' @param ... Additional parameters of the delay distribution.
#'
#' @return A vector of probabilities corresponding to discrete indices from
#' `0` to `max`, representing the discretised delay distribution.
#'
#' @export
#'
#' @examples
#' censored_delay_pmf(rgen = rgamma, max = 14, shape = 5, rate = 1)
censored_delay_pmf <- function(rgen, max, n = 1e+6, ...) {
## first, simulate exact time of first event (given by day), uniformly
## between 0 and 1
first <- runif(n, min = 0, max = 1)
## now, simulate the exact time of the second event
second <- first + rgen(n, ...)
## round down to get the delay in days
delay <- floor(second)
## get vector of counts
counts <- table(factor(delay, levels = seq(0, max)))
## normalise to get pmf
pmf <- counts / sum(counts)
## return
return(pmf)
}
30 changes: 30 additions & 0 deletions R/convolve-with-delay.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#' Convolve a Time Series with a Delay Distribution
#'
#' This function convolves a time series with a delay distribution given as a
#' probability mass function (PMF).
#'
#' @param ts A vector of the time series to convolve.
#' @param delay_pmf The probability mass function of the delay, given as a
#' vector of probabilities, corresponding to discrete indices 0, 1, 2 of the
#' discretised delay distribution.
#'
#' @return A vector of the convolved time series.
#'
#' @export
#'
#' @examples
#' convolve_with_delay(ts = c(10, 14, 10, 10), delay_pmf = c(0.1, 0.6, 0.3))
convolve_with_delay <- function(ts, delay_pmf) {
max_delay <- length(delay_pmf) - 1 ## subtract one because zero-indexed
convolved <- vapply(seq_along(ts), \(i) {
## get vector of infections over the possible window of the delay period
first_index <- max(1, i - max_delay)
ts_segment <- ts[seq(first_index, i)]
## take reverse of pmf and cut if needed
pmf <- rev(delay_pmf)[seq_len(i - first_index + 1)]
## convolve with delay distribution
ret <- sum(ts_segment * pmf)
return(ret)
}, numeric(1))
return(convolved)
}
Loading

0 comments on commit f9a2f80

Please sign in to comment.