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

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

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

问题

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

我目前有以下代码:

  1. library(deSolve)
  2. tau = 0
  3. # 参数
  4. Lambda = 0.5
  5. beta1 = 1.13921549
  6. beta2 = 2.68228986
  7. beta3 = 2.9627036
  8. beta4 = 1.19686956
  9. beta5 = 0.5
  10. beta6 = 1.34496108
  11. beta7 = 3.40332936
  12. beta8 = 1.48249240
  13. beta9 = 0.04681161
  14. beta10 = 2.32555864
  15. alpha = 0.5
  16. kappa = 0.02933240
  17. d = 0.0047876
  18. r = 0.09871
  19. tau = 7 # 时间延迟
  20. # 代表具有时间延迟的SEIRVB模型的函数
  21. seirvb_model <- function(t, y, parms) {
  22. if (t < tau)
  23. Ilag <- initial_conditions["I"]
  24. else
  25. Ilag <- lagvalue(t - tau, 3)
  26. dS <- Lambda - beta1 * y[1] * y[2] - (beta3 + alpha) * y[1]
  27. 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]
  28. dI <- beta5 * y[5] * Ilag + beta7 * y[6] * y[3] + beta2 * y[2] * y[3] - (d + alpha + r) * y[3]
  29. dR <- beta9 * y[5] + beta8 * y[6] + r * y[3] + kappa * y[2] - alpha * y[4]
  30. dV <- beta3 * y[1] - (beta9 + alpha + beta10) * y[5] - beta4 * y[5] * y[2] - beta5 * y[5] * Ilag
  31. dB <- beta10 * y[5] - beta6 * y[6] * y[2] - beta7 * y[6] * y[3] - (beta8 + alpha) * y[6]
  32. list(c(dS, dE, dI, dR, dV, dB))
  33. }
  34. # 初始条件
  35. initial_conditions <- c(
  36. S = 0.3,
  37. E = 0.1,
  38. I = 0.006,
  39. R = 0,
  40. V = 0,
  41. B = 0
  42. )
  43. # 时间向量
  44. times <- seq(0, 120, by = 1)
  45. # 解决SEIRVB模型
  46. ode_output <- dede(y = initial_conditions, times = times, func = seirvb_model, parms = NULL)
  47. # 绘制结果
  48. 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

  1. library(deSolve)
  2. tau=0
  3. # Parameters
  4. Lambda = 0.5
  5. beta1 = 1.13921549
  6. beta2 = 2.68228986
  7. beta3 = 2.9627036
  8. beta4 = 1.19686956
  9. beta5 = 0.5
  10. beta6 = 1.34496108
  11. beta7 = 3.40332936
  12. beta8 = 1.48249240
  13. beta9 = 0.04681161
  14. beta10 = 2.32555864
  15. alpha = 0.5
  16. kappa = 0.02933240
  17. d = 0.0047876
  18. r = 0.09871
  19. tau=7 #time delay
  20. # Function representing the SEIRVB model with time delay
  21. seirvb_model &lt;- function(t, y, parms) {
  22. if (t &lt; tau)
  23. Ilag &lt;- initial_conditions[&quot;I&quot;]
  24. else
  25. Ilag &lt;- lagvalue(t-tau,3)
  26. dS &lt;- Lambda - beta1 * y[1] * y[2] - (beta3 + alpha) * y[1]
  27. 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]
  28. dI &lt;- beta5 * y[5] * Ilag + beta7 * y[6] * y[3] + beta2 * y[2] * y[3] - (d + alpha + r) * y[3]
  29. dR &lt;- beta9 * y[5] + beta8 * y[6] + r * y[3] + kappa * y[2] - alpha * y[4]
  30. dV &lt;- beta3 * y[1] - (beta9 + alpha + beta10) * y[5] - beta4 * y[5] * y[2] - beta5 * y[5] * Ilag
  31. dB &lt;- beta10 * y[5] - beta6 * y[6] * y[2] - beta7 * y[6] * y[3] - (beta8 + alpha) * y[6]
  32. list(c(dS, dE, dI, dR, dV, dB))
  33. }
  34. # Initial conditions
  35. initial_conditions &lt;- c(
  36. S=0.3,
  37. E=0.1,
  38. I=0.006,
  39. R=0,
  40. V=0,
  41. B=0
  42. )
  43. # Time vector
  44. times &lt;- seq(0, 120, by = 1)
  45. # Solve the SEIRVB model
  46. ode_output &lt;- dede(y = initial_conditions, times = times, func = seirvb_model, parms = NULL)
  47. # Plotting the results
  48. 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:

  1. tau &lt;- 0
  2. ode_output2 &lt;- dede(y = initial_conditions, times = times, func = seirvb_model, parms = NULL)
  3. tau &lt;- 40
  4. 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);

  1. png(&quot;dede_diff.png&quot;)
  2. par(las = 1, bty = &quot;l&quot;)
  3. matplot(ode_output[,&quot;I&quot;] - cbind(ode_output3[,&quot;I&quot;],ode_output2[,&quot;I&quot;]),
  4. lty = 1:2, col = 1:2, ylab = &quot;diff&quot;, type = &quot;l&quot;)
  5. legend(&quot;topright&quot;,
  6. legend = c(&quot;tau = 40&quot;, &quot;tau=0&quot;),
  7. lty = 1:2, col = 1:2)
  8. 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:

确定