使用RStudio中的deSolve包中的dede来解决带有时间延迟的ODE。

huangapple go评论60阅读模式
英文:

Using dede in deSolve package in RStudio to solve time-delayed ODE

问题

我正在尝试使用RStudio中的deSolve包中的dede来解决带有时间延迟的SEIRVB ODE问题。

我目前有以下代码:

library(deSolve)

tau = 0

# 参数
Lambda = 0.5
beta1 = 1.13921549
beta2 = 2.68228986
beta3 = 2.9627036
beta4 = 1.19686956
beta5 = 0.5
beta6 = 1.34496108
beta7 = 3.40332936
beta8 = 1.48249240
beta9 = 0.04681161
beta10 = 2.32555864
alpha = 0.5
kappa = 0.02933240
d = 0.0047876
r = 0.09871
tau = 7 # 时间延迟

# 代表具有时间延迟的SEIRVB模型的函数
seirvb_model <- function(t, y, parms) {
  if (t < tau)
    Ilag <- initial_conditions["I"]
  else
    Ilag <- lagvalue(t - tau, 3)

  dS <- Lambda - beta1 * y[1] * y[2] - (beta3 + alpha) * y[1]
  dE <- beta4 * y[5] * y[2] + beta6 * y[6] * y[2] + beta1 * y[1] * y[2] - beta2 * y[2] * y[3] - (kappa + alpha) * y[2]
  dI <- beta5 * y[5] * Ilag + beta7 * y[6] * y[3] + beta2 * y[2] * y[3] - (d + alpha + r) * y[3]
  dR <- beta9 * y[5] + beta8 * y[6] + r * y[3] + kappa * y[2] - alpha * y[4]
  dV <- beta3 * y[1] - (beta9 + alpha + beta10) * y[5] - beta4 * y[5] * y[2] - beta5 * y[5] * Ilag
  dB <- beta10 * y[5] - beta6 * y[6] * y[2] - beta7 * y[6] * y[3] - (beta8 + alpha) * y[6]
  list(c(dS, dE, dI, dR, dV, dB))
}

# 初始条件
initial_conditions <- c(
  S = 0.3,
  E = 0.1,
  I = 0.006,
  R = 0,
  V = 0,
  B = 0
)

# 时间向量
times <- seq(0, 120, by = 1)

# 解决SEIRVB模型
ode_output <- dede(y = initial_conditions, times = times, func = seirvb_model, parms = NULL)

# 绘制结果
plot(ode_output, xlab = "时间", ylab = "人口", main = "COVID-19的SEIRVB模型")

尽管代码能够运行,但时间延迟未考虑在解决方案中,因为即使更改了时间延迟,我仍然得到相同的答案。有人能帮我解决这个问题吗?谢谢!

英文:

I'm trying to solve this SEIRVB ODE with time delay using dede in deSolve package of R in RStudio

I'm currently have this code

library(deSolve)
tau=0
# Parameters
Lambda = 0.5      
beta1 = 1.13921549    
beta2 = 2.68228986      
beta3 = 2.9627036      
beta4 = 1.19686956     
beta5 = 0.5    
beta6 = 1.34496108     
beta7 = 3.40332936   
beta8 = 1.48249240    
beta9 = 0.04681161     
beta10 = 2.32555864    
alpha = 0.5    
kappa = 0.02933240    
d = 0.0047876         
r = 0.09871        
tau=7 #time delay
# Function representing the SEIRVB model with time delay
seirvb_model &lt;- function(t, y, parms) {
if (t &lt; tau)
Ilag &lt;- initial_conditions[&quot;I&quot;]
else
Ilag &lt;- lagvalue(t-tau,3)
dS &lt;- Lambda - beta1 * y[1] * y[2] - (beta3 + alpha) * y[1]
dE &lt;- beta4 * y[5] * y[2] + beta6 * y[6] * y[2] + beta1 * y[1] * y[2] - beta2 * y[2] * y[3] - (kappa + alpha) * y[2]
dI &lt;- beta5 * y[5] * Ilag + beta7 * y[6] * y[3] + beta2 * y[2] * y[3] - (d + alpha + r) * y[3]
dR &lt;- beta9 * y[5] + beta8 * y[6] + r * y[3] + kappa * y[2] - alpha * y[4]
dV &lt;- beta3 * y[1] - (beta9 + alpha + beta10) * y[5] - beta4 * y[5] * y[2] - beta5 * y[5] * Ilag
dB &lt;- beta10 * y[5] - beta6 * y[6] * y[2] - beta7 * y[6] * y[3] - (beta8 + alpha) * y[6]
list(c(dS, dE, dI, dR, dV, dB))
}
# Initial conditions
initial_conditions &lt;- c(
S=0.3,
E=0.1,
I=0.006,
R=0,
V=0,
B=0
)
# Time vector
times &lt;- seq(0, 120, by = 1)
# Solve the SEIRVB model
ode_output &lt;- dede(y = initial_conditions, times = times, func = seirvb_model, parms = NULL)
# Plotting the results
plot(ode_output, xlab = &quot;Time&quot;, ylab = &quot;Population&quot;, main = &quot;SEIRVB Model for COVID-19&quot;)

Even though it works, the time delay was not considered in the solution as I got the same answer even though I changed the time delay already. Can someone help me how to fix this? Thank you!

答案1

得分: 3

以下是翻译好的部分:

"tau <- 0
ode_output2 <- dede(y = initial_conditions, times = times, func = seirvb_model, parms = NULL)
tau <- 40
ode_output3 <- dede(y = initial_conditions, times = times, func = seirvb_model, parms = NULL)"

"我们可以使用 all.equal() 来查看输出不相同。"

"可视化基线 tau=7 和两种备选项 (tau=0, tau=40) 之间的差异;"

"png("dede_diff.png")
par(las = 1, bty = "l")
matplot(ode_output[,"I"] - cbind(ode_output3[,"I"],ode_output2[,"I"]),
lty = 1:2, col = 1:2, ylab = "diff", type = "l")
legend("topright",
legend = c("tau = 40", "tau=0"),
lty = 1:2, col = 1:2)
dev.off()"

英文:

It appears there is a difference:

tau &lt;- 0 
ode_output2 &lt;- dede(y = initial_conditions, times = times, func = seirvb_model, parms = NULL)
tau &lt;- 40
ode_output3 &lt;- dede(y = initial_conditions, times = times, func = seirvb_model, parms = NULL)

We can use all.equal() to see that the outputs are not the same.

Visualizing the differences between the baseline tau=7 and two alternatives (tau=0, tau=40);

png(&quot;dede_diff.png&quot;)
par(las = 1, bty = &quot;l&quot;)
matplot(ode_output[,&quot;I&quot;] - cbind(ode_output3[,&quot;I&quot;],ode_output2[,&quot;I&quot;]),
        lty = 1:2, col = 1:2, ylab = &quot;diff&quot;, type = &quot;l&quot;)
legend(&quot;topright&quot;,
       legend = c(&quot;tau = 40&quot;, &quot;tau=0&quot;),
       lty = 1:2, col = 1:2)
dev.off()

使用RStudio中的deSolve包中的dede来解决带有时间延迟的ODE。

You may wonder why the delay makes so little difference to the output, but it does make some difference.

huangapple
  • 本文由 发表于 2023年6月18日 22:37:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76501073.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定