简体   繁体   English

抑制来自deSolve :: lsoda的错误

[英]Suppress error from deSolve::lsoda

I'm running some algorithms that use approximate Bayesian computation (eg see Toni et al., 2009) and these require repeatedly solving the Lotka-Volterra system of equations with a randomly generated set of input parameters. 我正在运行一些使用近似贝叶斯计算的算法(例如,参见Toni等,2009),这些算法需要使用一组随机生成的输入参数来反复求解Lotka-Volterra方程组。 I'm using the lsoda function from the deSolve package . 我正在使用deSolve软件包中lsoda函数。

Occasionally this function throws an error and I was hoping to ignore this using the try(..., silent = TRUE) function although this doesn't seem to work (see example below). 有时此函数会引发错误,我希望使用try(..., silent = TRUE)函数忽略此错误,尽管这似乎不起作用(请参见下面的示例)。 Setting options(show.error.messages = FALSE) doesn't work either. 设置options(show.error.messages = FALSE)也不起作用。

How do I suppress printing of the error message from deSolve::lsoda? 如何抑制从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)
}

Look at page 44 of the vignette in deSolve , this kind of error is described here . 请看deSolve插图的第44页, 这里描述这种错误。

You can solve this problem in reducing the absolute tolerance of the solution. 您可以通过减小解决方案的绝对公差来解决此问题。 In your example, the following approach works: 在您的示例中,以下方法有效:

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

Note: your data.frame dfs will be overwritten in every loop and if you want the output of the problem_seeds in a data.frame, you can run a fuinction of the apply family. 注意:data.frame dfs将在每个循环中被覆盖,如果要在data.frame中输出problem_seeds,则可以运行apply系列的功能。 And you don't need the try function from now on. 从现在开始,您不需要try函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM