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
@adamkucharski
adamkucharski / epinow2_infection.R
Last active February 26, 2024 14:18
Simulated infection recovery
# Example code for simulation recovery of infection dynamics from deaths for COVID -----------------------------------------
# Load libraries:
# install.packages("EpiNow2", repos = "https://epiforecasts.r-universe.dev")
library(EpiNow2)
# Simulate data and delays -----------------------------------------
# Simulate infections with a sharp drop
@sbfnk
sbfnk / weekly_forecasts.r
Created October 12, 2023 09:48
Weekly forecast plotting with EpiNow2
#' Aggregate daily forecasts to weekly forecasts
#'
#' Aggregate daily case forecasts from regional_epinow2 to weekly forecasts.
#'
#' @param inf List output from `EpiNow2::regional_epinow()`. No default.
#' @param week_start Week start day. Default is 1 (Monday).
#' @param summary_quantiles Quantiles for forecast summary. Default is median, plus upper and lower bound for 20\%, 50\% and 90\% forecasts.
#' @importFrom dplyr group_by summarise filter mutate select case_when contains
#' @importFrom tidyr pivot_wider
#' @importFrom purrr map_df
@zsusswein
zsusswein / convolve.stan
Last active October 2, 2023 08:41
Convolve two sequences in Stan with the FFT. Matches `convolve(x, rev(y), type = "open")` in R.
functions {
vector convolve(vector a, vector b) {
int na = num_elements(a); // Vector lengths
int nb = num_elements(b);
int n_zero_a = nb - 1; // Zero padding lengths
int n_zero_b = na - 1;
vector[nb] b_rev = reverse(b); // The reversed b vector
vector[na + n_zero_a] a_pad; // Instantiate zero padded vectors
@swo
swo / polars_cheat_sheet.py
Last active December 6, 2023 03:24
Polars cheat sheet
import polars as pl
# Making data frames ------------------------------------------
# with dictionary
df = pl.DataFrame({
'name': ['foo', 'bar', 'baz'],
'bar': [0, 1, 2],
'qux': [0.0, 1.0, 2.0]
})
wd <- system("echo $(git rev-parse --show-toplevel)", intern = TRUE)
setwd(wd)
source(paste0(wd, "/influenza/scripts/depends.R"))
output_dir <- fs::path(paste0(wd, "/influenza/papers/univariate/outputs"))
# nhs_data path for getting population sizes:
nhs_data_path <- "" # Removed path to influenza data + population catchments per trust
# Purpose: This is just a play model to think about time spent on large and big grants/opportunities and
# overall likelihood of a successful result (undefined, just general feel).
# 2080 is the hours in a year if working 40 hours (because everyone does that...)
using JuMP
using Ipopt
model = Model(Ipopt.Optimizer)
p1 = .05 # Probability of winning grant 1
sx1 = .4 # Probability of successful work (class 1)
##' Get estimates shown at \url{https://epiforecasts.io/covid} for all times
##'
##' Nowcasts at \url{https://epiforecasts.covid} and the related github repository at
##' \url{https://github.com/epiforecasts/covid-rt-estimates} only cover the last 3
##' months. This function downloads all available estimates and applies a median
##' average to the provided quantiles to provide an estimate of a time series
##' covering all times available
##'
##' @param dataset character; data set corresponding to directories at
##' \url{https://github.com/epiforecasts/covid-rt-estimates}. Default is
@sbfnk
sbfnk / get_utla_estimates.r
Created June 17, 2021 11:32
get all UTLA-level Rt estimates
library("gh")
library("vroom")
owner <- "epiforecasts"
repo <- "covid-rt-estimates"
path <- "subnational/united-kingdom-local/cases/summary/rt.csv"
rt_commits <-
gh("/repos/{owner}/{repo}/commits?path={path}",
owner = owner,
@sharlagelfand
sharlagelfand / labeller
Created March 26, 2021 16:00
Demonstrating the usage of the argument `labeller = label_wrap_gen()` in the ggplot2 function facet_wrap()
library(ggplot2)
library(tibble)
df <- tribble(
~x, ~y, ~label,
1, 2, "A super long label oh god how am I going to deal with this",
2, 1, "A shorter one"
)
# Default - text is cut off
@jarvisc1
jarvisc1 / aa_plot_continent.r
Last active May 26, 2020 16:28
Plot range of Rt for continents
library(data.table)
library(ggplot2)
cases <- fread('_nowcasts/covid-global/national-summary/cases.csv')
df <- fread('_nowcasts/covid-global/national-summary/rt.csv')
continents <- fread('_nowcasts/covid-global/data/continents_cnty.csv')
cases <- cases[, .(country, date, cases = median)]
dfcont <- merge(df, continents, by.x = "country", by.y = "country_name", all.x = TRUE)
dfcont <- merge(dfcont, cases, by = c("country", "date"), all.x = TRUE)