2

我有一个简单的 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))
4

1 回答 1

2

这是一个建议,但我不完全确定它的行为方式是否符合您的要求,因此请先检查一下,如果没有,请回复我。请注意,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 包(v0.3.0)于 2021-08-22 创建

于 2021-08-22T12:08:00.367 回答