Search code examples
rerror-handlingsapplynon-linear-regressionnls

Dealing with an error in Sapply loop with nls - loop stops at first error and does not output any data


I have a very large dataset with a independent variable (Kpl) and a couple of thousand dependent variables (Y1,... Yi).

I want to calculate the AICc of, for example, the fit for a quadratic function for all the Kpl Vs Yi pairs.

I have the following code that I tested and worked fine for a few samples

cor_data <- read.csv("R/data/experimental_data/kpl_1.csv") 
Kpl <- as.matrix(cor_data[,1])

sapply(cor_data[,-1], function(x) AICc(nls(x ~ a*exp(b*Kpl),start = list(a=1,b=1),control=list(maxiter=1000))))

When I add more samples to the original file, if there is an error, the whole sapply loop stops and does not output any data. I realize that some models are not going to be able to be fit with the equation I am trying and that is fine, but is there a way to force the sapply loop to continue after the error and give the results for the samples that actually did work?

Bonus if it can also print the cause for failing the fit, as it could be a bunch of different things (going below minfactor, going above maxiter or bad starting values).


Solution

  • You could use a try-catch or look at tidy-evaluation in tidyverse:

    evalF <- function(x){
      fit <- try(nls(x ~ a * exp(b * Kpl),
                     start = list(a = 1, b = 1),
                     control = list(maxiter = 1000)))
      if(!inherits(fit, 'try-error'))
        AIC(fit)
      else
        NULL
    }
    sapply(cor_data[, -1], evalF)
    

    If you want to print the error message, you could simply return fit instead of NULL or similarly print it to console. Consider using the warning(...) function in the latter case, for formatting purposes.