Handling error produced by drm
Asked Answered
U

0

7

I am stuck with this problem and my R abilities are apparently not sufficient to solve it. Hopefully, someone can help me out.

I am currently working on a script that uses the function "drm" of the package "drc". I want to obtain the EC10, 20, and 50 values from the best model that are produced by drm using different given functions. I would like to implement a solution for datasets that do not converge with all or some of these functions (for example printing out "dataset does not converge" if not). Handling the error using tryCatch does not work. It only catches the error produced by "drmOpt" and not the one produced by "optim" and thus stops the script.
This is the error I get without try or tryCatch:

Error in optim(startVec, opfct, hessian = TRUE, method = optMethod, control = list(maxit = maxIt, : non-finite finite-difference value [2]
Error in drmOpt(opfct, opdfct1, startVecSc, optMethod, constrained, warnVal, : Convergence failed

With try or tryCatch I only get the first error.
The error can be reproduced with the following code (LL.2 produces the errors, LL.3 doesn't):

library(drc)
library(data.table)
data <- data.table(Dose = c(0.0,  0.0,  0.0,  0.0,  0.0,  0.0,  0.0,  0.0,  0.5, 0.5,  0.5,  0.5,  3.0,  3.0, 3.0,  3.0, 9.0,  9.0,  9.0,  9.0, 27.0, 27.0, 27.0, 27.0, 81.0, 81.0, 81.0, 81.0), Value = c(1, 3, 2, 7, 5, 7, 6, 0, 2, 2, 4, 6, 4, 5, 4, 2, 2, 4, 5, 5, 4, 4, 3, 1, 6, 5, 4, 2))

data.LL.2 <- drm(Value ~ Dose, data = data, fct = LL.2(), type = "continuous")
data.LL.3 <- drm(Value ~ Dose, data = data, fct = LL.3(), type = "continuous")

data.LL.2.try <- tryCatch(drm(Value ~ Dose, data = data, fct = LL.2(), type = "continuous"), error = function(e) "No convergence")
data.LL.3.try <- tryCatch(drm(Value ~ Dose, data = data, fct = LL.3(), type = "continuous"), error = function(e) "No convergence")

I don't wan't to adapt the dataset or change the functions, as the script is supposed to be used with many datasets in a loop and many functions that cannot be handled separately. The output "doesn't converge with this function" is a valuable information in this case.

Thank you very much in advance.

Unbowed answered 9/11, 2017 at 6:45 Comment(3)
I ran your code in RStudio and it appears that the trycatch is working okay. I still see the error messages printed to the console, but the script execution continues on as I think you want. If I add print statements to the end of the script (e.g. print(data.LL.2.try) ) and then execute the whole thing, i see the printed statements at the end -- which shows the script is executing all the way to the end.Cense
I've got the same error. Ever solve this?Adeliaadelice
No, I never found a "real" solution. I made a work-around adding control = drmc(errorm=FALSE) in the function call and then using is.null(model$"RSS"[2]), which worked in my case.Unbowed

© 2022 - 2024 — McMap. All rights reserved.