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 / 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 / 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 / covidregionaldata-0.9.1-overview.R
Created May 7, 2021 14:50
An overview of the features included in the 0.9.1 release of covidregionaldata. See the release notes for more details: https://github.com/epiforecasts/covidregionaldata/releases/tag/v0.9.1
library(covidregionaldata)
# set up a data cache
start_using_memoise()
#> Using a cache at: /tmp/RtmphiSeWY
# check for supported countries
get_available_datasets()
#> # A tibble: 18 x 8
#> origin class level_1_region level_2_region level_3_region type data_urls
@seabbs
seabbs / process-rt.R
Created March 9, 2021 11:18
Example script for processing samples.
# Packages ----------------------------------------------------------------
require(data.table, quietly = TRUE)
require(EpiNow2, quietly = TRUE)
require(purrr)
require(ggplot2)
# Target date -------------------------------------------------------------
creation_date <- Sys.Date()
extraction_date <- creation_date
@seabbs
seabbs / covid19-utla-rt-from-admissions.R
Last active June 9, 2021 12:17
R code using {covid19.nhs.data} and estimates from epiforecasts.io/covid to generate a gif of the effective reproduction for Covid-19 using hospital admissions by upper-tier local authority in England.
# Packages ----------------------------------------------------------------
library(covid19.nhs.data)
library(vroom)
library(dplyr)
library(tidyr)
library(lubridate)
library(gganimate)
#devtools::install_github("thomasp85/transformr")
library(transformr)
library(gifski)
@seabbs
seabbs / covid19-utla-admissions.R
Last active January 25, 2021 14:52
R code using {covid19.nhs.data} to generate a gif of Covid-19 hospital admissions byupper-tier local authority in England.
# Packages ----------------------------------------------------------------
library(covid19.nhs.data)
library(dplyr)
library(tidyr)
library(lubridate)
library(gganimate)
#devtools::install_github("thomasp85/transformr")
library(transformr)
library(gifski)
library(ggplot2)
@seabbs
seabbs / covid19-ltla-admissions.R
Last active January 25, 2021 14:53
R code using {covid19.nhs.data} to generate a gif of weekly Covid-19 hospital admissions by lower-tier local authority in England.
# Packages ----------------------------------------------------------------
library(covid19.nhs.data)
library(dplyr)
library(tidyr)
library(lubridate)
library(gganimate)
#devtools::install_github("thomasp85/transformr")
library(transformr)
library(gifski)
library(ggplot2)
@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"