Skip to contents

Fits 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), with each observation's contribution to the log-likelihood multiplied by a nonnegative row weight weights[i]. Setting all weights to 1 recovers fast_neg_bin_cpp exactly.

Usage

fast_neg_bin_weighted_cpp(
  X,
  y,
  weights,
  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\).

weights

A nonnegative numeric vector of length \(n\) giving each row's weight.

warm_start_params

Optional starting values for coefficients and dispersion. If provided, smart_cold_start is 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_idx at.

optimization_alg

Optimization algorithm: "lbfgs" (default) or "newton_raphson".

warm_start_fisher_info

Optional initial Fisher Information matrix to warm-start curvature information.

estimate_only

If TRUE, skip Fisher information calculation.

Value

A list with the same components as fast_neg_bin_cpp: b, theta_hat, logLik, converged, iterations, and fisher_information (all reflecting the weighted log-likelihood).

See also

fast_neg_bin_cpp for the unweighted model and full documentation.

Examples

X = matrix(rnorm(100), 10, 10)
y = rpois(10, 2)
fast_neg_bin_weighted_cpp(X, y, weights = rep(1, 10))
#> $b
#>  [1] -0.81438388 -0.52829207  3.81750031 -1.07439036  0.64576731 -0.01877717
#>  [7] -1.00407514  1.54623564 -1.51721790  1.83034277
#> 
#> $theta_hat
#> [1] 291936.1
#> 
#> $logLik
#> [1] -12.12116
#> 
#> $converged
#> [1] TRUE
#> 
#> $num_iter
#> [1] 72
#> 
#> $hit_iteration_cap
#> [1] FALSE
#> 
#> $gradient_norm
#> [1] 0.01211137
#> 
#> $min_eigenvalue_information
#> [1] NaN
#> 
#> $dispersion_at_poisson_boundary
#> [1] FALSE
#> 
#> $fisher_information
#>                [,1]          [,2]          [,3]          [,4]          [,5]
#>  [1,]  5.045558e+01  9.012699e+00  1.367873e+01  1.350032e+01 -3.128901e+00
#>  [2,]  9.012699e+00  2.152030e+01  7.531589e+00 -3.931314e+00  7.421464e+00
#>  [3,]  1.367873e+01  7.531589e+00  1.413124e+01  1.076319e+01  1.126055e+00
#>  [4,]  1.350032e+01 -3.931314e+00  1.076319e+01  1.605902e+01 -3.440238e+00
#>  [5,] -3.128901e+00  7.421464e+00  1.126055e+00 -3.440238e+00  9.028291e+00
#>  [6,]  1.141938e+01 -3.685278e+00  3.419862e+00  3.903307e+00 -4.255066e+00
#>  [7,] -1.722459e+01  1.605262e+00 -1.829337e+00 -6.913126e+00  7.855294e+00
#>  [8,]  1.216672e-01 -1.310821e+01 -4.087707e+00  2.090201e+00 -3.081010e+00
#>  [9,] -3.986844e-01  7.574819e+00  7.169471e+00  1.412911e+00  3.019803e-01
#> [10,]  2.430313e+00  2.737888e+00 -7.139270e+00 -1.013566e+01 -2.954905e+00
#> [11,] -1.647834e-08 -8.502636e-08 -4.322206e-08 -1.896278e-08 -6.006274e-09
#>                [,6]          [,7]          [,8]          [,9]         [,10]
#>  [1,]  1.141938e+01 -1.722459e+01  1.216672e-01 -3.986844e-01  2.430313e+00
#>  [2,] -3.685278e+00  1.605262e+00 -1.310821e+01  7.574819e+00  2.737888e+00
#>  [3,]  3.419862e+00 -1.829337e+00 -4.087707e+00  7.169471e+00 -7.139270e+00
#>  [4,]  3.903307e+00 -6.913126e+00  2.090201e+00  1.412911e+00 -1.013566e+01
#>  [5,] -4.255066e+00  7.855294e+00 -3.081010e+00  3.019803e-01 -2.954905e+00
#>  [6,]  2.681092e+01 -5.325343e+00  9.947580e-01 -6.261774e+00  4.964961e+00
#>  [7,] -5.325343e+00  2.598691e+01 -4.605790e+00 -6.480921e+00 -4.621877e+00
#>  [8,]  9.947580e-01 -4.605790e+00  1.632309e+01  2.168351e+00 -2.365522e-02
#>  [9,] -6.261774e+00 -6.480921e+00  2.168351e+00  2.023479e+01 -1.781042e-01
#> [10,]  4.964961e+00 -4.621877e+00 -2.365522e-02 -1.781042e-01  1.390166e+01
#> [11,] -2.529203e-08  1.013854e-07  1.956264e-07  2.542136e-08  2.226323e-08
#>               [,11]
#>  [1,] -1.647834e-08
#>  [2,] -8.502636e-08
#>  [3,] -4.322206e-08
#>  [4,] -1.896278e-08
#>  [5,] -6.006274e-09
#>  [6,] -2.529203e-08
#>  [7,]  1.013854e-07
#>  [8,]  1.956264e-07
#>  [9,]  2.542136e-08
#> [10,]  2.226323e-08
#> [11,]  3.938853e-05
#>