1

我想了解示例代码中 tryCatch 的行为:

require(fitdistrplus)
a = as.data.frame(table(rpois(n = 1000,lambda = 6)))
fitdist(data = a$Freq,distr = "nbinom",discrete = TRUE)
result = tryCatch(expr = (fitdist(data = a$Freq, distr = "nbinom",discrete = TRUE)),
error = function(err) {print(paste("MY_ERROR:  ",err))},
warning = function(wrr) {print(paste("MY_WARNING:  ",wrr))})

fitdist命令在命令之外执行时tryCatch会给出与nbinom拟合相关的参数。但是,当我在 try catch 命令中执行相同操作时,我收到以下警告,

[1] "MY_WARNING:  simpleWarning in dnbinom(c(3L, 18L, 37L, 77L, 125L, 152L, 163L, 168L, 104L, 77L, : NaNs produced\n"

有人可以帮我理解这个观察吗?

编辑:我创建了这个测试用例作为我在运行循环以适应跨大数据集的多个列的分布时遇到的问题的样本。我需要使用 tryCatch 来迭代多个分布和分布拟合方法,如 mle、mme 等我面临的主要问题是 fitdist 在独立执行但不在 tryCatch 命令中时表现如预期。

4

0 回答 0