我想使用 deSolve 创建蝴蝶生态的动态模型。模拟运行了几个模拟年,并且一些事件在一年中的某一天触发(所以我添加了一个状态变量days
)。为了触发这些事件,我想使用一个ifelse
语句并且它工作正常,直到我尝试在ifelse
语句中放入一个涉及另一个状态变量的操作:D.egg.sus=(ifelse(days<270,(400 * adult.sus),0))
. 当我这样做时,模拟运行,但它似乎忽略了该ifelse
语句。谁能帮帮我?这是我的完整代码:
days = 1
egg.sus = 0
larvae.sus = 0
pupae.sus = 0
adult.sus = 1000
state = c(days = days, egg.sus=egg.sus, larvae.sus=larvae.sus,
pupae.sus=pupae.sus, adult.sus=adult.sus)
model = function(t, state, parameters)
{
with(as.list(c(state, parameters)),
{
D.Days = 1
D.egg.sus =
( ifelse(days<270, (400*adult.sus) ,0)) ## This is the line causing trouble
(- egg.sus/5)
(- egg.sus * rbeta(1, 6.038892/5,1.4612593)*.95)
D.larvae.sus =
(+ egg.sus/5)
(- larvae.sus * rbeta(1, 0.248531/14,0.2094379)*0.95)
(- larvae.sus/14)
D.pupae.sus =
(+ larvae.sus/14)
(- pupae.sus * rbeta(1, 0.022011/15, 1.43503))
(- pupae.sus/15)
D.adult.sus =
(+ pupae.sus/15)
(- adult.sus/30)
list(c( D.Days, D.egg.sus, D.larvae.sus,D.pupae.sus, D.adult.sus))
}
)}
events <- data.frame(var = c('days'),
time = seq(364,73000,by=365) ,
value = 0,
method = "rep")
require(deSolve)
times = seq(1,900, by = 1)
out = ode(y=state, times = times, func = model, parms = parameters, events = list(data=events))
dev.cur()
plot(out, col = 2)