| No event | Event | |
|---|---|---|
| Treatment | 10 | 0 |
| Control | 0 | 10 |
| No event | Event | |
|---|---|---|
| Treatment | 10 | 0 |
| Control | 6 | 4 |
glm(..., method = "brglmFit") or via the logistf package.logistf package also provides profile penalized-likelihood confidence intervals and hypothesis tests.For a binary outcome, separation can occur when one treatment arm has no events and the other has both events and non-events.
In both cases the predictor (here, treatment) splits the outcomes so cleanly that the data contain no information about how large the effect could be. Consequently, the likelihood keeps increasing as the log odds ratio moves towards \(\pm\infty\), so there is no finite maximum likelihood estimate (MLE).
Outcome
|
||
|---|---|---|
| No event | Event | |
| Treatment | 10 | 0 |
| Control | 0 | 10 |
Outcome
|
||
|---|---|---|
| No event | Event | |
| Treatment | 10 | 0 |
| Control | 6 | 4 |
Assume we have the following data from a trial.
Outcome
|
|||
|---|---|---|---|
| No event | Event | Total | |
| Treatment | 22 | 0 | 22 |
| Control | 24 | 4 | 28 |
| Total | 46 | 4 | 50 |
The treatment seems highly effective!
The SAP specifies to conduct a logistic regression and estimate the odds ratio of treatment against control.
Call:
glm(formula = y ~ x, family = binomial, data = dat)
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -1.7918 0.5401 -3.318 0.000908 ***
x -18.7743 3780.1276 -0.005 0.996037
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
(Dispersion parameter for binomial family taken to be 1)
Null deviance: 27.877 on 49 degrees of freedom
Residual deviance: 22.967 on 48 degrees of freedom
AIC: 26.967
Number of Fisher Scoring iterations: 19
However, the data have quasi-complete separation. Note the large treatment effect estimate and its standard error. Depending on the model, glm may also warn that fitted probabilities are numerically 0 or 1. These are the typical signs of separation.
With a single binary predictor, an empty cell in the 2x2 table is easy to spot. With several covariates it is not. The detectseparation package can check the model formally.
# install.packages("detectseparation")
library(detectseparation)
glm(y ~ x, family = binomial, data = dat,
method = "detect_separation")Implementation: ROI | Solver: lpsolve
Separation: TRUE
Existence of maximum likelihood estimates
(Intercept) x
0 -Inf
0: finite value, Inf: infinity, -Inf: -infinity
The output confirms that separation was detected (Separation: TRUE). For each coefficient it shows whether the MLE is finite (0) or infinite (Inf / -Inf). Here the intercept is finite, but the treatment coefficient is -Inf. In that case, the log odds ratio would keep decreasing without bound because the treatment arm has no events.
In logistic regression, let \(Y_i \in \{0,1\}\) with \[ \text{logit}(\Pr(Y_i = 1 \mid \mathbf{x}_i)) = \mathbf{x}_i^\top \boldsymbol{\beta}, \]
and let \(\ell(\boldsymbol{\beta})\) denote the log-likelihood. The MLE \(\hat{\boldsymbol{\beta}}\) is consistent but carries a finite-sample bias of order \(O(n^{-1})\): \[ \text{Bias}(\boldsymbol{\beta}) = \mathrm{E}[\hat{\boldsymbol{\beta}} - \boldsymbol{\beta}] = \frac{\mathbf{b}(\boldsymbol{\beta})}{n} + O(n^{-2}). \]
For sparse or imbalanced data this bias inflates coefficient magnitudes away from zero. Under complete or quasi-complete separation the likelihood is monotone, the MLE does not exist, and the estimate diverges to \(\pm\infty\).
Firth (1993) proposed eliminating the \(O(n^{-1})\) bias term by augmenting the log-likelihood with half the log-determinant of the Fisher information matrix \(\mathcal{I}(\boldsymbol{\beta})\): \[ \ell^*(\boldsymbol{\beta}) = \ell(\boldsymbol{\beta}) + \tfrac{1}{2}\log|\mathcal{I}(\boldsymbol{\beta})|. \]
This is equivalent to placing Jeffreys’ invariant prior \(p(\boldsymbol{\beta}) \propto |\mathcal{I}(\boldsymbol{\beta})|^{1/2}\) on the coefficients. Maximising \(\ell^*\) gives the modified score equation \[ \mathbf{U}^*(\boldsymbol{\beta}) = \mathbf{U}(\boldsymbol{\beta}) + \mathcal{I}(\boldsymbol{\beta})\,\mathbf{b}(\boldsymbol{\beta}) = \mathbf{0}, \] which by construction subtracts the first-order bias, leaving residual bias of \(O(n^{-2})\).
The penalty serves a second role under separation: as \(\|\boldsymbol{\beta}\| \to \infty\) the term \(\tfrac{1}{2}\log|\mathcal{I}(\boldsymbol{\beta})|\) decreases fast enough to make \(\ell^*\) concave with a finite maximum, so penalized estimates always exist and are finite.
The figure below illustrates this for the example data. For each value of the treatment log odds ratio, the intercept is set to its best value and the (penalized) log-likelihood is plotted relative to its maximum. The standard log-likelihood keeps rising as the log odds ratio decreases and never reaches a peak, so the MLE does not exist. The Firth penalty turns it into a curve with a clear maximum.
Going back to the example, we see that this fixes the issue.
# install.packages("brglm2") # if not already installed
library(brglm2)
m_firth <- glm(y ~ x, family = binomial, data = dat, method = "brglmFit")
summary(m_firth)
Call:
glm(formula = y ~ x, family = binomial, data = dat, method = "brglmFit")
Deviance Residuals:
Min 1Q Median 3Q Max
-0.5807 -0.5807 -0.2097 -0.2097 1.9304
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -1.695 0.522 -3.247 0.00117 **
x -2.112 1.552 -1.361 0.17365
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
(Dispersion parameter for binomial family taken to be 1)
Null deviance: 27.920 on 49 degrees of freedom
Residual deviance: 23.967 on 48 degrees of freedom
AIC: 27.967
Type of estimator: AS_mixed (mixed bias-reducing adjusted score equations)
Number of Fisher Scoring iterations: 5
The coefficients are on the log-odds scale. To report an odds ratio, exponentiate the estimate and its confidence interval. Note that confint() on a brglmFit object gives Wald intervals, which can be unreliable with so few events.
OR 2.5 % 97.5 %
0.120987661 0.005772634 2.535760032
For profile-likelihood confidence intervals, the logistf package can be used instead. It fits Firth regression and by default (pl = TRUE) computes profile penalized likelihood intervals.
# install.packages("logistf") # if not already installed
library(logistf)
m_logistf <- logistf(y ~ x, data = dat)
# odds ratio with profile-likelihood 95% CI
exp(cbind(OR = coef(m_logistf), confint(m_logistf)))["x", ] OR Lower 95% Upper 95%
0.120987654 0.000893939 1.234081715
The estimated odds ratio for treatment is finite and the interval is wide. It still includes 1, which reflects how little information 4 events in 50 subjects carry. The following table summarizes these methods and their estimates.
| Method | Package | log OR | SE | OR | 95% CI | CI type |
|---|---|---|---|---|---|---|
| MLE | base (glm) |
-18.8 | 3.78e+03 | 7.02e-09 | ( 0, Inf) | Wald |
| Firth | brglm2 |
-2.11 | 1.55 | 0.121 | (0.00577, 2.54) | Wald |
| Firth | logistf |
-2.11 | 1.52 | 0.121 | (0.000894, 1.23) | Profile |
We compared standard logistic regression (MLE) with Firth regression (from \(\textsf{R}\) packages brglm2, logistf) in small-sample settings. The aim was to see (i) how often the MLE breaks down because of separation and (ii) whether Firth’s estimates are less biased and what they cost in precision.
For each scenario, \(N_\text{sim} = 2{,}000\) datasets are generated under the logistic regression model
\[ \text{logit}\, P(Y_i = 1 \mid X_i) = \beta_0 + \beta X_i, \qquad X_i \sim \text{Bernoulli}(0.5), \]
with true log-odds ratio \(\beta = \log(1/3)\) (OR = 1/3, moderate protective effect). The intercept \(\beta_0\) is chosen to match a target marginal prevalence \(P(Y = 1)\). For each dataset, three estimators are fitted: the standard MLE (glm), Firth regression via brglm2 and Firth regression via logistf.
For each simulated dataset, we recorded whether quasi-complete separation or near separation occurred. We then evaluated the performance of the three estimators by recording (i) whether the estimation procedure converged, (ii) the bias of the estimates, and (iii) their standard errors (SEs). See Table 4.
| Metric | Definition |
|---|---|
| Quasi-complete separation | Exactly one level of \(X\) has a constant outcome (all 0 or all 1). |
| Near separation | At least one cell of the \(2 \times 2\) table contains fewer than 5 subjects. |
| MLE non-convergence | The MLE coefficient or its standard error exceeds 10 in absolute value or the model fails entirely. |
Firth (brglm2) non-convergence |
Same criterion applied to the brglm2 Firth estimator. |
Firth (logistf) non-convergence |
Same criterion applied to the logistf Firth estimator. |
| Bias | Estimated \(\hat\beta\) minus the true \(\beta\), computed only when the respective model converges.1 |
| SE | Standard error of \(\hat\beta\), computed only when the respective model converges.2 |
We vary the main drivers of separation, sample size and prevalence.
Figure 2 shows how often separation and MLE non-convergence occur, and Figure 3 and Figure 4 compare bias and standard errors of the three estimators.
brglm2 and logistf). Marginal prevalence fixed at 5%.
brglm2 and logistf). Marginal prevalence fixed at 5%.
Figure 5 shows how often separation and MLE non-convergence occur, and Figure 6 and Figure 7 compare bias and standard errors of the three estimators.
brglm2 and logistf). Sample size fixed at 100.
brglm2 and logistf). Sample size fixed at 100.
brglm2 and logistf gave similar estimates in the simulation scenarios.Prespecify Firth logistic regression in the SAP when separation is plausible, for example for small sample sizes or a rare outcome. Additionally, in our simulations it performed no worse than the MLE when events were not rare.
Template code (replace outcome, treatment and covariates with your variables).
library(logistf)
# Fit Firth regression
fit <- logistf(outcome ~ treatment + covariate1 + covariate2, data = dat)
# Odds ratios with profile-likelihood 95% CI
exp(cbind(OR = coef(fit), confint(fit)))
# Alternative: brglm2 (note: confint() gives Wald intervals)
# library(brglm2)
# fit <- glm(outcome ~ ., family = binomial, data = dat, method = "brglmFit")Suggested SAP wording (adapt as needed):
The treatment effect will be estimated as an odds ratio using logistic regression with Firth’s bias-reduction (R package
logistf), because few events are expected with 95% CIs based on the profile penalized likelihood.
Points to consider
logistf package.brglm2 package (Kosmidis and Firth 2021) implements bias-reduction for the full family of generalized linear models, not only logistic regression. The method is invoked by passing method = "brglmFit" directly inside the standard glm() call, requiring no change to the model formula or the rest of the analysis workflow.logistf package (Heinze, Ploner, and Jiricka 2024), which also provides profile likelihood confidence intervals and tests (the default).coxphf package (Heinze et al. 2023) fits Cox regression with Firth’s penalization of the partial likelihood.Variations of the penalty exist. Specifically, there is also an extended Firth approach to a general mean-bias-reduction framework (Kosmidis and Firth 2009).
R version 4.5.2 (2025-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 11 x64 (build 22631)
Matrix products: default
LAPACK version 3.12.1
locale:
[1] LC_COLLATE=English_Australia.utf8 LC_CTYPE=English_Australia.utf8
[3] LC_MONETARY=English_Australia.utf8 LC_NUMERIC=C
[5] LC_TIME=English_Australia.utf8
time zone: Europe/Zurich
tzcode source: internal
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] detectseparation_0.4.0 kableExtra_1.4.0 knitr_1.51
[4] logistf_1.26.1 brglm2_1.0.1 data.table_1.18.2.1
[7] ggplot2_4.0.1
loaded via a namespace (and not attached):
[1] tidyselect_1.2.1 viridisLite_0.4.3 dplyr_1.2.1
[4] farver_2.1.2 S7_0.2.1 fastmap_1.2.0
[7] digest_0.6.39 rpart_4.1.24 lifecycle_1.0.5
[10] survival_3.8-3 statmod_1.5.1 magrittr_2.0.4
[13] compiler_4.5.2 rlang_1.1.7 tools_4.5.2
[16] yaml_2.3.12 labeling_0.4.3 htmlwidgets_1.6.4
[19] xml2_1.5.2 RColorBrewer_1.1-3 enrichwith_0.4.0
[22] registry_0.5-1 nleqslv_3.3.7 withr_3.0.2
[25] purrr_1.2.1 numDeriv_2016.8-1.1 nnet_7.3-20
[28] grid_4.5.2 jomo_2.7-6 mice_3.19.0
[31] scales_1.4.0 iterators_1.0.14 MASS_7.3-65
[34] cli_3.6.5 rmarkdown_2.30 reformulas_0.4.3.1
[37] generics_0.1.4 otel_0.2.0 rstudioapi_0.18.0
[40] minqa_1.2.8 stringr_1.6.0 operator.tools_1.6.3.1
[43] splines_4.5.2 vctrs_0.7.1 boot_1.3-32
[46] glmnet_4.1-10 Matrix_1.7-4 jsonlite_2.0.0
[49] slam_0.1-56 mitml_0.4-5 ROI.plugin.lpsolve_1.0-2
[52] systemfonts_1.3.1 foreach_1.5.2 tidyr_1.3.2
[55] glue_1.8.0 ROI_1.0-2 nloptr_2.2.1
[58] pan_2.0 codetools_0.2-20 stringi_1.8.7
[61] shape_1.4.6.1 gtable_0.3.6 lme4_1.1-38
[64] tibble_3.3.1 pillar_1.11.1 htmltools_0.5.9
[67] R6_2.6.1 textshaping_1.0.4 Rdpack_2.6.5
[70] formula.tools_1.7.1 lpSolveAPI_5.5.2.0-17.15 evaluate_1.0.5
[73] lattice_0.22-7 rbibutils_2.4.1 backports_1.5.0
[76] broom_1.0.12 Rcpp_1.1.1 svglite_2.2.2
[79] nlme_3.1-168 checkmate_2.3.4 mgcv_1.9-3
[82] xfun_0.56 pkgconfig_2.0.3
logistf: Firth’s Bias-Reduced Logistic Regression. https://CRAN.R-project.org/package=logistf.
coxphf: Cox Regression with Firth’s Penalized Likelihood. https://doi.org/10.32614/CRAN.package.coxphf.