【问题标题】:Suppress error from deSolve::lsoda抑制来自 deSolve::lsoda 的错误
【发布时间】:2016-10-14 12:18:47
【问题描述】:

我正在运行一些使用近似贝叶斯计算的算法(例如,参见 Toni 等人,2009 年),这些算法需要使用一组随机生成的输入参数重复求解 Lotka-Volterra 方程组。我正在使用来自deSolve packagelsoda 函数。

这个函数偶尔会抛出一个错误,我希望使用try(..., silent = TRUE) 函数来忽略它,尽管这似乎不起作用(参见下面的示例)。设置options(show.error.messages = FALSE) 也不起作用。

如何禁止从 deSolve::lsoda 打印错误消息?

require(deSolve)

# Differential equations defining the system
LV <- function(Time, State, Pars){

    with(as.list(c(State, Pars)), {

    dx <- a*x - x*y
    dy <- b*x*y - y

    return(list(c(dx, dy)))
    }
    )
}

# Parameters
pars <- c(a = 1.0, b = 1.0)

# Initial conditions
init <- c(x = 1.0, y = 0.5)

# Time steps
times <- seq(0, 15, length.out = 100)

problem_seeds <- c(7, 241, 361, 365, 468, 473, 649, 704, 724, 745, 838)

for (i in problem_seeds){
    set.seed(i)

    # Sample from pi(theta), prior for parameter vector (a, b)
    theta_star <- runif(2, -10, 10)
    names(theta_star) <- c("a", "b")

    # Simulate a dataset using these parameters (at the specified times)
    try(out <- lsoda(func = LV, 
            y = init, 
            parms = theta_star, 
            times = times), 
            silent = TRUE)
    dfs <- as.data.frame(out)
}

【问题讨论】:

    标签: r error-handling ode differential-equations


    【解决方案1】:

    deSolve中小插图的第44页,这种错误描述为here

    您可以通过降低解决方案的绝对容差来解决此问题。在您的示例中,以下方法有效:

    out <- lsoda(func = LV, 
                 y = init, 
                 parms = theta_star, 
                 times = times,
                 atol = 1e-3)
    

    注意:您的 data.frame dfs 将在每个循环中被覆盖,如果您希望在 data.frame 中输出问题种子,您可以运行 apply 系列的函数。从现在开始,您不再需要 try 函数。

    【讨论】:

    • 这是一个有用的解决方法。回复:覆盖 df - 这只是 ABC 算法的一小部分(只是为了重现问题),在主要算法中,数据帧在被覆盖之前使用。我会等着看是否有人对如何抑制错误有想法。非常感谢您的回答。
    • 我试过 google 和 SO 但没有看到这个。感谢分享!似乎这个问题在 2013 年发布后仍然存在于 2016 年。哦,好吧,我会尝试他们建议的解决方法。谢谢。
    猜你喜欢
    • 2019-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多