Skip to content

Instantly share code, notes, and snippets.

View seabbs's full-sized avatar
🏠
Working from home

Sam Abbott seabbs

🏠
Working from home
View GitHub Profile
@seabbs
seabbs / regional-secondary.R
Last active December 14, 2023 19:00
Prototype function for forecasting a secondary observation from a primary observation across multiple regions. An example application of this function can be found here: https://github.com/epiforecasts/covid-german-forecasts/blob/master/rt-forecast/update-death-from-cases.R. See the documentation of EpiNow2 for more details: https://epiforecasts…
# load required packages
library(EpiNow2)
library(future.apply)
library(purrr)
library(data.table)
warning("This gist is depreciated. Please use the following development repository: https://github.com/seabbs/regional-secondary.git")
# extract priors from a posterior and update fitting args
extract_secondary_priors <- function(posterior) {
@seabbs
seabbs / forecast-covid-deaths-from-covid-cases.R
Last active November 14, 2023 18:28
Example of using EpiNow2 to forecast Covid-19 deaths from Covid-19 cases (both observed and forecast) for a country in the ECDC dataset. See the documentation for more details: https://epiforecasts.io/EpiNow2/dev/
# set number of cores to use fitting the model
# no benefit on runtime if cores > chains which is set to 4 by default
options(mc.cores = 4)
# Packages ----------------------------------------------------------------
# install.packages(c("data.table", "remotes", "ggplot2"))
# remotes::install_github("epiforecasts/EpiNow2")
# remotes::install_github("epiforecasts/covidregionaldata")
library(data.table)
library(ggplot2)
@seabbs
seabbs / latent_Re_custom_prior.R
Created October 24, 2023 10:45
Latent reproduction number estimation with a joint nowcast and custom prior specification for the expectation model.
# Load packages
library(epinowcast)
library(data.table)
library(ggplot2)
# Load and filter germany hospitalisations
nat_germany_hosp <- germany_covid19_hosp[location == "DE"][age_group %in% "00+"]
nat_germany_hosp <- enw_filter_report_dates(
nat_germany_hosp,
latest_date = "2021-10-01"
@seabbs
seabbs / nginx-conf
Created August 8, 2018 21:26
Nginx conf generated by nginx auto proxy
# If we receive X-Forwarded-Proto, pass it through; otherwise, pass along the
# scheme used to connect to this server
map $http_x_forwarded_proto $proxy_x_forwarded_proto {
default $http_x_forwarded_proto;
'' $scheme;
}
# If we receive X-Forwarded-Port, pass it through; otherwise, pass along the
# server port the client connected to
map $http_x_forwarded_port $proxy_x_forwarded_port {
default $http_x_forwarded_port;
@seabbs
seabbs / pomp-tb-model
Created August 8, 2019 11:05
An example POMP TB transmission model
#' Disease Model, with Vaccination, Data Based Population Demographics and Ageing as a Pomp Object
#'
#' @param df A data frame of data to be passed to the pomp model.
#' @param t0 An integer value denoting the starting time for the model, this may be negative.
#' @param nstageA The number of age compartments to model.
#' @param timestep Value indicating the timestep over which to solve the model.
#' @param time The variable to use the time parameter from the data.
#' @param covars Additional data to use within the model.
#' @param tcovar A character string indicating the time variable in covar.
#' @param obsnames A character string of the observable variables.
@seabbs
seabbs / epinow2-covid-rt-national.R
Last active June 25, 2023 08:02
Example of using EpiNow2 to estimate the Rt of Covid-19 in last 3 months for a country in the WHO dataset. See the documentation for more details and examples of producing estimates for subregional areas: https://epiforecasts.io/EpiNow2/
# packages
# install.packages(c("data.table", "remotes", "EpiNow2"))
library(data.table)
library(EpiNow2)
library(covidregionaldata)
# target country (must be present in ECDC data)
country <- "france"
# set number of cores to use fitting the model
@seabbs
seabbs / interveral-censored-right-truncated-distribution-estimation-with-brms.R
Last active October 27, 2022 16:45
This gist shows how to estimate a doubly censored (i.e daily data) and right truncated (i.e due to epidemic phase) distribution using the brms package.
# Load packages
library(brms)
library(cmdstanr)
library(data.table) # here we use the development version of data.table install it with data.table::update_dev_pkg
library(purrr)
# Set up parallel cores
options(mc.cores = 4)
# Simulate some truncated and truncation data
functions{
#include functions/ct_trajectory.stan
#include functions/truncated_normal_rng.stan
#include functions/censor.stan
}
data {
int P; // number of patients
int N; // number of tests
real c_lod; // Ct value at limit of detection
@seabbs
seabbs / deterministic-deconvolution.R
Last active February 7, 2022 16:02
A script containing the functions required to simulation infections from a Poisson process, calculate prevalence assuming a deterministic convolution with a vector of detection probabilities, deconvolve prevalence to infections by numerically estimating the inverse of the convolution matrix, and then plotting a simulation of this process. *Note …
library(data.table)
library(snakecase)
library(ggplot2)
# make a convolution matrix
convolution_matrix <- function(vec1, vec2) {
lvec1 <- length(vec1)
lvec2 <- length(vec2)
conv <- matrix(0, nrow = lvec1, ncol = lvec1)
for (s in 1:lvec1) {
@seabbs
seabbs / epinow2-covid-rt-region.R
Last active November 30, 2021 13:22
Example of using EpiNow2 to estimate the Rt of Covid-19 in last 3 months for a region in a country supported in covidregionaldata. See the documentation for more details and examples: https://epiforecasts.io/EpiNow2/
# packages
# install.packages(c("data.table", "remotes", "EpiNow2"))
# remotes::install_github("epiforecasts/EpiNow2")
# remotes::install_github("epiforecasts/covidregionaldata")
library(data.table)
library(EpiNow2)
library(covidregionaldata)
# target country (must be supported in covidregionaldata)
country <- "uk" # harder to fit "india"