
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 (bypassing the QR-dropping retry sequence).- 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\) (and
log_theta), 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
X = matrix(rnorm(100), 10, 10)
y = rpois(10, 2)
fast_negbin_regression(X, y)
#> $b
#> [1] -0.9596045 -5.3394862 -4.6358347 2.9344194 -3.8877138 3.1351174
#> [7] 1.1038271 -1.8453900 -1.3101352 2.1310655
#>
#> $fisher_information
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] 2.324664e+01 4.714559e+00 -8.039031e+00 3.040424e+00 -4.615123e+00
#> [2,] 4.714559e+00 1.175361e+01 -8.048800e+00 -3.288965e+00 -9.224357e-01
#> [3,] -8.039031e+00 -8.048800e+00 1.469061e+01 2.980926e+00 6.306473e-01
#> [4,] 3.040424e+00 -3.288965e+00 2.980926e+00 8.020267e+00 -1.453199e+00
#> [5,] -4.615123e+00 -9.224357e-01 6.306473e-01 -1.453199e+00 2.890236e+00
#> [6,] -3.086728e+00 4.179218e+00 5.780677e-01 -5.952127e+00 5.393942e-01
#> [7,] -1.371449e+00 1.012091e+00 1.554572e-01 -2.334988e+00 7.496043e-01
#> [8,] -2.024982e+00 -5.707578e+00 4.818658e+00 -1.281909e+00 1.181998e+00
#> [9,] 6.467103e+00 -2.473180e+00 -1.085123e+00 1.178830e+00 -1.331548e+00
#> [10,] -6.741789e+00 -7.291201e-01 1.157921e+01 -4.164722e+00 4.001628e+00
#> [11,] 4.077667e-14 -2.357524e-14 7.537572e-14 -1.178387e-14 2.740840e-14
#> [,6] [,7] [,8] [,9] [,10]
#> [1,] -3.086728e+00 -1.371449e+00 -2.024982e+00 6.467103e+00 -6.741789e+00
#> [2,] 4.179218e+00 1.012091e+00 -5.707578e+00 -2.473180e+00 -7.291201e-01
#> [3,] 5.780677e-01 1.554572e-01 4.818658e+00 -1.085123e+00 1.157921e+01
#> [4,] -5.952127e+00 -2.334988e+00 -1.281909e+00 1.178830e+00 -4.164722e+00
#> [5,] 5.393942e-01 7.496043e-01 1.181998e+00 -1.331548e+00 4.001628e+00
#> [6,] 9.403058e+00 4.554959e+00 3.668448e+00 -9.003663e-01 5.572128e+00
#> [7,] 4.554959e+00 3.977252e+00 1.979870e+00 -1.179100e+00 -1.100780e+00
#> [8,] 3.668448e+00 1.979870e+00 1.251983e+01 4.563102e+00 1.019889e+01
#> [9,] -9.003663e-01 -1.179100e+00 4.563102e+00 7.440602e+00 4.197750e-01
#> [10,] 5.572128e+00 -1.100780e+00 1.019889e+01 4.197750e-01 3.718477e+01
#> [11,] -2.206143e-14 4.390827e-15 -5.909941e-14 -3.394793e-14 9.878728e-14
#> [,11]
#> [1,] 4.077667e-14
#> [2,] -2.357524e-14
#> [3,] 7.537572e-14
#> [4,] -1.178387e-14
#> [5,] 2.740840e-14
#> [6,] -2.206143e-14
#> [7,] 4.390827e-15
#> [8,] -5.909941e-14
#> [9,] -3.394793e-14
#> [10,] 9.878728e-14
#> [11,] -7.367194e-02
#>