This is an old version, view current version.

## 14.4 Negative-Binomial-2-Log Generalized Linear Model (Negative Binomial Regression)

Stan also supplies a single function for a generalized linear model with negative binomial likelihood and log link function, i.e. a function for a negative binomial regression. This provides a more efficient implementation of negative binomial regression than a manually written regression in terms of a negative binomial likelihood and matrix multiplication.

### 14.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).$

### 14.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.

### 14.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 parameter phi, where the same intercept alpha, inverse overdispersion parameter phi and dependant variable y are used for all observations. The number of columns of x needs to match the size of the coefficient vector beta. If x and y are data (not parameters) this function can be executed on a GPU.

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 parameter phi, where the same inverse overdispersion parameter phi and independent variable value y are used for all observations. Intercept alpha is used that is allowed to vary by observation. The number of rows of x must match the size of alpha and the number of columns of x needs to match the size of the coefficient vector beta. If x and y are data (not parameters) this function can be executed on a GPU.

real neg_binomial_2_log_glm_lpmf(int[] y | row_vector x, real alpha, vector beta, real phi)
The log negative binomial probability mass of y given log-location alpha + x * beta and inverse overdispersion parameter phi, where the same intercept alpha, inverse overdispersion parameter phi and independent variable values x are used for all observations. The number of columns of x needs to match the size of the coefficient vector beta.

real neg_binomial_2_log_glm_lpmf(int[] y | row_vector x, vector alpha, vector beta, real phi)
The log negative binomial probability mass of y given log-location alpha + x * beta and inverse overdispersion parameter phi, where the same inverse overdispersion parameter phi and independent variable values x are used for all observations. Intercept alpha is used that is allowed to vary by observation. The size of y must match the size of alpha and the number of columns of x needs to match the size of the coefficient vector beta.

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 parameter phi, where the same intercept alpha and inverse overdispersion parameter phi is used for all observations. The number of rows of the independent variable matrix x needs to match the size of the dependent variable vector y and the number of columns of x needs to match the size of the coefficient vector beta. If x and y are data (not parameters) this function can be executed on a GPU.

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 parameter phi, where the same inverse overdispersion parameter phi is used for all observations and an intercept alpha is used that is allowed to vary by observation. The number of rows of the independent variable matrix x needs to match the size of the dependent variable vector y and alpha and the number of columns of x needs to match the size of the coefficient vector beta. If x and y are data (not parameters) this function can be executed on a GPU.