[R-sig-dyn-mod] ODE question
Thomas Petzoldt
Thomas.Petzoldt at tu-dresden.de
Fri Jul 2 18:25:40 CEST 2010
On 02.07.2010 06:25, Hill, Andrew N. wrote:
> Hello,
>
> I have a system of ODEs for an infectious disease model. It is a
> heterogeneous SEIR model, where each S, E, I, R type compartment has
> n strata. Depending on the situation, n is typically of the order of
> 10. My question is can I define the system of ODEs by some kind of
> vectorization and pass to deSolve to solve? Rather than type out the
> 10*4=40 differential equations, I would prefer to set it up as 4
> vectorized diff eqs for each S, E, I, R. How do I do this in R and
> specify 4 initial conditions consisting of vectors of length 10 then
> solve it with deSolve?
>
> I hope my question makes sense and thanks in advance.
>
> Andrew
Yes, of course, it makes sense!
Do you mean something like this:
##------------------------------------------------------------------------------
library(deSolve)
func <- function(t, y, p) {
S <- y[1:2]
I <- y[3:4]
R <- y[5:6]
dS <- - p$beta * S * I
dI <- p$beta * S *I - p$nu * I
dR <- p$nu * I
list(c(dS, dI, dR))
}
## note that parms can also be alist
parms <- list(beta=c(0.1, 0.2), nu=c(0.1, 0.1))
init <- c(S1 = 10, S2 = 8, I1 = 1, I2 = 2, R1 = 0, R2 = 0)
times <- seq(1, 10, 0.1)
out <- ode(init, times, func, parms)
plot(out)
##------------------------------------------------------------------------------
See also:
https://stat.ethz.ch/pipermail/r-help/2009-June/201212.html
for another example that uses an interaction matrix.
Hope it helps
Thomas Petzoldt
More information about the R-sig-dynamic-models
mailing list