Calculating log likelihood for hierarchical negative binomial model

It should be, at least in the reference guide for Stan 2.17 it’s right under neg_binomial_2.

On top of all the confusion generated (to me it was at some point, at least) by all the different parameterizations of the negative binomial distribution, there could be another layer of confusion between those two distributions mentioned. Here’s what it should be, and I hope I’m getting this right:

  • y ~ neg_binomial_2(mu, phi) is the log of the ‘negative binomial likelihood’ of observation vector \mathbf{y} according to the parameterization in the reference guide, and it’s equivalent to a vectorized form of neg_binomial_2_lpmf(ints y | reals mu, reals phi), like the loop over y_rep[n]

  • y ~ neg_binomial_2_log(eta, phi) is an alternative parameterization where \eta = log(\mu), and is equivalent to looping over neg_binomial_2_log_lpmf(ints y | reals eta, reals phi).

Apparently the latter can be useful. if you are using a log link function like in a GLM or alike, so you can just use that function directly and it can be faster, but it’s otherwise equivalent to exponentiating the predicted function and using the former.

So @c5v, if you are using neg_binomial_2_log and neg_binomial_2_lpmf together as equivalents it would appear that you are mixing two different things, and it should be solve by using either pair of functions above (_cdf functions shoudl follow the same rule, although I’m not they are implemented for the latter).
Without further information or details of your model it’s not completely clear to me what you are trying to do with y_rep[n] = neg_binomial_2_log(eta_n, phi), if you are trying to get a random number than the suggestion above by @ScottAlder of using neg_binomial_2_rng should be what you are looking for.

2 Likes