sir | R Documentation |
Simulates time series of the susceptible, infected, and removed population sizes and corresponding time series of births, incidence, and reported incidence. Simulations are based on an SIR model with user-defined forcing and a simple model for observation error.
sir(n, beta, nu, mu, constants, stochastic = TRUE,
prob = 1, delay = 1, useCompiled = TRUE, ...)
n |
a positive integer. The simulation uses |
beta , nu , mu |
functions of one or more argument returning transmission, birth, and natural death rates, respectively, at the time point indicated by the first argument. Arguments after the first must be strictly optional. The functions need not be vectorized. |
constants |
a numeric vector of the form
|
stochastic |
a logical indicating if the simulation should be stochastic; see ‘Details’. |
prob |
a numeric vector of length |
delay |
a numeric vector of positive length such that
|
useCompiled |
a logical indicating if derivatives should be
computed by compiled C functions rather than by R
functions (which may be byte-compiled). Set to
|
... |
optional arguments passed to
|
sir(stochastic = FALSE)
works by numerically integrating the
system of ordinary differential equations
\begin{aligned}
\frac{\text{d} S}{\text{d} t} &= \nu(t) - \beta(t) S I + \delta R - \mu(t) S \\
\frac{\text{d} I}{\text{d} t} &= \beta(t) S I - \gamma I - \mu(t) I \\
\frac{\text{d} R}{\text{d} t} &= \gamma I - \delta R - \mu(t) R
\end{aligned}
between times t = 0, \ldots, n
, where t
is understood
to be a unitless measure of time relative to the duration of an
observation interval. To generate time series of births and incidence
\begin{aligned}
B(t) &= \int_{t - 1}^{t} \nu(s)\,\text{d}s \\
Z(t) &= \int_{t - 1}^{t} \beta(s) S(s) I(s)\,\text{d}s
\end{aligned}
the system is augmented with two additional equations describing
cumulative incidence and cumulative births (with right
hand sides given by the integrands above), and the augmented
system with five equations is integrated. Case reports are simulated
by scaling incidence by prob
and convolving the result with
delay
.
sir(stochastic = TRUE)
works by simulating a Markov process
corresponding to the augmented system, as described in the reference.
Case reports are simulated from incidence by binning binomial samples
taken with probabilities prob
over future observation intervals
according to multinomial samples taken with probabilities delay
.
A “multiple time series” object, inheriting from class
mts
. Beneath the class,
it is an (n+1)
-by-(5+r)
numeric matrix X
,
where r
is 0 if both prob
and delay
are missing (indicating no observation error) and 1 otherwise.
Rows correspond to time points 0:n
. Columns are named
S
, I
, R
, B
, Z
, and (if r
is 1) Z.obs
. X[, 1:3]
give the state at each time,
and X[, 4:6]
give the number of births, infections, and
infections reported during the observation interval ending
at each time.
X[1, 4:6]
is NA
, and X[2:length(delay), 6]
can contain incomplete information if length(delay) >= 2
.
Cao, Y., Gillespie, D. T., & Petzold, L. R. (2007). Adaptive explicit-implicit tau-leaping method with automatic tau selection. Journal of Chemical Physics, 126(22), Article 224101, 1-9. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.1063/1.2745299")}
sir.library
.
beta <- function (t, a = 1e-01, b = 1e-05)
b * (1 + a * cospi(t / 26))
nu <- function (t) 1e+03
mu <- function (t) 1e-03
S0 <- 5e+04
I0 <- 1e+03
R0 <- 1e+06 - S0 - I0
constants <- c(S0 = S0, I0 = I0, R0 = R0, gamma = 0.5, delta = 0)
n <- 250L
prob <- 0.1
delay <- diff(pgamma(0:8, 2.5))
set.seed(0L)
X <- sir(n, beta, nu, mu, constants, prob = prob, delay = delay)
str(X)
plot(X)
r <- 10L
Y <- do.call(cbind, replicate(r, simplify = FALSE,
sir(n, beta, nu, mu, constants, prob = prob, delay = delay)[, "Z.obs"]))
str(Y) # FIXME: stats:::cbind.ts mangles dimnames
plot(Y, plot.type = "single", col = seq_len(r), ylab = "Case reports")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.