[英]R - deSolve package (ode function): change a matrix of parameters in SIR model according to time
[英]in R desolve SIR model with loop and if else
我有一个简单的 SIR model,我正在尝试实施疫苗接种方法 (V),首先检查感染者是否高于阈值 (100),如果仍有足够的易感者 (50),它将接种疫苗每个时间步一定数量(50)。
然而,我想做的是,一旦条件满足,它应该接种 7 天(无论在这 7 天内感染者是否仍然高于阈值,例如,如果在第 4 天之后, I = 70它仍然应该继续,只有在 S < 50 时才应该停止。7 天结束后,它应该再次检查条件,然后再开始 7 天或不开始。
到目前为止,如果有人帮助我实现该循环,我将不胜感激
sirV=function(time, y, params){
S = y[1]
I = y[2]
R = y[3]
V = y[4]
with(as.list(params),{
vac_helper = if (I > 100 & S > 50) {50}
else {0}
N = S+I+R+V
dS = -S*beta*I/N - vac_helper
dI = S*beta*I/N - gamma*I
dR = +gamma*I
dV = vac_helper
return(list(c(dS, dI, dR, dV)))
})
}
myparameters = c(gamma=1/10,beta=0.2)
times <- seq(0, 300)
my_ode <- as.data.frame(ode( y=c(100000, 10, 0,0), times, sirV, myparameters))
这是一个建议,但我不完全确定它是否按照您想要的方式运行,所以请先检查一下,如果不正确请再回来找我。 请注意, end
必须在全局环境中。
library(deSolve)
sirV = function(time, y, params){
S = y[1]
I = y[2]
R = y[3]
V = y[4]
with(as.list(params),{
# Has the previous vaccination ended, and do we still need to vaccinate?
if(time > end & I > 100) {
end <<- time + 7
}
# Can we vaccinate?
vaccinate = ifelse(end > time & S > 50, 50, 0)
N = S+I+R+V
dS = -S*beta*I/N - vaccinate
dI = S*beta*I/N - gamma*I
dR = gamma*I
dV = vaccinate
# Store some results in a global list so we can check what is happening under the hood
temp = data.frame(time, end, S, I, R, V, dS, dI, dR, dV, vaccinate)
catch_results[[length(catch_results)+1]] <<- temp
return(list(c(dS, dI, dR, dV)))
})
}
myparameters = c(gamma = 1/10, beta = 0.2)
times <- seq(from = 0, to = 300, by = 1)
end <- 0 # Reset end time
catch_results = list() # Catch results from inside the function
my_ode <- ode( y=c(100000, 10, 0, 0), times, sirV, myparameters)
plot(my_ode)
# Check this to see if we get the expected behavior, especially at around time = 189
results = dplyr::bind_rows(catch_results)
由reprex package (v0.3.0) 创建于 2021-08-22
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.