I am trying to compute the log-likelihood in a Poisson regression in R. However, my computation produces negative infinity values for some observations. This is my code:
freq*exposure * log(lambda)- lambda - log(factorial(freq*exposure))
the $-\infty$ occurs in the
factorial(freq*exposure) portion when the frequency and exposure are too large.
Does anyone know how to get around this? When I discretize my data into smaller bins the issue disappears, however, I get a worse fit.
The error occurs when I use these observations:
exposure = 2.994500e+03 frequency = 0.13224244 exposure*frequency = 396
Based on @StupidWolf's suggestion I changed the
log(factorial(freq*exposure)) in the log-likelihood to:
freq*exposure * log(lambda)- lambda - lfactorial(freq*exposure)
this solved the issue.