This is an old version, view current version.

## 13.4 Negative-Binomial-2-Log Generalised Linear Model (Negative Binomial Regression)

Stan also supplies a single primitive for a Generalised Linear Model with negative binomial likelihood and log link function, i.e. a primitive for a negative binomial regression. This should provide a more efficient implementation of negative binomial regression than a manually written regression in terms of a negative binomial likelihood and matrix multiplication.

### 13.4.1 Probability Mass Function

If $$x\in \mathbb{R}^{n\cdot m}, \alpha \in \mathbb{R}^n, \beta\in \mathbb{R}^m, \phi\in \mathbb{R}^+$$, then for $$y \in \mathbb{N}^n$$, $\text{NegBinomial2LogGLM}(y~|~x, \alpha, \beta, \phi) = \prod_{1\leq i \leq n}\text{NegBinomial2}(y_i~|~\exp(\alpha_i + x_i\cdot \beta), \phi).$

### 13.4.2 Sampling Statement

y ~ neg_binomial_2_log_glm(x, alpha, beta, phi)

Increment target log probability density with neg_binomial_2_log_glm_lpmf( y | x, alpha, beta, phi) dropping constant additive terms.

### 13.4.3 Stan Functions

real neg_binomial_2_log_glm_lpmf(int[] y | matrix x, real alpha, vector beta, real phi)
The log negative binomial probability mass of y given log-location alpha+x*beta and inverse overdispersion control phi, where a constant intercept alpha and phi is used for all observations. The number of rows of the independent variable matrix x needs to match the length of the dependent variable vector y and the number of columns of x needs to match the length of the weight vector beta.

real neg_binomial_2_log_glm_lpmf(int[] y | matrix x, vector alpha, vector beta, real phi)
The log negative binomial probability mass of y given log-location alpha+x*beta and inverse overdispersion control phi, where a constant phi is used for all observations and an intercept alpha is used that is allowed to vary with the different observations. The number of rows of the independent variable matrix x needs to match the length of the dependent variable vector y and alpha and the number of columns of x needs to match the length of the weight vector beta.