在 R 中使用 nls2::nls2 时无法抑制警告或消息
Can't suppress warnings or messages while using nls2::nls2 in R
我正在尝试将约 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
明确地说,我期待消息和错误,因为我知道我经常缺乏足够的数据来充分描述三指数衰减,但应该一些 抑制所有这些警告的方法,不应该吗?
您可以尝试将通话包装在 tryCatch
中。它允许您在表达式抛出错误或警告时执行特定函数。
当 nls2
调用产生错误时,将执行提供的函数,并且 returns NULL
,因此您的控制台中没有垃圾邮件。
tryCatch(
expr = {
nls2::nls2(...)
},
error = function(e) NULL,
warning = function(w) NULL
)
我将建议 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>
我正在尝试将约 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
明确地说,我期待消息和错误,因为我知道我经常缺乏足够的数据来充分描述三指数衰减,但应该一些 抑制所有这些警告的方法,不应该吗?
您可以尝试将通话包装在 tryCatch
中。它允许您在表达式抛出错误或警告时执行特定函数。
当 nls2
调用产生错误时,将执行提供的函数,并且 returns NULL
,因此您的控制台中没有垃圾邮件。
tryCatch(
expr = {
nls2::nls2(...)
},
error = function(e) NULL,
warning = function(w) NULL
)
我将建议 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>