Skip to contents

Fits the logistic regression model documented in full at fast_logistic_regression_cpp (log-odds-ratio interpretation, IRLS/L-BFGS/Newton-Raphson optimization) via fast_logistic_regression_with_var_cpp, and additionally detects and automatically retries on (quasi-)complete separation — the well-known logistic-regression failure mode where the MLE does not exist because some linear combination of covariates perfectly (or near-perfectly) predicts the outcome, causing the optimizer's coefficient estimates to diverge to a large-but-finite value that would otherwise silently pass ordinary is.finite() convergence checks and corrupt downstream confidence intervals.

Usage

fast_logistic_regression_with_var(
  X,
  y,
  j = 2,
  optimization_alg = "lbfgs",
  warm_start_beta = 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, expected to be binary (0 or 1).

j

The index of the coefficient to compute the variance for. Defaults to 2.

optimization_alg

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

warm_start_beta

Optional starting values for the coefficients.

warm_start_fisher_info

Optional initial Fisher Information matrix.

Value

A list containing the following components:

b

A numeric vector of the obtained logistic regression coefficients \(\hat\beta\), from whichever (possibly covariate-reduced) fit in the retry sequence ultimately converged.

ssq_b_j

The squared standard error (variance) of the j-th estimated coefficient.

ssq_b_2

The squared standard error (variance) of the second estimated coefficient, which typically corresponds to the treatment effect.

Details

Separation detection and retry. After each fit attempt, is_separated_coefficient_magnitude() checks whether any fitted coefficient exceeds a fixed separation-detection threshold (EDI_SEPARATION_THRESHOLD); if so, the fit is treated as converged = FALSE regardless of what the underlying C++ optimizer itself reported. On non-convergence (including detected separation), the covariate (column index \(\ge\) 3, i.e. never the intercept in column 1 or the treatment column in column 2) with the largest absolute fitted coefficient is dropped, and the model is refit on the reduced design; this repeats until either the fit converges or only the intercept and treatment columns remain. If separation persists even with just those two columns, this function stop()s with an explicit "complete separation detected" error rather than returning a corrupted variance estimate.

Interpretation caveat. Because covariates can be silently dropped by this retry loop, the returned b may have fewer coefficients than ncol(X) implies, and the fitted model's covariate adjustment set can differ from what was requested; callers relying on a specific covariate being present in the final fit should check for this rather than assume it always is.

See also

fast_logistic_regression_with_var_cpp for the underlying single-fit (no retry) backend and its variance-computation details; fast_logistic_regression_cpp for the full model documentation.

Examples

X = matrix(rnorm(100), 10, 10)
y = rbinom(10, 1, 0.5)
fast_logistic_regression_with_var(X, y)
#> $b
#>  [1] 18.660097 -8.937242 11.506774 21.429857  2.750403 25.502314 -4.698897
#>  [8] 28.941625 12.564570  4.954459
#> 
#> $ssq_b_j
#> [1] 2.74214e+11
#> 
#> $ssq_b_2
#> [1] 2.74214e+11
#>