1

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.

Srivatsa Srinath
  • 177
  • 2
  • 10
  • Thanks for your response. However, using withCallingHandlers instead of tryCatch isn't helping me, the issue is still that fitdist behaves differently while inside the error handler and outside. – Srivatsa Srinath Dec 19 '15 at 03:37
  • Thank your prompt reponse @slickrickulicious, I removed the warning handler and the result did contain the result of the fitted distribution. I would like to understand the warning message that R throws up though for fitdist, can you help me? Maybe I should open a different question for the same ... – Srivatsa Srinath Dec 19 '15 at 04:18

0 Answers0