gpt4 book ai didi

r - 在 R 中用循环和 if else 解析 SIR 模型

转载 作者:行者123 更新时间:2023-12-04 07:14:22 26 4
gpt4 key购买 nike

我有一个简单的 SIR 模型,我正在尝试实现一种疫苗接种方法 (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)
创建于 2021-08-22 由 reprex package (v0.3.0)

关于r - 在 R 中用循环和 if else 解析 SIR 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68878257/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com