A built-in negative-binomial family for use with [fastglm()] and [fastglmPure()] when `theta` (the NB2 dispersion) is known. Equivalent to `MASS::negative.binomial(theta, link)` but without taking a hard dependency on the **MASS** package. The variance is `mu + mu^2 / theta`; as `theta -> Inf` it reduces to Poisson.

negbin(theta, link = "log")

Arguments

theta

the (known) dispersion parameter; must be positive.

character; one of `"log"` (default), `"sqrt"`, or `"identity"`.

Value

A `family` object with class `"family"` and `family$family == "Negative Binomial(theta)"`. The object also carries the slot `family$theta`, which `family_code()` and the dispatch layer use to detect the native NB fast path.

Details

For joint estimation of `theta` together with `beta`, use the dedicated (forthcoming) `fastglm_nb()` entry point. The `negbin()` family here is intended for use cases where `theta` has been pre-specified or estimated separately.

Examples

set.seed(1)
n <- 500
x  <- cbind(1, matrix(rnorm(n * 2), n, 2))
mu <- exp(x %*% c(0.3, 0.4, -0.2))
y  <- rpois(n, lambda = mu * rgamma(n, shape = 2, rate = 2))   # NB(theta=2)

fit <- fastglm(x, y, family = negbin(theta = 2, link = "log"))
coef(fit)
#> (Intercept)          X1          X2 
#>   0.2748751   0.4914446  -0.2048106