【问题标题】:Can't suppress warnings or messages while using nls2::nls2 in R在 R 中使用 nls2::nls2 时无法抑制警告或消息
【发布时间】:2020-09-01 18:58:38
【问题描述】:

我正在尝试将大约 100 个数据集拟合到三指数衰减公式,但数据通常不能很好地拟合。这很好,但我似乎无法抑制由此产生的大量警告。由于这是降价脚本的一部分,最后,我得到一页又一页的重复警告消息。

这是我的数据示例,我将其命名为 DF

structure(list(Time_min = c(19, 34, 49, 64, 94, 124, 154, 184, 
214, 244, 304), Concentration = c(477.08, 284.26, 189.16, 134.66, 
74.32, 53.04, 28.16, 16.78, 9.24, 8.7, 4.42)), row.names = c(NA, 
-11L), class = "data.frame")

这是我尝试过的一个例子:

StartGuess <- data.frame(A = c(100, 500),
                         alpha = c(0.01, 0.5),
                         B = c(100, 500),
                         beta = c(0.001, 0.05),
                         G = c(10, 100),
                         gamma = c(0.0001, 0.01))

suppressMessages(nls2::nls2(Concentration ~ A * exp(-alpha * Time_min) +
                            B * exp(-beta * Time_min) +
                            G * exp(-gamma * Time_min), 
                      data = DF, start = StartGuess))

suppressWarnings(nls2::nls2(Concentration ~ A * exp(-alpha * Time_min) +
                            B * exp(-beta * Time_min) +
                            G * exp(-gamma * Time_min), 
                      data = DF, start = StartGuess))


suppressWarnings(
      suppressMessages(nls2::nls2(Concentration ~ A * exp(-alpha * Time_min) +
                                  B * exp(-beta * Time_min) +
                                  G * exp(-gamma * Time_min), 
                            data = DF, start = StartGuess)))

无论我如何尝试抑制事物,我都会得到一个 loooooooonnnnnggg 错误列表,例如:

Error in numericDeriv(form[[3L]], names(ind), env) : 
  Missing value or an infinity produced when evaluating the model
Error in (function (formula, data = parent.frame(), start, control = nls.control(),  : 
  singular gradient

明确地说,我期待消息和错误,因为我知道我经常缺乏足够的数据来充分描述三指数衰减,但应该有一些方法压制所有这些警告,不应该吗?

【问题讨论】:

    标签: r suppress-warnings nls error-suppression


    【解决方案1】:

    您可以尝试将呼叫封装在 tryCatch 中。它允许您在表达式抛出错误或警告时执行特定的功能。

    nls2 调用产生错误时,提供的函数将被执行,并返回NULL,因此您的控制台中没有垃圾邮件。

    tryCatch(
        expr = {
            nls2::nls2(...)
                },
        error = function(e) NULL,
        warning = function(w) NULL
    )
    

    【讨论】:

      【解决方案2】:

      我将建议capture.output(type="message", ...)try() 的组合。仅try()(或tryCatch())并不能捕获所有消息,因为它们是从nls2::nls2 的更深处发出的......

      cc <- capture.output(type="message",
               res <- try(nls2::nls2(Concentration ~ A * exp(-alpha * Time_min) +
                            B * exp(-beta * Time_min) +
                            G * exp(-gamma * Time_min), 
                            data = DF, start = StartGuess),
          silent=TRUE)
      )
      

      在这种情况下,res 最终成为 try-error 类型的对象:您可以检测到这一点并通过测试 if (inherits(res,"try-error")) ... 来做您想做的事情

      [1] "Error in result[[which.min(ss)]] : \n  attempt to select less than one element in get1index\n"
      attr(,"class")
      [1] "try-error"
      attr(,"condition")
      <simpleError in result[[which.min(ss)]]: attempt to select less than one element in get1index>
      

      【讨论】:

        猜你喜欢
        • 2012-02-09
        • 1970-01-01
        • 2012-02-06
        • 1970-01-01
        • 1970-01-01
        • 2014-08-30
        • 1970-01-01
        • 2016-06-17
        相关资源
        最近更新 更多