model-diagnostics

Why glm.fit reports ‘algorithm did not converge’ or ‘fitted probabilities numerically 0 or 1’ and how to fix it

The messages glm.fit: algorithm did not converge and glm.fit: fitted probabilities numerically 0 or 1 indicate that the maximum likelihood estimation failed to find a stable sol...

Mara Ellison
Why glm.fit reports ‘algorithm did not converge’ or ‘fitted probabilities numerically 0 or 1’ and how to fix it

What the warnings actually mean in practice

The messages glm.fit: algorithm did not converge and glm.fit: fitted probabilities numerically 0 or 1 indicate that the maximum likelihood estimation failed to find a stable solution. The first warns that iterative reweighted least squares (IRLS) did not reach the requested tolerance within the limit. The second usually signals separation: a linear combination of predictors perfectly or almost perfectly separates the outcome, causing some fitted probabilities to hit 0 or 1 and making the log-likelihood unbounded. These are not syntax errors but numerical and identifiability warnings that require diagnosis rather than blind retries.

How generalized linear models and IRLS work under the hood

Fitting a glm fits a generalized linear model by maximizing the log-likelihood via IRLS. At each iteration, the algorithm forms a working response and a diagonal weight matrix derived from the current linear predictor and the variance function. It then solves a weighted least squares problem to update the coefficients. Convergence is declared when the coefficient change or the deviance change falls below a tolerance. When separation exists or the model is overparameterized, weights collapse, the weighted least squares step becomes unstable, and the iterates can diverge or produce extreme predictions that saturate the Bernoulli or binomial likelihood, yielding the exact 0/1 probabilities that trigger the warning.

The canonical link (e.g., logit for binomial) maps the mean to the linear predictor. For binomial data, the working response is a adjusted dependent variable that approximates the log-odds, and the weights are inversely proportional to the variance at the current mean. When probabilities are near 0 or 1, the variance approaches 0, inflating the weights and amplifying numerical issues. If a predictor perfectly predicts the event, the MLE for the corresponding coefficient tends toward infinity, causing non-convergence and fitted values that are exactly 0 or 1 in-sample.

Common causes of non-convergence and complete separation

  • Quasi-complete or complete separation: one or more predictors perfectly or near-perfectly separate the outcome by a level or threshold.
  • Overparameterization: more predictors than events in binomial models (including many zeros in offsets), especially with sparse data.
  • Data issues: empty cells in contingency tables, duplicate combinations of predictors yielding identical outcomes, or influential outliers.
  • Model complexity: high-order interactions or redundant polynomial terms creating near-collinearity that tips into exact separation.
  • Offset or exposure misuse: an incorrectly specified offset can destabilize the working responses and weights.

How to diagnose the problem rigorously

Inspect warnings and model frame details

Start by confirming the message content and count how many iterations were used. Examine the model frame to see the distribution of the outcome and the ranges of predictors. Create a table of predictor-by-outcome counts for categorical variables, and visualize continuous predictors against the binary outcome to spot near-perfect separation. Compute a baseline unconditional model to see whether the issue persists with a single predictor.

Use stable diagnostics for separation and collinearity

Check for separation by building contingency tables or mosaic plots for categorical predictors. Run variance inflation factors (VIFs) to detect high collinearity, but remember that VIF does not detect separation. Fit a penalized or Firth-style bias-reduced logistic regression to compare coefficient stability. Examine the effective degrees of freedom or the number of non-redundant parameters implied by the data. If a predictor’s coefficient appears to be diverging in sign or magnitude across bootstrap samples, separation is likely present.

Practical fixes and robust modeling strategies

Immediate remedies for non-convergence

  1. Increase maxit in glm (e.g., maxit=100 or 1000) to see whether a higher limit suffices.
  2. Simplify the model by removing redundant or rarely populated interaction terms.
  3. Combine sparse factor levels to reduce the number of parameters.
  4. Center and scale continuous predictors to reduce near-collinearity caused by numeric instability.
  5. Drop highly influential outliers if they correspond to data entry errors; otherwise, treat them cautiously.

Addressing separation explicitly

If separation is the culprit, consider Firth’s bias-reduced logistic regression, which penalizes the likelihood to obtain finite estimates even under separation. In R, this is available via logistf or brglm2. Alternatively, use regularized methods such as ridge logistic regression (e.g., bigstatsr or glmnet with Gaussian family and binomial link) to stabilize coefficient estimation. For causal or inferential settings, exact logistic regression can provide valid inference with small samples and separation.

Modeling best practices to avoid future issues

When designing models, apply regularization by default for high-dimensional or sparse settings. Pre-screen categorical variables for rare levels and collapse where substantively justified. Use cross-validation or holdout data to compare model stability across specifications. Store model frames and contrasts to ensure that new data aligns structurally with the training set. Prefer principled feature engineering—such as target encoding with smoothing or prior weighting—over chasing convergence with fragile raw interactions.

Quick checklist to resolve glm.fit warnings

StepActionGoal
1Check maxit and increase if neededAllow more iterations
2Summarize outcome and predictors; look for perfect predictionSpot separation and data issues
3Compute VIFs and inspect coefficient stabilityDetect collinearity and near-separation
4Simplify model; remove or combine sparse levelsReduce parameters and redundancy
5Apply centering/scaling to continuous termsImprove numerical conditioning
6Try Firth or ridge regularization for separationObtain finite, stable estimates
7Validate on holdout data or via cross-validationEnsure robustness and generalizability

How to reproduce and share the issue safely

To help others diagnose your model, provide the R code used, the exact warning text, the number of iterations attempted, the number of successes and failures, and a summary of each predictor including levels for factors and range for continuous variables. Share a reproducible example using dput() for small data or datasets::mtcars style datasets when possible. Note whether you used an offset, class weights, or strata, and document any data cleaning steps that affect separation. This makes it easier for reviewers to distinguish harmless in-sample separation from data issues that require substantive changes.

When to escalate: simulation, exact tests, and alternative tools

If standard diagnostics remain inconclusive or separation persists after regularization, move to exact logistic tests or Bayesian formulations with weakly informative priors to stabilize inference. In large datasets, even small violations of separation can produce misleadingly significant coefficients; always assess practical significance alongside statistical stability. For production pipelines, wrap model fitting in try-catch blocks, log convergence status, and fall back to regularized or penalized models when warnings occur. Remember that a converged model with substantively sound features is preferable to a fragile model that merely fits the training sample.