Continuous-time stochastic simulation of epidemics in R

Similar documents
Package adaptivetau. October 25, 2016

Estimating R 0 : Solutions

INTRODUCTION TO R. Basic Graphics

Modeling of Complex Social. MATH 800 Fall 2011

Estimating HIV transmission rates with rcolgem

Statistical Programming with R

PharmaSUG China. Compartmental Models in SAS: Application to Model Epidemics Ka Chun Chong and Benny Chung-Ying Zee

Package visualizationtools

Matrix algebra. Basics

Practice for Learning R and Learning Latex

Plotting: An Iterative Process

Package MmgraphR. R topics documented: September 14, Type Package

Package SimInf. November 20, 2018

How Infections Spread on Networks CS 523: Complex Adaptive Systems Assignment 4: Due Dec. 2, :00 pm

STATISTICAL LABORATORY, April 30th, 2010 BIVARIATE PROBABILITY DISTRIBUTIONS

CSSS 510: Lab 2. Introduction to Maximum Likelihood Estimation

Package DPBBM. September 29, 2016

The nor1mix Package. August 3, 2006

Package TPD. June 14, 2018

Package hybridmodels

Curve fitting. Lab. Formulation. Truncation Error Round-off. Measurement. Good data. Not as good data. Least squares polynomials.

The nor1mix Package. June 12, 2007

The Coral Project: Defending against Large-scale Attacks on the Internet. Chenxi Wang

SISMID Spatial Statistics in Epidemiology and Public Health 2017 R Notes: Infectious Disease Data

Extremely short introduction to R Jean-Yves Sgro Feb 20, 2018

SISMID Module 11 Lab 1: EpiFire Tutorial

UP School of Statistics Student Council Education and Research

Package RCEIM. April 3, 2017

MCMC for Bayesian Inference Metropolis: Solutions

Epidemic spreading on networks

Risk Management Using R, SoSe 2013

PRINCIPLES OF PHYLOGENETICS Spring 2008 Updated by Nick Matzke. Lab 11: MrBayes Lab

Package beast. March 16, 2018

FAST ESTIMATION OF TIME-VARYING TRANSMISSION RATES

Package SCBmeanfd. December 27, 2016

Chapter 1. Introduction

Package texteffect. November 27, 2017

ddhazard Diagnostics Benjamin Christoffersen

R is a programming language of a higher-level Constantly increasing amount of packages (new research) Free of charge Website:

Package simed. November 27, 2017

An Introduction to R Graphics with examples

Package gibbs.met documentation

Lecture 6 - Multivariate numerical optimization

AGENT-BASED MODELING BOOTCAMP FOR HEALTH RESEARCHERS AUGUST 2012 A SIMPLE NETWORK-BASED INFECTION SPREAD AGENT-BASED MODEL

Univariate Data - 2. Numeric Summaries

A Handbook of Statistical Analyses Using R. Brian S. Everitt and Torsten Hothorn

Package areaplot. October 18, 2017

A recursive point process model for infectious diseases.

Package mixphm. July 23, 2015

Package DBKGrad. R topics documented: December 2, 2018

Univariate Data - 2. Numeric Summaries

Vital Dynamic NMG. Department of Statistics, University of Washington, Seattle, November 10, 2013

Massachusetts Institute of Technology Sloan School of Management. Understanding Epidemics Using. VensimPLE. For use with VensimPLE, version 6.

Fathom Dynamic Data TM Version 2 Specifications

Data mining --- mining graphs

Assignment 4: CS Machine Learning

Package bayescl. April 14, 2017

Name: Dr. Fritz Wilhelm Lab 1, Presentation of lab reports Page # 1 of 7 5/17/2012 Physics 120 Section: ####

Accelerated Life Testing Module Accelerated Life Testing - Overview

S4C03, HW2: model formulas, contrasts, ggplot2, and basic GLMs

Computation for the Introduction to MCMC Chapter of Handbook of Markov Chain Monte Carlo By Charles J. Geyer Technical Report No.

Data Visualization. Andrew Jaffe Instructor

Guide- Using GLEAMviz

Package SHELF. August 16, 2018

Package libstabler. June 1, 2017

Using Built-in Plotting Functions

IST 3108 Data Analysis and Graphics Using R Week 9

Vaxx Project: Source Code for the Benchmark Experiments (1 and 2)

Modeling Plant Succession with Markov Matrices

Algorithmic Problems in Epidemiology

Package gibbs.met. February 19, 2015

Package sp23design. R topics documented: February 20, Type Package

Package CLA. February 6, 2018

The coupling effect on VRTP of SIR epidemics in Scale- Free Networks

Package intccr. September 12, 2017

Math Modeling in Java: An S-I Compartment Model

L Modeling and Simulating Social Systems with MATLAB

Package coenocliner. R topics documented: August 29, Type Package Title Coenocline Simulation Version Date

Package ibm. August 29, 2016

Package distrteach. R topics documented: January 20, Version 2.3 Date

Models and Algorithms for Network Immunization

Package hbm. February 20, 2015

Scicos is a Scilab toolbox included in the ScicosLab package. The Scicos editor can be opened by the scicos command

Math 408R: UT Fall 2016

Package Mondrian. R topics documented: March 4, Type Package

jackstraw: Statistical Inference using Latent Variables

Package CINID. February 19, 2015

B Y P A S S R D E G R Version 1.0.2

Package kofnga. November 24, 2015

## In the comments below, cp refers to the change point, w/ is "with", w/o is "without".

Applied Calculus. Lab 1: An Introduction to R

Data organization. So what kind of data did we collect?

Chapter 5 An Introduction to Basic Plotting Tools

Package SemiMarkov. January 19, 2016

R Programming: Worksheet 6

Package SEL. February 19, 2015

Consistency and Replication. Why replicate?

Chapter 2: The Normal Distributions

Package nltt. October 12, 2016

Learning Network Graph of SIR Epidemic Cascades Using Minimal Hitting Set based Approach

Transcription:

Continuous-time stochastic simulation of epidemics in R Ben Bolker May 16, 2005 1 Introduction/basic code Using the Gillespie algorithm, which assumes that all the possible events that can occur (death of an individual, birth, infection, etc.) occur independently and (given the current state of the system number of infectives, susceptibles, etc.) with constant probability per unit time. Given that this is true, we can pick an exponential deviate (using R s rexp() function) that tells us how long it is until the next event occurs, and then pick from among the possible transitions with probabilities proportional to their individual rates (sample()). Set the random-number seed so that results are reproducible every time we run this code (you can really use any integer you want here). > set.seed(1001) An R function that implements the Gillespie algorithm. specify start: starting values The user must ratefun: a function of the current state variables, parameters, and time that returns a numeric vector of the rates (probabilities per unit time) at which each kind of event is occurring trans: a matrix indicating the changes in each state variable (column) that occur when a particular event (row) takes place pars: a (named) numeric vector of parametesr times: a vector of times at which to report output 2 Simple SIR Defining the appropriate inputs for a simple SIR model with no vital dynamics (nor any other complications). Defining names for the state variables and transitions isn t absolutely necessary, but will make your code much easier to read later on:n 1

> statenames.sir <- c("s", "I", "R") > transnames.sir <- c("infection", "death", "recovery") Define the matrix of transitions (not the same as the transition matrix of a Markov chain) and the function : The transition matrix ends up looking like this: > trans.sir S I R infection -1 1 0 death 0-1 0 recovery 0-1 1 Define parameters (numeric vector with names): > pars.sir <- c(beta = 0.1, alpha = 1, gamma = 1) (R 0 in this case is equal to βn/(α + γ); if N = 100 then R 0 = 5). Run stochastic simulation: > G.SIR <- gillesp(start = c(s = 97, I = 3, R = 0), times = seq(0, + 5, by = 0.05), ratefun = ratefun.sir, trans = trans.sir, + pars = pars.sir) Plot it (matplot plots the columns of a matrix as separate lines against a single x variable. type="l" specifies lines, lty=1 specifies solid lines, xlab and ylab specify x- and y-axis labels. G.SIR[,"times"] picks out the column labeled times, G.SIR[,-1] picks out all but the first column). > matplot(x = G.SIR[, "times"], y = G.SIR[, -1], type = "l", lty = 1, + xlab = "Time", ylab = "Number") 2

Number 0 20 40 60 80 100 0 1 2 3 4 5 Time Fancy: use replicate run 100 stochastic simulations, using [,"I"] to save just the number of infectives... > G.SIR.mult <- replicate(100, gillesp(start = c(s = 100, I = 3, + R = 0), times = seq(0, 5, by = 0.05), ratefun = ratefun.sir, + trans = trans.sir, pars = pars.sir)[, "I"]) Plot it, adding a line for the mean (lines adds a line to the existing plot, lwd=2 sets the line width to 2): > matplot(g.sir[, "times"], G.SIR.mult, type = "l", col = "gray", + lty = 1, xlab = "Time", ylab = "Number infective") > lines(g.sir[, "times"], rowmeans(g.sir.mult), lwd = 2) 3

Number infective 0 10 20 30 40 50 60 70 0 1 2 3 4 5 Time 3 SIR with vital dynamics (constant population size) > ratefun.vsir <- function(x, pars, time) { + vals <- c(as.list(pars), as.list(x)) + rates <- with(vals, c(birth = mu * K, infection = beta * + S * I, Sdeath = (mu * S), Ideath = (alpha + mu) * I, + Rdeath = (mu * R), recovery = gamma * I)) + } > statenames.vsir <- statenames.sir > transnames.vsir <- c("birth", "infection", "Sdeath", "Ideath", + "Rdeath", "recovery") > trans.vsir <- matrix(c(1, 0, 0, -1, 1, 0, -1, 0, 0, 0, -1, 0, + 0, 0, -1, 0, -1, 1), byrow = TRUE, ncol = 3, dimnames = list(transnames.vsir, + statenames.vsir)) Here I m going to make β quite a bit larger (R 0 = βn/(α+γ+µ) = 150/2.2 = 68) (!), in order to avoid having the epidemic go extinct during the crash after the first epidemic peak. There is a general epidemiological puzzle here about how new epidemics get started from low numbers without going extinct in the first epidemic trough... 4

> pars.vsir <- c(beta = 1.5, alpha = 1, gamma = 1, mu = 0.2, K = 100) Run it: > G.vSIR <- gillesp(start = c(s = 100, I = 3, R = 0), times = seq(0, + 50, by = 0.05), ratefun = ratefun.vsir, trans = trans.vsir, + pars = pars.vsir) Plot it: > matplot(g.vsir[, "times"], G.vSIR[, -1], type = "l", lty = 1, + xlab = "Time", ylab = "Number") Number 0 20 40 60 80 100 0 10 20 30 40 50 Time 4 Extensions vertical transmission: split birth into Sbirth and Ibirth vaccination: e.g. > rates <- c(sbirth = ifelse(time < vaccstart, b * N, (1 - vaccrate) * + b * N), Rbirth = ifelse(time < vaccstart, 0, vaccrate * b * + N)) waning immunity 5

multiple infected classes to simulate gamma/non-exponential infectious periods exposed class density-dependent birth/death rates density-dependent disease-induced mortality??? seasonal variation in rates: e.g. sinusoidal > beta.seas <- beta.0 * (1 + beta.ampl * cos(2 * pi * time)) or step-function: > beta <- ifelse(time%%1 < seas.frac, beta.0, 0) multiple classes of individuals with differential mixing/susceptibility/etc. (S1,I1,R1,S2,I2,R2, etc., although this can get tedious) 5 Other challenges Write odesolve/lsoda code to check against these results... 6