Can't suppress warnings or messages while using nls2::nls2 in R

450 Views Asked by At

I'm attempting to fit ~100 data sets to a triexponential decay formula, and the data often don't fit well. That's fine, but I can't seem to suppress the gazillion warnings that this produces. Since this is a part of a markdown script, in the end, I get pages and pages of repetitive warning messages.

Here is an example of my data, which I've named 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")

And here's an example of what I've tried:

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)))

No matter how I try suppressing things, I get a loooooooonnnnnggg list of errors such as:

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

To be clear, I'm expecting the messages and errors because I know I often lack sufficient data to adequately describe a triexponential decay, but there should be some way to suppress all these warnings, shouldn't there?

2

There are 2 best solutions below

0
Ben Bolker On BEST ANSWER

I'm going to suggest a combination of capture.output(type="message", ...) and try(). Just the try() (or tryCatch()) doesn't catch all of the messages, since they are emitted from deeper within 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)
)

In this case res ends up as an object of type try-error: you can detect this and do what you want by testing 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>
0
thorepet On

You can try wrapping the call in a tryCatch. It allows you to exectute specific functions when the expression throws an error or warning.

When the nls2 call produces an error, the provided function is executed, and returns NULL, so no spam in your console.

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