
Fast Beta Regression with Variance Calculation (C++ Backend)
Source:R/helper_glm_fit.R, R/RcppExports.R
fast_beta_regression_with_var_cpp.RdFits the same beta regression model as fast_beta_regression_cpp
(see that page for the full model, parameterization, and optimizer contract) and
additionally computes the variance-covariance matrix and standard errors of the
fitted parameters, via the same working-weights (\(X^\top W X\)) curvature
matrix documented there.
Usage
fast_beta_regression_with_var_cpp(
X,
y,
warm_start_beta = NULL,
smart_cold_start = TRUE,
start_phi = 10,
compute_std_errs = TRUE,
fixed_idx = NULL,
fixed_values = NULL,
optimization_alg = "lbfgs",
warm_start_fisher_info = NULL
)Arguments
- X
A numeric matrix of predictors, \(n \times p\).
- y
A numeric vector of responses, strictly in \((0, 1)\).
- warm_start_beta
Optional starting values for coefficients. 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.
- start_phi
Starting value for the precision parameter \(\phi\) (natural scale).
- compute_std_errs
Deprecated; standard errors are always computed by this entry point regardless of this argument's value.
- fixed_idx
Optional integer indices (into the
c(beta, log(phi))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; see
fast_beta_regression_cpp.
Value
A list containing the following components:
- coefficients
A numeric vector of the obtained Poisson regression coefficients.
- phi
The estimated precision parameter phi.
- vcov
The variance-covariance matrix.
- neg_ll
The negative log-likelihood at the final iteration.
- converged
A logical value indicating whether the algorithm converged.
A list with components coefficients (\(\hat\beta\)),
phi (\(\hat\phi\)), neg_loglik, vcov (the full
(p + 1) x (p + 1) parameter variance-covariance matrix), std_errs
(sqrt(diag(vcov))), converged (logical), and
fisher_information (the working-weights curvature matrix vcov was
inverted from).
Details
Variance computation. The fit's working-information matrix
(fit.XtWX, over all p + 1 parameters c(beta, log(phi))) 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;
std_errs is sqrt(diag(vcov)). Because this uses a plain inverse, a
rank-deficient or near-singular X (after restricting to free parameters)
will produce numerically unstable or NaN standard errors rather than a
graceful fallback — callers should ensure X is full rank on the free
parameters (e.g. via the package's shared drop_linearly_dependent_cols()
preprocessing) before calling this function if that is not already guaranteed.
See also
fast_beta_regression_cpp for the estimate-only variant and
the full model/parameterization documentation;
fast_beta_regression_weighted_cpp for the row-weighted
estimate-only variant.
Examples
X = matrix(rnorm(100), 10, 10)
y = runif(10)
fast_beta_regression_with_var_cpp(X, y)
#> $coefficients
#> [1] 0.1404631 0.4523441 0.4706064 0.8154016 -0.2878395 -0.6651487
#> [7] -0.1563096 0.3101339 2.7315070 0.3901471
#>
#> $phi
#> [1] 1.387335e+12
#>
#> $neg_loglik
#> [1] -138.4185
#>
#> $vcov
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] -2.432272e-12 -1.594200e-12 -2.292889e-12 5.542899e-12 -5.265807e-12
#> [2,] -1.594200e-12 2.651966e-13 -9.546776e-13 3.591789e-12 -2.898714e-12
#> [3,] -2.292889e-12 -9.546776e-13 -8.143168e-13 5.814217e-12 -5.057101e-12
#> [4,] 5.542899e-12 3.591789e-12 5.814217e-12 -7.049834e-12 6.104040e-12
#> [5,] -5.265807e-12 -2.898714e-12 -5.057101e-12 6.104040e-12 -4.171920e-12
#> [6,] 6.937944e-12 3.456799e-12 5.754786e-12 -1.356846e-11 1.217487e-11
#> [7,] -3.730488e-12 -2.005776e-12 -4.595015e-12 4.521445e-12 -3.700393e-12
#> [8,] -8.142827e-12 -3.819782e-12 -6.989001e-12 1.480505e-11 -1.271826e-11
#> [9,] -1.064552e-11 -4.358952e-12 -8.532547e-12 2.009427e-11 -1.727815e-11
#> [10,] -4.170627e-12 -2.126162e-12 -3.867059e-12 6.941165e-12 -5.833520e-12
#> [11,] 3.054574e-06 1.497290e-06 2.813415e-06 -4.673664e-06 3.832003e-06
#> [,6] [,7] [,8] [,9] [,10]
#> [1,] 6.937944e-12 -3.730488e-12 -8.142827e-12 -1.064552e-11 -4.170627e-12
#> [2,] 3.456799e-12 -2.005776e-12 -3.819782e-12 -4.358952e-12 -2.126162e-12
#> [3,] 5.754786e-12 -4.595015e-12 -6.989001e-12 -8.532547e-12 -3.867059e-12
#> [4,] -1.356846e-11 4.521445e-12 1.480505e-11 2.009427e-11 6.941165e-12
#> [5,] 1.217487e-11 -3.700393e-12 -1.271826e-11 -1.727815e-11 -5.833520e-12
#> [6,] -1.655401e-11 9.140006e-12 1.987781e-11 2.512249e-11 1.030907e-11
#> [7,] 9.140006e-12 -7.593776e-13 -9.424455e-12 -1.258573e-11 -4.076583e-12
#> [8,] 1.987781e-11 -9.424455e-12 -2.210596e-11 -2.878315e-11 -1.130793e-11
#> [9,] 2.512249e-11 -1.258573e-11 -2.878315e-11 -3.388207e-11 -1.428772e-11
#> [10,] 1.030907e-11 -4.076583e-12 -1.130793e-11 -1.428772e-11 -5.253806e-12
#> [11,] -7.283302e-06 2.699988e-06 7.869743e-06 1.022158e-05 3.792228e-06
#> [,11]
#> [1,] 3.054574e-06
#> [2,] 1.497290e-06
#> [3,] 2.813415e-06
#> [4,] -4.673664e-06
#> [5,] 3.832003e-06
#> [6,] -7.283302e-06
#> [7,] 2.699988e-06
#> [8,] 7.869743e-06
#> [9,] 1.022158e-05
#> [10,] 3.792228e-06
#> [11,] -1.216187e+00
#>
#> $std_errs
#> [1] NaN 5.149724e-07 NaN NaN NaN
#> [6] NaN NaN NaN NaN NaN
#> [11] NaN
#>
#> $converged
#> [1] FALSE
#>
#> $num_iter
#> [1] 1000
#>
#> $hit_iteration_cap
#> [1] FALSE
#>
#> $gradient_norm
#> [1] NaN
#>
#> $min_eigenvalue_information
#> [1] NaN
#>
#> $fisher_information
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] 4.994758e+12 7.865334e+11 -1.082724e+12 9.700346e+11 2.025222e+12
#> [2,] 7.865334e+11 2.355959e+12 3.918558e+10 -8.846065e+11 2.552485e+10
#> [3,] -1.082724e+12 3.918558e+10 2.317478e+12 -7.896130e+11 -6.097834e+11
#> [4,] 9.700346e+11 -8.846065e+11 -7.896130e+11 2.259217e+12 1.465497e+12
#> [5,] 2.025222e+12 2.552485e+10 -6.097834e+11 1.465497e+12 2.035003e+12
#> [6,] 7.839869e+11 -3.968795e+10 2.498100e+11 -5.054867e+11 -1.717808e+11
#> [7,] -1.763221e+11 -2.109047e+11 9.101545e+11 1.108282e+11 -4.944108e+10
#> [8,] -1.157129e+12 -1.053941e+12 -3.890328e+11 1.910187e+11 -6.162292e+11
#> [9,] 7.076543e+11 -2.458972e+11 -1.883183e+11 -4.384314e+10 3.012147e+11
#> [10,] -2.291513e+12 2.117587e+11 -5.541052e+10 1.070313e+11 -1.253308e+12
#> [11,] -1.098245e+05 -1.032828e+04 5.465138e+04 -6.919683e+04 4.264785e+04
#> [,6] [,7] [,8] [,9] [,10]
#> [1,] 7.839869e+11 -1.763221e+11 -1.157129e+12 7.076543e+11 -2.291513e+12
#> [2,] -3.968795e+10 -2.109047e+11 -1.053941e+12 -2.458972e+11 2.117587e+11
#> [3,] 2.498100e+11 9.101545e+11 -3.890328e+11 -1.883183e+11 -5.541052e+10
#> [4,] -5.054867e+11 1.108282e+11 1.910187e+11 -4.384314e+10 1.070313e+11
#> [5,] -1.717808e+11 -4.944108e+10 -6.162292e+11 3.012147e+11 -1.253308e+12
#> [6,] 1.493552e+12 -2.809201e+10 6.370060e+11 4.916500e+11 -9.982104e+11
#> [7,] -2.809201e+10 1.008676e+12 -1.553938e+11 3.153111e+10 -7.705534e+11
#> [8,] 6.370060e+11 -1.553938e+11 2.195104e+12 -1.432496e+11 -2.953051e+11
#> [9,] 4.916500e+11 3.153111e+10 -1.432496e+11 6.985410e+11 -1.345994e+12
#> [10,] -9.982104e+11 -7.705534e+11 -2.953051e+11 -1.345994e+12 6.091420e+12
#> [11,] 3.419044e+04 8.561741e+04 1.401742e+05 2.926163e+04 5.454706e+04
#> [,11]
#> [1,] -1.098245e+05
#> [2,] -1.032828e+04
#> [3,] 5.465138e+04
#> [4,] -6.919683e+04
#> [5,] 4.264785e+04
#> [6,] 3.419044e+04
#> [7,] 8.561741e+04
#> [8,] 1.401742e+05
#> [9,] 2.926163e+04
#> [10,] 5.454706e+04
#> [11,] 7.243058e-01
#>