【发布时间】:2016-10-14 12:18:47
【问题描述】:
我正在运行一些使用近似贝叶斯计算的算法(例如,参见 Toni 等人,2009 年),这些算法需要使用一组随机生成的输入参数重复求解 Lotka-Volterra 方程组。我正在使用来自deSolve package 的lsoda 函数。
这个函数偶尔会抛出一个错误,我希望使用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