You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R语言多参数集ODE模拟循环报错:精度警告无法消除

问题:遍历多参数集运行ODE模型时出现DLSODA精度警告,需修复for循环

问题背景

我尝试用for循环遍历不同参数集(场景A-I)运行疾病传播的ODE模型,希望以最透明的方式实现并保存结果用于后续对比绘图,但运行时出现精度相关警告。调整rtol和atol参数(扩大10倍、100倍、1000倍)后,警告仍未消失。此前得到的建议要么不够透明,要么无法适配我真实数据集的13个参数规模,因此自行构建for循环但存在问题。

错误警告信息

DLSODA提示信息

DLSODA- At T (=R1), too much accuracy requested
for precision of machine.. See TOLSF (=R2)
In above message, R1 = 1, R2 = nan

DLSODA- At T (=R1), too much accuracy requested
for precision of machine.. See TOLSF (=R2)
In above message, R1 = 1, R2 = nan

DLSODA- At T (=R1), too much accuracy requested
for precision of machine.. See TOLSF (=R2)
In above message, R1 = 1, R2 = nan

R警告信息

Warning messages:
1: In lsoda(y, times, func, parms, ...) :
Excessive precision requested. scale up rtol' and atol' e.g by the factor 10
2: In lsoda(y, times, func, parms, ...) :
Returning early. Results are accurate, as far as they go
3: In lsoda(y, times, func, parms, ...) :
Excessive precision requested. scale up rtol' and atol' e.g by the factor 10
4: In lsoda(y, times, func, parms, ...) :
Returning early. Results are accurate, as far as they go
5: In lsoda(y, times, func, parms, ...) :
Excessive precision requested. scale up rtol' and atol' e.g by the factor 10
6: In lsoda(y, times, func, parms, ...) :
Returning early. Results are accurate, as far as they go

可复现代码

以下是调试用的基础SIR模型代码:

library(deSolve)
library(ggplot2)
library(dplyr)
library(tidyverse)

parms = c(
  "beta" = 0.00016,
  "gamma" = 0.12
)

CoVode = function(t, x, parms) {
  S =  x[1]  # 易感者
  I =  x[2]  # 感染者
  R =  x[3]  # 康复者

  beta = parms["beta"]
  gamma  = parms["gamma"]

  dSdt <- -beta*S*I
  dIdt <- beta*S*I-gamma*I
  dRdt <- gamma*I

  output = c(dSdt,dIdt,dRdt)
  names(output) = c('S', 'I', 'R')

  return(list(output))
}

# 初始条件
init = numeric(3)
init[1] = 10000
init[2] = 1
names(init) = c('S','I','R')

# 运行模型
odeSim = ode(y = init, 0:50, CoVode, parms)

# 绘图
Simdat <- data.frame(odeSim)

Simdat_long <-
  Simdat %>%
  pivot_longer(!time, names_to = "groups", values_to = "count")

ggplot(Simdat_long, aes(x= time, y = count, group = groups, colour = groups)) +
  geom_line()

需要实现的多参数遍历代码

我需要遍历A-I共9个场景,将每个场景的模型输出保存到数据框中,用于后续对比绘图:

parmdf <- data.frame(scenario=c("A", "B", "C", "D", "E", "F", "G", "H", "I"),
                     beta=c(0.0001,0.0001, 0.0001, 0.001, 0.001, 0.001, 0.124, 0.124, 0.124),                       
                     gamma=c(0.1, 0.2, 0.3, 0.1, 0.2, 0.3, 0.1, 0.2, 0.3) )

# 尝试的for循环(存在问题)
for (i in parmdf) {
  parms = as.numeric(unlist(parmdf[i,-1]))
  names(parmdf) = names(parms)
  odeSim5 = ode(y = init, 0:50, CoVode, parms)
}

print(odeSim5)

需要修复这个for循环,消除警告并正确保存所有场景的结果。


内容的提问来源于stack exchange,提问作者Kit McLean

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 20:05:02