I would like to use try()
or tryCatch()
or a function like this to detect if there is an error in my model called "fit1". If the model is fine, I want to use "fit1", otherwise I want to use "fit2"
fit1<-glmer(stat ~ dataint + DBH + DBH2 + (1|site_plot), family=binomial(link="logit"))
fit2<-glm (stat ~ dataint + DBH + DBH2, family=binomial(link="logit"))
Do you know how to do this? I don't add any data because my problem is probably easy to solve, but if it's needed, I can upload them.
THanks!
The tryCatch() function in R evaluates an expression with the possibility to catch exceptions. The class of the exception thrown by a standard stop() call is try-error. The tryCatch() function allows the users to handle errors. With it, you can do things like: if(error), then(do this).
In R Programming, there are basically two ways in which we can implement an error handling mechanism. Either we can directly call the functions like stop() or warning(), or we can use the error options such as “warn” or “warning. expression”.
The simplest way of handling conditions in R is to simply ignore them: Ignore errors with try() . Ignore warnings with suppressWarnings() .
Entering debug mode (stopping) In order to enter debug mode, you'll need to tell R when you want to pause the computation. R doesn't have a “pause now” feature (and most computations are so fast that such a feature would not be helpful!). Instead, you'll want to “set your traps” prior to starting your computation.
Currently, from R v1.8.0 there is a new implementation of trycatch (), which is a "wrapper" around the new tryCatch () function. If running R v1.7.1 or before the old trycatch () is used for backward compatibility.
If the value is 1 (one), then it is printed as soon as it has been encountered, while if the value is 2 (two), then immediately, the generated warning is converted into an error. tryCatch (…): The tryCatch () function helps evaluate the code and assign the exceptions. In this article, we will see how the tryCatch () function works.
FYI - irr is an actual function from FinCal in this case where got errors in a few cases on a large data set. Set up tryCatch as part of a function. For example: irr2 <- function (x) { out <- tryCatch (irr (x), error = function (e) NULL) return (out) } For the error (or warning) to work, you actually need to create a function.
How to write trycatch in R 1 Setting up the code. 2 Applying the code. 3 Investigating the output. 4 Additional remarks. More ...
Using try
or tryCatch
is not difficult. To read more about error handling, I suggest reading Hadley Wickham's new upcoming book's chapter Advanced R Programming: Exceptions and Debugging. Its just wonderful!
For your specific example, you can use one of these two functions bellow: using try
allows you to continue execution of a function call even if error occurs (which you can take note of later), while with tryCatch
you can specify error handling in advance:
select<-function(data, formula1, formula2){
fit1 <- try(lm(formula1, data))
fit2 <- lm(formula2, data)
if(is(fit1, "try-error")) fit2 else fit1
}
select1<-function(data, formula1, formula2){
tryCatch(lm(formula1, data), error = function(e) lm(formula2, data))
}
But that of course works if you know that only the first model can fail. There might be other scenarios, so think those over. Good luck!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With