
Fast Negative Binomial Regression with Variance Calculation (C++ Backend)
Source:R/helper_glm_fit.R, R/RcppExports.R
fast_neg_bin_with_var_cpp.RdFits the same mean/dispersion-parameterized negative-binomial regression as
fast_neg_bin_cpp (see that page, and
fast_dnbinom_mu_vec_cpp, for the full model) and additionally
computes the full variance-covariance matrix of c(beta, log(theta)).
Usage
fast_neg_bin_with_var_cpp(
X,
y,
warm_start_params = NULL,
smart_cold_start = FALSE,
maxit = 1000L,
eps_f = 1e-08,
eps_g = 1e-06,
fixed_idx = NULL,
fixed_values = NULL,
optimization_alg = "lbfgs",
warm_start_fisher_info = NULL,
estimate_only = FALSE
)Arguments
- X
A numeric matrix of predictors, \(n \times p\).
- y
A numeric (integer-valued) vector of non-negative observed counts, length \(n\).
- warm_start_params
Optional starting values for coefficients and dispersion. If provided,
smart_cold_startis ignored.- smart_cold_start
Logical. If TRUE, use an initial OLS-based guess when starting from scratch (a "cold start") with no prior knowledge. This is ignored if a warm start is provided.
- maxit
Maximum number of optimizer iterations.
- eps_f
Convergence tolerance on the objective (log-likelihood) value.
- eps_g
Convergence tolerance on the gradient norm.
- fixed_idx
Optional integer indices (into the
c(beta, log(theta))parameter layout) of parameters to hold fixed rather than estimate.- fixed_values
Optional values to fix the parameters named by
fixed_idxat.- optimization_alg
Optimization algorithm:
"lbfgs"(default) or"newton_raphson".- warm_start_fisher_info
Optional initial Fisher Information matrix to warm-start curvature information.
Value
A list containing the following components:
- b
A numeric vector of the obtained Poisson regression coefficients.
- hess_fisher_info_matrix
The Fisher information matrix.
- neg_ll
The negative log-likelihood at the final iteration.
- converged
A logical value indicating whether the final gradient norm was below the convergence tolerance (
gradient_norm < tol); uniform across the"lbfgs"/"newton_raphson"optimizers.- num_iter
The number of optimizer iterations performed.
- hit_iteration_cap
A logical value, mutually exclusive with
converged:TRUEiff the optimizer exhaustedmaxititerations without meeting the gradient-norm convergence criterion.- gradient_norm
The norm of the gradient at the returned parameters.
A list with components b (\(\hat\beta\)),
theta_hat (\(\hat\theta\)), logLik, vcov (the full
(p + 1) x (p + 1) parameter variance-covariance matrix),
information_invertible (whether its free information block passed
the invertibility check),
converged, iterations, and hess_fisher_info_matrix
(the working-weights curvature matrix vcov was inverted from).
Details
Variance computation. The working-weights curvature matrix
res.XtWX (over all p + 1 parameters) is restricted to the
free (non-fixed_idx) parameters. A FullPivLU invertibility
check guards the same plain matrix inverse used previously, so invertible
fits retain their existing covariance values while a singular or non-finite
free block returns a NaN covariance. Callers should check
is.finite(vcov) before using its entries.
See also
fast_neg_bin_cpp for the estimate-only variant and
full model documentation; fast_neg_bin_weighted_cpp for the
row-weighted estimate-only variant.
Examples
X = matrix(rnorm(100), 10, 10)
y = rpois(10, 2)
fast_neg_bin_with_var_cpp(X, y)
#> $b
#> [1] -0.6046581 -0.9185522 3.4926759 3.7390077 0.9959934 -0.1752641
#> [7] 2.6662493 -1.9580572 4.7232755 -5.1020716
#>
#> $theta_hat
#> [1] 5945276
#>
#> $logLik
#> [1] -11.66992
#>
#> $converged
#> [1] TRUE
#>
#> $num_iter
#> [1] 60
#>
#> $hit_iteration_cap
#> [1] FALSE
#>
#> $gradient_norm
#> [1] 0.02391691
#>
#> $min_eigenvalue_information
#> [1] NaN
#>
#> $dispersion_at_poisson_boundary
#> [1] FALSE
#>
#> $information_invertible
#> [1] TRUE
#>
#> $hess_fisher_info_matrix
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] 2.294478e+01 -7.847226e+00 -2.816324e+00 -5.462212e+00 5.215446e+00
#> [2,] -7.847226e+00 1.184595e+01 -4.722332e+00 9.066051e+00 -3.802423e+00
#> [3,] -2.816324e+00 -4.722332e+00 1.429149e+01 -1.188275e+01 1.340130e-01
#> [4,] -5.462212e+00 9.066051e+00 -1.188275e+01 2.995602e+01 -2.264010e+00
#> [5,] 5.215446e+00 -3.802423e+00 1.340130e-01 -2.264010e+00 1.551828e+01
#> [6,] -1.021573e+01 1.482848e+00 -3.408698e+00 7.489872e+00 -4.769824e+00
#> [7,] -4.288844e+00 1.903619e+00 -1.692995e+00 2.181457e+00 -8.056131e+00
#> [8,] 3.600625e+00 -6.706271e+00 4.831086e+00 -9.099955e-01 2.142658e-01
#> [9,] -3.014193e+00 4.849642e+00 5.413902e+00 -6.627341e+00 -1.711735e+00
#> [10,] -1.569689e+01 1.221533e+01 3.446787e+00 1.052159e+01 -5.261873e+00
#> [11,] 9.371242e-09 -9.128390e-09 1.614789e-09 -4.592752e-09 2.356335e-10
#> [,6] [,7] [,8] [,9] [,10]
#> [1,] -1.021573e+01 -4.288844e+00 3.600625e+00 -3.014193e+00 -1.569689e+01
#> [2,] 1.482848e+00 1.903619e+00 -6.706271e+00 4.849642e+00 1.221533e+01
#> [3,] -3.408698e+00 -1.692995e+00 4.831086e+00 5.413902e+00 3.446787e+00
#> [4,] 7.489872e+00 2.181457e+00 -9.099955e-01 -6.627341e+00 1.052159e+01
#> [5,] -4.769824e+00 -8.056131e+00 2.142658e-01 -1.711735e+00 -5.261873e+00
#> [6,] 3.020067e+01 7.656353e+00 -1.216376e+01 -4.833828e+00 8.428070e+00
#> [7,] 7.656353e+00 9.226949e+00 1.501023e+00 -2.751527e+00 1.516851e+00
#> [8,] -1.216376e+01 1.501023e+00 1.988648e+01 -2.626164e+00 -6.442747e+00
#> [9,] -4.833828e+00 -2.751527e+00 -2.626164e+00 1.419431e+01 1.190579e+01
#> [10,] 8.428070e+00 1.516851e+00 -6.442747e+00 1.190579e+01 2.657836e+01
#> [11,] -5.992821e-09 -9.505400e-10 5.221635e-09 -6.462631e-09 -1.288458e-08
#> [,11]
#> [1,] 9.371242e-09
#> [2,] -9.128390e-09
#> [3,] 1.614789e-09
#> [4,] -4.592752e-09
#> [5,] 2.356335e-10
#> [6,] -5.992821e-09
#> [7,] -9.505400e-10
#> [8,] 5.221635e-09
#> [9,] -6.462631e-09
#> [10,] -1.288458e-08
#> [11,] 1.677425e-06
#>
#> $vcov
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] 1.538547e+02 6.611554102 -4.629346e+02 -5.759209e+02 -1.706233e+02
#> [2,] 6.611554e+00 1.595904679 -1.902662e+01 -2.424586e+01 -7.283306e+00
#> [3,] -4.629346e+02 -19.026622589 1.394627e+03 1.734387e+03 5.137759e+02
#> [4,] -5.759209e+02 -24.245856391 1.734387e+03 2.157345e+03 6.391017e+02
#> [5,] -1.706233e+02 -7.283305730 5.137759e+02 6.391017e+02 1.894835e+02
#> [6,] -4.658580e+01 -1.340200015 1.406672e+02 1.747366e+02 5.169171e+01
#> [7,] -3.897514e+02 -17.051117546 1.173442e+03 1.459826e+03 4.326737e+02
#> [8,] 2.131783e+02 9.502643613 -6.417213e+02 -7.983792e+02 -2.365807e+02
#> [9,] -7.941748e+02 -33.258348051 2.391567e+03 2.974801e+03 8.812779e+02
#> [10,] 7.865150e+02 32.394685425 -2.368834e+03 -2.946233e+03 -8.727399e+02
#> [11,] -6.348205e-05 0.001366067 4.244463e-04 2.060668e-04 1.719102e-04
#> [,6] [,7] [,8] [,9] [,10]
#> [1,] -4.658580e+01 -3.897514e+02 2.131783e+02 -7.941748e+02 7.865150e+02
#> [2,] -1.340200e+00 -1.705112e+01 9.502644e+00 -3.325835e+01 3.239469e+01
#> [3,] 1.406672e+02 1.173442e+03 -6.417213e+02 2.391567e+03 -2.368834e+03
#> [4,] 1.747366e+02 1.459826e+03 -7.983792e+02 2.974801e+03 -2.946233e+03
#> [5,] 5.169171e+01 4.326737e+02 -2.365807e+02 8.812779e+02 -8.727399e+02
#> [6,] 1.455275e+01 1.178383e+02 -6.434055e+01 2.411094e+02 -2.390202e+02
#> [7,] 1.178383e+02 9.885074e+02 -5.405997e+02 2.012896e+03 -1.993270e+03
#> [8,] -6.434055e+01 -5.405997e+02 2.957928e+02 -1.100766e+03 1.090018e+03
#> [9,] 2.411094e+02 2.012896e+03 -1.100766e+03 4.102267e+03 -4.062837e+03
#> [10,] -2.390202e+02 -1.993270e+03 1.090018e+03 -4.062837e+03 4.024130e+03
#> [11,] 7.369761e-04 -3.760330e-04 5.046174e-04 7.926552e-04 -9.239014e-04
#> [,11]
#> [1,] -6.348205e-05
#> [2,] 1.366067e-03
#> [3,] 4.244463e-04
#> [4,] 2.060668e-04
#> [5,] 1.719102e-04
#> [6,] 7.369761e-04
#> [7,] -3.760330e-04
#> [8,] 5.046174e-04
#> [9,] 7.926552e-04
#> [10,] -9.239014e-04
#> [11,] 5.961519e+05
#>