
Fast Negative Binomial Regression, Estimate-Only (R Wrapper)
Source:R/helper_glm_fit.R
fast_negbin_regression.RdThis function provides a fast implementation of mean/dispersion-parameterized
negative binomial regression, wrapping a C++ backend
(fast_neg_bin_cpp; see that page, and
fast_dnbinom_mu_vec_cpp, for the full model). It returns point
estimates only (no standard errors) — see fast_negbin_regression_with_var
for the variance-computing counterpart. Columns 1 and 2 of X (conventionally
the intercept and treatment indicator) are always kept; this wrapper adds automatic,
silent handling of rank-deficient or numerically unstable covariate sets
beyond those first two columns: (1) if warm_start_params is supplied, a single
fit is attempted on the full matrix using the warm start, and its result is returned
if successful; (2) otherwise, upfront, any covariate columns (3 onward) found
rank-deficient by qr are dropped before the first fit attempt;
(3) if the C++ fit still fails (e.g. an L-BFGS line-search failure), covariates are
dropped one at a time, in reverse QR-pivot order (most redundant first), retrying
after each drop, until the fit succeeds or only the intercept and treatment columns
remain — at which point, if it still fails, this function stop()s with an
explicit error rather than returning a corrupted fit.
Usage
fast_negbin_regression(
X,
y,
optimization_alg = "lbfgs",
warm_start_params = NULL,
warm_start_fisher_info = NULL
)Arguments
- X
A numeric matrix of predictor variables. It is assumed that an intercept column (e.g., a column of ones) is already included in
Xif desired.- y
A numeric vector of the response variable, representing count data.
- optimization_alg
Optimization algorithm:
"lbfgs"(default) or"newton_raphson".- warm_start_params
Optional starting values for coefficients and
log_theta, passed straight through to the C++ backend for a single warm-started fit attempt on the fullX; if that fit succeeds its result is returned without the QR-dropping retry sequence, and if it errors the retry sequence runs as usual (without the warm start).- warm_start_fisher_info
Optional initial Fisher information matrix, used only together with
warm_start_params.
Value
A list containing the following components:
- b
A numeric vector of the estimated negative binomial regression coefficients \(\hat\beta\) (the dispersion is not included), from whichever (possibly covariate-reduced) fit in the retry sequence ultimately succeeded.
- fisher_information
The C++ backend's returned Fisher information matrix for the successful fit.
See also
fast_negbin_regression_with_var for the variance-computing,
similarly retry-hardened wrapper; fast_neg_bin_cpp for the underlying
backend and full model documentation.
Examples
set.seed(1)
n = 100
# intercept, treatment indicator, and two covariates
X = cbind(1, rbinom(n, 1, 0.5), matrix(rnorm(n * 2), n, 2))
y = rnbinom(n, mu = exp(0.5 + 0.4 * X[, 2] + 0.2 * X[, 3]), size = 2)
fast_negbin_regression(X, y)
#> $b
#> [1] 0.50265191 0.42805818 0.08968523 -0.09065440
#>
#> $fisher_information
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] 97.07919860 51.8879335 2.4334530 -2.686983 0.02981625
#> [2,] 51.88793347 51.8879335 6.7085655 -3.577829 -0.18874386
#> [3,] 2.43345299 6.7085655 80.1797525 7.757210 -0.21721007
#> [4,] -2.68698303 -3.5778292 7.7572101 107.501301 0.49842398
#> [5,] 0.02981625 -0.1887439 -0.2172101 0.498424 10.75196777
#>