Skip to content

[R-sig-dyn-mod] metapopulation model with random effect in parameters

2 messages · 胡艺, Thomas Petzoldt

#
Thomas and all, my further question is that how can I use the forcing
function as a vector? In my above coding, I would like to interpolate the
birth rate for each subpopulation but encounter a problem. I've come up
with a solution, that is, the same way just like we deal with signals in
beta (i.e., to define a external birth), but I'm wondering if I can use the
approxfun directly within the trans function. I ran the following code, but
it ends with "Error in lsoda(y, times, func, parms, ...) :   REAL() can
only be applied to a 'numeric', not a 'list'".

thanks

Yi

my updated coding is as follows:


require(deSolve)
n<-3
moverate = matrix(nr = n, nc = n, 0.08)
moverate[1,2] <- 0.05
moverate[1,3] <- 0.07
moverate[2,3] <- 0.06
diag(moverate) <- 0

b1<-approxfun(x=c(0,365),y=c(3,6),rule = 2)
b2<-approxfun(x=c(0,365),y=c(4,7),rule = 2)
b3<-approxfun(x=c(0,365),y=c(2,5),rule = 2)

e<-function(x) c(b1(x),b2(x),b3(x))

trans <- function(time, state, pars) {

  S <- state[1:n]
  E <- state[(n+1):(2*n)]
  I <- state[(2*n+1):(3*n)]
  R <- state[(3*n+1):(4*n)]
  N <- S + E + I + R

  birth <- pars[1:n]
  a0 <- pars[(n+1):(2*n)]
  alpha <- pars[2*n+1]
  gamma <- pars[2*n+2]

  *beta = a0+sigl[time+1,] *
*  birth = e*

  dS <- birth(time)-beta*S*I/N
  dE <- beta*S*I/N-E*alpha
  dI <- E*alpha-I*gamma
  dR <- I*gamma

  FluxS <- (S*moverate) # described as a matrix[i,j], flux of susceptibles
from subpop i to subpop j
  FluxE <- (E*moverate)
  FluxI <- (I*moverate) # described as a matrix[i,j], flux of infectives
from subpop i to subpop j
  FluxR <- (R*moverate)

  dS <- dS - (rowSums(FluxS) - colSums(FluxS))
  dE <- dE - (rowSums(FluxE) - colSums(FluxE))
  dI <- dI - (rowSums(FluxI) - colSums(FluxI))
  dR <- dR - (rowSums(FluxR) - colSums(FluxR))

  return(list (c(dS, dE, dI, dR),beta,birth=birth))

}

pars  <- c(birth   = c(5,4,6),    # birth rate
           a0  = runif(3,1,3),    # guess the contact rate
           alpha  = 1/4 ,   # incubation rate
           gamma = 1/8)    # recovery rate

yini  <- c(S = c(998,999,1000),
           E = c(1,0,0),
           I = c(1,1,0),
           R = c(0,0,0))

times <- seq(0, 365, by = 1)

sigl<-matrix(rnorm(n*366,sd=0.5),ncol = n)

out   <- ode(yini, times, trans, pars)
plot(out)
On Tue, Sep 25, 2018 at 10:18 AM ?? <stevenhuyi at gmail.com> wrote:

            

  
  
#
Hi,

Note that approxfun uses a special feature of R: It returns a function.

If you create:

b1<-approxfun(x=c(0,365),y=c(3,6),rule = 2)

then just use

b1(time)

in your model.


Please read the ?approxfun help page again. For examples how to use it 
with deSolve, have a look at:

http://limno-live.hydro.tu-dresden.de/user2017/

and in particular:

http://limno-live.hydro.tu-dresden.de/user2017/2.1-forcing+events/desolve-forcing.html#(5)

Thomas