# Posterior predictive samples in R using a negative binomial model fitted with Stan

**URL:** <https://discourse.mc-stan.org/t/posterior-predictive-samples-in-r-using-a-negative-binomial-model-fitted-with-stan/4862>\
**Category:** Modeling\
**Tags:** specification\
**Created:** [July 14, 2018, 12:55pm UTC](https://discourse.mc-stan.org/t/posterior-predictive-samples-in-r-using-a-negative-binomial-model-fitted-with-stan/4862 "2018-07-14T12:55:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pko](https://avatars.discourse-cdn.com/v4/letter/p/b5e925/32.png) [@pko](https://discourse.mc-stan.org/u/pko)\
**Post date:** [July 14, 2018, 12:55pm UTC](https://discourse.mc-stan.org/t/posterior-predictive-samples-in-r-using-a-negative-binomial-model-fitted-with-stan/4862/1 "2018-07-14T12:55:51Z")

</div>

Hi  
I’v fitted a neg binom model using Stan’s function neg\_binomial\_2\_lpmf(_y_ | _mu_, _phi_). _phi_ is the precision. Now I would like to create posterior predictive samples in R using rnbinom(_n_, _size_, _prob_, _mu_). It seems clear that the _mu_ from neg\_binomial\_2\_lpmf is the same as for rnbinom. Then, I guess I provide _size_, the “dispersion parameter” according to the rnbinom help file. But how is the link between the precision _phi_ and the dispersion parameter _size_? The help file says “The variance is mu + mu^2/size”. I can solve variance = _mu_+_mu_^2/_size_ = 1/precision = 1/_phi_ for _size_, but then I get negative values for _size_, hence something is wrong here. Is _phi_ simply 1/_size_?  
many thanks, Pius

---

<div class="post-metadata">

**Author:** ![bbbales2](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bbbales2/32/77_2.png) [@bbbales2](https://discourse.mc-stan.org/u/bbbales2)\
**Post date:** [July 14, 2018, 6:39pm UTC](https://discourse.mc-stan.org/t/posterior-predictive-samples-in-r-using-a-negative-binomial-model-fitted-with-stan/4862/2 "2018-07-14T18:39:55Z")

</div>

> [@pko](#):
>
> I would like to create posterior predictive samples in R

Nooo! You would like to create posterior predictive samples in a generated quantities block in Stan! Switching between probability distributions like this is error prone anyway. You can avoid that with generated quantities!

I got \phi = n which agrees with you. \phi is required to be positive in Stan and n is required to be positive in R, so where does the negative size come from?
