# Generated Quantities in STAN: negative binomial rng [closed]

I'm working with a Negative binomial regression in STAN.

I would like to make predictions on a test set, but looking at the reference I can't find a negative_binomial random number generator.

Is there any way to do so without saving mean and overdispersion distributions and sampling from them in R ?

## closed as off-topic by whuber♦Mar 28 '17 at 15:05

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "This question appears to be off-topic because EITHER it is not about statistics, machine learning, data analysis, data mining, or data visualization, OR it focuses on programming, debugging, or performing routine operations within a statistical computing platform. If the latter, you could try the support links we maintain." – whuber
If this question can be reworded to fit the rules in the help center, please edit the question.

• I think you need to add the self-study tag. – Michael Chernick Mar 27 '17 at 22:31
• Also since you are mainly looking for information on two software packages (STAN and R), the question is off topic for this site. – Michael Chernick Mar 27 '17 at 22:41
• Ok, I'll move to stackoverflow and add the self study tag there, thank you for raising my awareness about this site :) – Tommaso Guerrini Mar 27 '17 at 22:42

From the Stan User's Guide (2.14.0), it appears you should be able to generate samples from a negative binomial using:

neg_binomial_rng(real alpha, real beta)

You can draw from this distribution on each step of your chain by including it in the normal manner in your generated quantities block, e.g.

generated quantities { vector[N] y_rep; for (n in 1:N) { y_rep[n] = neg_binomial_rng(alpha, beta); } }

Where alpha and beta are your parameter estimates at each step, and N is the total number of samples to generate - specified as input to the data block.

• Why can't I find things on the Stan reference remains a mistery. Thank you!! – Tommaso Guerrini Mar 27 '17 at 22:46
• @TommasoGuerrini No problem. FWIW, you can also specify using alternate parameterizations neg_binomial_2_rng(real mu, real phi) and neg_binomial_2_log_rng(real eta, real phi) which take precision and logged parameters, respectively. – khonegger Mar 27 '17 at 22:52
• Yep, actually I used the one with the overdispersion and log, thank you again! – Tommaso Guerrini Mar 27 '17 at 22:57