
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),
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 and inverted via a plain
matrix inverse (.inverse(), not a rank-aware pseudo-inverse as used
by, e.g., fast_adjacent_category_logit_with_var_cpp) before
being expanded back to the full (p + 1) x (p + 1) size as
vcov. A rank-deficient or near-singular design (after restricting to
free parameters) will therefore produce numerically unstable or NaN
variances rather than a graceful fallback.
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] -5.5535274 6.4091499 2.6718152 2.6447782 -0.7615799 5.2675376
#> [7] 0.5837481 -8.7495596 -17.2772539 -10.2864877
#>
#> $theta_hat
#> [1] 1094981920
#>
#> $logLik
#> [1] -15.18427
#>
#> $converged
#> [1] TRUE
#>
#> $num_iter
#> [1] 76
#>
#> $hit_iteration_cap
#> [1] FALSE
#>
#> $gradient_norm
#> [1] 0.02793997
#>
#> $min_eigenvalue_information
#> [1] NaN
#>
#> $dispersion_at_poisson_boundary
#> [1] FALSE
#>
#> $hess_fisher_info_matrix
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] 1.408460e+01 -1.685258e+00 2.505326e+00 -2.436189e+00 1.137067e+01
#> [2,] -1.685258e+00 8.569259e+00 -3.513348e+00 -9.310828e+00 -1.030916e+01
#> [3,] 2.505326e+00 -3.513348e+00 4.705939e+01 -7.600849e+00 2.890151e+00
#> [4,] -2.436189e+00 -9.310828e+00 -7.600849e+00 3.682673e+01 2.279544e+01
#> [5,] 1.137067e+01 -1.030916e+01 2.890151e+00 2.279544e+01 7.072506e+01
#> [6,] 1.938622e-01 9.859119e+00 7.163678e+00 -1.038931e+01 -1.807038e+00
#> [7,] 7.500484e+00 -8.914326e-01 2.652133e+01 -2.199734e+01 -3.506756e-01
#> [8,] -4.764506e+00 1.032350e+01 2.143270e+01 -1.460305e+01 1.151428e+00
#> [9,] -7.280647e+00 -1.609342e+00 -2.370612e-02 1.315614e+01 1.467770e+00
#> [10,] 6.761537e+00 2.641441e+00 -7.357906e+00 -1.530877e+01 -1.786101e+01
#> [11,] 2.140978e-11 -2.463651e-13 3.619509e-11 -2.185004e-11 -3.817267e-11
#> [,6] [,7] [,8] [,9] [,10]
#> [1,] 1.938622e-01 7.500484e+00 -4.764506e+00 -7.280647e+00 6.761537e+00
#> [2,] 9.859119e+00 -8.914326e-01 1.032350e+01 -1.609342e+00 2.641441e+00
#> [3,] 7.163678e+00 2.652133e+01 2.143270e+01 -2.370612e-02 -7.357906e+00
#> [4,] -1.038931e+01 -2.199734e+01 -1.460305e+01 1.315614e+01 -1.530877e+01
#> [5,] -1.807038e+00 -3.506756e-01 1.151428e+00 1.467770e+00 -1.786101e+01
#> [6,] 2.464169e+01 8.738664e+00 2.038722e+01 6.664992e+00 -1.049122e+01
#> [7,] 8.738664e+00 4.452260e+01 1.639023e+01 -1.253240e+01 1.160082e+01
#> [8,] 2.038722e+01 1.639023e+01 4.108681e+01 -3.435348e+00 -7.851036e+00
#> [9,] 6.664992e+00 -1.253240e+01 -3.435348e+00 2.031220e+01 -2.222669e+01
#> [10,] -1.049122e+01 1.160082e+01 -7.851036e+00 -2.222669e+01 3.380938e+01
#> [11,] 1.799285e-11 4.121886e-11 2.942159e-11 5.678107e-12 -1.768595e-11
#> [,11]
#> [1,] 2.140978e-11
#> [2,] -2.463651e-13
#> [3,] 3.619509e-11
#> [4,] -2.185004e-11
#> [5,] -3.817267e-11
#> [6,] 1.799285e-11
#> [7,] 4.121886e-11
#> [8,] 2.942159e-11
#> [9,] 5.678107e-12
#> [10,] -1.768595e-11
#> [11,] 9.398988e-06
#>
#> $vcov
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] 1.829840e+00 -7.011402e+00 -9.410939e-01 -1.402299e+00 2.226417e-01
#> [2,] -7.011402e+00 6.448245e+01 5.729446e+00 9.943168e+00 -9.679961e-01
#> [3,] -9.410939e-01 5.729446e+00 6.567335e-01 9.763596e-01 -1.165606e-01
#> [4,] -1.402299e+00 9.943168e+00 9.763596e-01 1.739573e+00 -2.420944e-01
#> [5,] 2.226417e-01 -9.679961e-01 -1.165606e-01 -2.420944e-01 9.447547e-02
#> [6,] 4.374795e-01 -1.504654e+01 -9.461926e-01 -1.877397e+00 3.709398e-02
#> [7,] -1.726978e+00 1.637389e+01 1.399961e+00 2.537158e+00 -2.608159e-01
#> [8,] 3.841848e+00 -2.476228e+01 -2.571085e+00 -4.214534e+00 5.610673e-01
#> [9,] 5.751735e+00 -3.104477e+01 -3.486902e+00 -5.715806e+00 9.577810e-01
#> [10,] 4.861399e+00 -3.484455e+01 -3.399322e+00 -5.813569e+00 8.069721e-01
#> [11,] -2.700666e-06 2.479803e-06 9.913580e-07 9.295910e-07 -6.421313e-09
#> [,6] [,7] [,8] [,9] [,10]
#> [1,] 4.374795e-01 -1.726978e+00 3.841848e+00 5.751735e+00 4.861399e+00
#> [2,] -1.504654e+01 1.637389e+01 -2.476228e+01 -3.104477e+01 -3.484455e+01
#> [3,] -9.461926e-01 1.399961e+00 -2.571085e+00 -3.486902e+00 -3.399322e+00
#> [4,] -1.877397e+00 2.537158e+00 -4.214534e+00 -5.715806e+00 -5.813569e+00
#> [5,] 3.709398e-02 -2.608159e-01 5.610673e-01 9.577810e-01 8.069721e-01
#> [6,] 5.060475e+00 -3.899040e+00 4.345709e+00 4.235991e+00 6.753729e+00
#> [7,] -3.899040e+00 4.230201e+00 -6.230792e+00 -7.791962e+00 -8.848956e+00
#> [8,] 4.345709e+00 -6.230792e+00 1.091158e+01 1.483929e+01 1.477061e+01
#> [9,] 4.235991e+00 -7.791962e+00 1.483929e+01 2.142895e+01 1.995581e+01
#> [10,] 6.753729e+00 -8.848956e+00 1.477061e+01 1.995581e+01 2.051496e+01
#> [11,] 1.996668e-06 2.840109e-07 -3.339906e-06 -5.669579e-06 -2.945407e-06
#> [,11]
#> [1,] -2.700666e-06
#> [2,] 2.479803e-06
#> [3,] 9.913580e-07
#> [4,] 9.295910e-07
#> [5,] -6.421313e-09
#> [6,] 1.996668e-06
#> [7,] 2.840109e-07
#> [8,] -3.339906e-06
#> [9,] -5.669579e-06
#> [10,] -2.945407e-06
#> [11,] 1.063944e+05
#>