Skip to contents

This 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 X if 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] -1.0493003 -0.2414721  0.8880010  1.2720706  1.4348667  0.7226149
#>  [7]  0.1949020 -0.6832334  0.2999400 -0.2060604
#> 
#> $fisher_information
#>                [,1]          [,2]          [,3]          [,4]          [,5]
#>  [1,]  1.598314e+01 -1.279838e+01  1.114382e+01  4.102916e+00  1.5153758199
#>  [2,] -1.279838e+01  2.422699e+01 -7.808594e+00 -1.308196e-01 -7.5661633056
#>  [3,]  1.114382e+01 -7.808594e+00  2.720672e+01  2.832963e+00 -2.5396020072
#>  [4,]  4.102916e+00 -1.308196e-01  2.832963e+00  6.552712e+00 -0.5337676945
#>  [5,]  1.515376e+00 -7.566163e+00 -2.539602e+00 -5.337677e-01 17.9996111621
#>  [6,] -1.602372e+01  1.734974e+01 -9.850515e+00 -2.963110e-01  1.6955593369
#>  [7,] -2.627289e+00  2.166753e+00  1.134921e+00 -6.399363e-01 -7.5759608398
#>  [8,] -7.302258e+00  2.720078e+00 -3.044497e+00  3.414965e-01 18.4233360615
#>  [9,]  1.487046e+00 -3.896146e+00 -1.943189e+00 -9.814531e-01 -8.2635348763
#> [10,]  1.286278e+01 -1.263959e+01  9.162636e+00  5.588362e-01  4.9106844081
#> [11,] -1.893954e-05 -2.167054e-05 -2.748439e-06 -2.353656e-06  0.0000137036
#>                [,6]          [,7]          [,8]          [,9]         [,10]
#>  [1,] -1.602372e+01 -2.627289e+00 -7.302258e+00  1.487046e+00  1.286278e+01
#>  [2,]  1.734974e+01  2.166753e+00  2.720078e+00 -3.896146e+00 -1.263959e+01
#>  [3,] -9.850515e+00  1.134921e+00 -3.044497e+00 -1.943189e+00  9.162636e+00
#>  [4,] -2.963110e-01 -6.399363e-01  3.414965e-01 -9.814531e-01  5.588362e-01
#>  [5,]  1.695559e+00 -7.575961e+00  1.842334e+01 -8.263535e+00  4.910684e+00
#>  [6,]  2.298888e+01 -7.431071e-01  1.572883e+01 -5.251816e+00 -1.689993e+01
#>  [7,] -7.431071e-01  1.207085e+01 -4.759070e+00  1.156822e+01 -1.574582e+00
#>  [8,]  1.572883e+01 -4.759070e+00  3.111394e+01 -1.040008e+01 -4.323446e+00
#>  [9,] -5.251816e+00  1.156822e+01 -1.040008e+01  1.982008e+01 -2.130090e+00
#> [10,] -1.689993e+01 -1.574582e+00 -4.323446e+00 -2.130090e+00  1.828977e+01
#> [11,]  1.177624e-05 -1.267319e-05  7.630130e-06 -1.171774e-05 -1.115984e-05
#>               [,11]
#>  [1,] -1.893954e-05
#>  [2,] -2.167054e-05
#>  [3,] -2.748439e-06
#>  [4,] -2.353656e-06
#>  [5,]  1.370360e-05
#>  [6,]  1.177624e-05
#>  [7,] -1.267319e-05
#>  [8,]  7.630130e-06
#>  [9,] -1.171774e-05
#> [10,] -1.115984e-05
#> [11,]  4.796574e-04
#>