I would like to understand the behaviour of tryCatch in my sample code:
require(fitdistrplus)
a = as.data.frame(table(rpois(n = 1000,lambda = 6)))
fitdist(data = a$Freq,distr = "nbinom",discrete = TRUE)
result = tryCatch(expr = (fitdist(data = a$Freq, distr = "nbinom",discrete = TRUE)),
error = function(err) {print(paste("MY_ERROR: ",err))},
warning = function(wrr) {print(paste("MY_WARNING: ",wrr))})
The fitdist
command when executed outside of the tryCatch
command gives the parameters associated with the nbinom
fit. However, when I do the same within the try catch command I am getting the following warning,
[1] "MY_WARNING: simpleWarning in dnbinom(c(3L, 18L, 37L, 77L, 125L, 152L, 163L, 168L, 104L, 77L, : NaNs produced\n"
Can someone please help me understand this observation?
EDIT: I created this test case as a sample of an issue that I am facing while I a running a loop to fit distributions across multiple column of a big dataset. I need to use tryCatch to iterate over multiple distributions and distribution fit methods like mle, mme e.t.c. The main issue I am facing is that fitdist is behaving as expected while executed standlone but not inside a tryCatch command.