`fastglm_nb()` fits a negative-binomial regression model, jointly maximising the likelihood over the regression coefficients `beta` and the NB2 dispersion `theta`. It is the fastglm analogue of [MASS::glm.nb()], Dense fits use native coefficient and size updates. Sparse and streamed fits alternate the existing GLM solver with size updates accumulated over row blocks, without loading the full design into a dense matrix.

fastglm_nb(
  x,
  y = NULL,
  weights = NULL,
  offset = NULL,
  start = NULL,
  init.theta = NULL,
  link = c("log", "sqrt", "identity"),
  method = 2L,
  tol = 1e-08,
  maxit = 100L,
  outer.maxit = 50L,
  outer.tol = 1e-07,
  theta.tol = 1e-08,
  theta.maxit = 100L,
  block.size = 8192L
)

Arguments

x

numeric matrix, `dgCMatrix`, or [fastglm_source()] supplying rows on demand. A source must supply its own responses, weights, and offsets.

y

non-negative integer response vector; omitted for a data source.

weights

optional prior weights vector of length `length(y)`.

offset

optional offset vector of length `length(y)`.

start

optional starting values for `beta`.

init.theta

optional starting value for `theta`. If `NULL`, dense fits use a moment initializer; sparse and source fits start at one after fitting the Poisson mean model.

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

method

integer; `0..5`, see [fastglm()].

tol

convergence tolerance for the IRLS inner loop.

maxit

maximum number of inner-loop IRLS iterations.

outer.maxit

maximum number of `(beta, theta)` outer iterations.

outer.tol

convergence tolerance for the outer loop on the sup-norm of the beta update plus the relative change in `theta`.

theta.tol

tolerance for the inner theta update.

theta.maxit

max iterations for the inner theta MLE.

block.size

maximum rows processed together for sparse-model size estimation and joint inference. A source determines its own block sizes.

Value

A list of class `c("fastglm_nb", "fastglm")` with the usual fastglm components plus `theta`, `SE.theta`, `iter.theta`, and `twologlik` (twice the maximized NB log-likelihood). `SE.theta` uses the joint observed information for coefficients and theta. The default coefficient covariance retains the GLM expected-information convention; `vcov(fit, full=TRUE)` returns the joint observed covariance.

Examples

set.seed(1)
n <- 500
x <- cbind(1, matrix(rnorm(n * 2), n, 2))
eta <- x %*% c(0.3, 0.5, -0.2)
mu  <- exp(eta)
if (requireNamespace("MASS", quietly = TRUE)) {
  y   <- MASS::rnegbin(n, mu = mu, theta = 2)
  fit <- fastglm_nb(x, y)
  c(theta = fit$theta, fit$coefficients)
}
#>      theta         x1         x2         x3 
#>  1.9625479  0.3059098  0.5879277 -0.1601587