Skip to content

Instantly share code, notes, and snippets.

@adamkucharski
adamkucharski / serology_roc.R
Created February 29, 2024 09:10
Ratio vs subtraction method for seropositivity
# Define parameters
bkg_val <- 15 # background value
xx <- 1e3 # Number of values to simulate from positive and negative distribution
xx_pos <- xx; xx_neg <- xx
set.seed(1)
# Set up plot
par(mfcol=c(2,2),mgp=c(2,0.7,0),mar = c(3,3,1,3))
@adamkucharski
adamkucharski / vaccination_antigenic_mismatch.R
Created February 8, 2024 13:32
VE calculation when vaccine is antigenically different to circulating infection
# Analysis of VE during epidemic when vaccine protection < infection protection -------------------------------------------------------------
# Adam Kucharski (2024)
# Define assumptions about exposure over time in epidemic -----------------
xx <- seq(0,6,1/30) # time points for model
peak_time <- 3 # peak in months post-vaccine campaign
peak_width <- 0.8 # variance in epidemic width (i.e. sd of distribution)
exposure_prop <- 0.6 # proportion of population exposure during wave
Banishment Traitors Correct Total
Nicky 3 0 19
Imran 3 0 17
Ivan 3 0 16
Tom 3 0 14
Alyssa 2 1 14
Rayan 2 0 13
Theo 2 0 10
Amanda 2 1 8
Maddy 3 0 6
@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
@adamkucharski
adamkucharski / 2024_01_delay_PCR.R
Last active January 2, 2024 12:26
Illustrative plots for PCR positivity
# - - - - - - - - - - - - - - - - - - - - - - -
# Code by Adam Kucharski to accompany post 'Counting current COVID infections' (2nd Jan 2024)
# Post is available at: https://kucharski.substack.com
# Code in this file is shared under an MIT license (https://opensource.org/license/mit/)
# - - - - - - - - - - - - - - - - - - - - - - -
# Load libraries
library(dplyr)
library(readr)
# Calculate final size using {finalsize}
library(finalsize)
xx <- seq(1,4,0.1) # range of R0 values
finalsize_calc <- sapply(xx,function(x){finalsize::final_size(r0=x,matrix(1),1,matrix(1),matrix(1))$p_infected}) # simple final size model
# Plot results
par(mfrow=c(1,1),mar=c(4,4,1,1),las=0)
plot(xx,finalsize_calc, ylim=c(0,1),lwd=2, col="dark blue",type="l", main="", yaxs="i",xaxs="i", yaxt="n",xlab=expression(R[0]), ylab="Percentage infected",bty="l")
axis(2, at=seq(0,1,0.1),labels = paste0(seq(0,100,10),"%"),col = "black")
@adamkucharski
adamkucharski / final_size_prompt.md
Last active June 7, 2023 12:20
Custom GPT prompt for final size

You are R software teacher specialising in epidemic scenario modelling. Use the below example code for the R package 'finalsize' as a basis for your response to the user question at the end.

Use case: There is substantial heterogeneity in contact patterns in a population. We want to know how this heterogeneity could affect the final size of the epidemic. Example code:

# load finalsize
library(finalsize)

# load necessary packages
if (!require("socialmixr")) install.packages("socialmixr")
@adamkucharski
adamkucharski / serofoi_fiji.R
Last active April 17, 2023 23:38
Application of serofoi package to Fiji dengue seroprevalence data
# serofoi analysis on Fiji serosurvey data
# Load libraries
library(remotes)
remotes::install_github("TRACE-LAC/serofoi")
library(serofoi)
library(rio)
# Load Fiji dengue data
url_fiji <- "https://raw.githubusercontent.com/adamkucharski/fiji-denv3-2014/master/data/serology_inputs.csv"
@adamkucharski
adamkucharski / cluster_size_estimation
Created January 10, 2023 10:21
Estimate R and k from clusters sizes using MLE
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Code to estimate R and k from clusters sizes using MLE via quickfit
# Adam Kucharski (2023)
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
library(devtools)
library(rstan)
library(MASS)
#devtools::install_github("adamkucharski/quickfit")
@adamkucharski
adamkucharski / finalsize_contact.R
Created January 5, 2023 13:01
Final size attack rate simulation from contact data
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Illustrative code to general finalsize model outputs from contact matrix
# Adapted from: https://epiverse-trace.github.io/finalsize/articles/finalsize.html
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
library(devtools)
install.packages("finalsize")
library(finalsize)
library(rio)
library(tidyverse)