Firth logistic regression

Key points
  • With small samples or rare outcomes, standard logistic regression can give unstable odds ratio estimates.
  • Firth logistic regression can solve this problem.
  • In \(\textsf{R}\) it can be fit by using glm(..., method = "brglmFit") or via the logistf package.
  • Both methods generally provide similar results. The logistf package also provides profile penalized-likelihood confidence intervals and hypothesis tests.
  • Prespecify Firth regression in the SAP if few events or separation are anticipated (see Code and SAP wording).

Problem

What is separation?

For a binary outcome, separation can occur when one treatment arm has no events and the other has both events and non-events.

  • Complete separation: the predictor perfectly separates the outcomes. All treated subjects have no event and all controls have the event (or vice versa).
  • Quasi-complete separation: the predictor separates the outcomes in one direction only. One cell of the 2x2 table is empty, but the other group has both outcomes.

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).

Complete separation
Outcome
No event Event
Treatment 10 0
Control 0 10
Quasi-complete separation
Outcome
No event Event
Treatment 10 0
Control 6 4
Table 1: Small illustrative 2x2 tables: complete separation (left) and quasi-complete separation (right). Empty cells are highlighted.

Example

Assume we have the following data from a trial.

Show data example code
# 50 subjects: x = treatment (1) vs control (0), y = outcome (1 = event)
dat <- data.frame(
  x = rep(c(0, 0, 1, 1), times = c(24, 4, 22, 0)),
  y = rep(c(0, 1, 0, 1), times = c(24, 4, 22, 0))
)

x <- dat$x
y <- dat$y
Outcome
No event Event Total
Treatment 22 0 22
Control 24 4 28
Total 46 4 50
Table 2: 2x2 contingency table for a binary treatment indicator \(X\) and binary outcome \(Y\) with sample size of 50.

The treatment seems highly effective!

The SAP specifies to conduct a logistic regression and estimate the odds ratio of treatment against control.

m_glm <- glm(y ~ x, family = binomial, data = dat)
summary(m_glm)

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.

Checking for 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.

Theory

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.

Figure 1: Profile log-likelihood for the treatment log odds ratio in the example data. Maximum marked by the dashed line.

Solution

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.

# odds ratios with Wald 95% CI
exp(cbind(OR = coef(m_firth), confint(m_firth))["x", ])
         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
Table 3: Treatment effect from standard logistic regression (MLE) and Firth regression.

Simulation

Setup: MLE vs Firth regression

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
Table 4: Metrics recorded for each simulated dataset.

We vary the main drivers of separation, sample size and prevalence.

  1. Varying sample size — \(n \in \{10, 20, 30, 50, 100, 200, 300, 500\}\) with the intercept fixed to give a marginal prevalence of 5%.
  2. Varying prevalence — \(n = 100\) fixed, with marginal prevalence varying across \(\{1\%, 2\%, 3\%, 5\%, 10\%, 20\%, 30\%, 50\%\}\).

Varying sample size

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.

Figure 2: Separation and non-convergence rates by sample size. Marginal prevalence fixed at 5%. The MLE non-convergence and quasi-complete separation lines overlap completely.
Figure 3: Bias by sample size for the logistic regression (MLE vs Firth with brglm2 and logistf). Marginal prevalence fixed at 5%.
Figure 4: Standard errors by sample size for the logistic regression (MLE vs Firth with brglm2 and logistf). Marginal prevalence fixed at 5%.

Varying prevalence

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.

Figure 5: Separation and non-convergence rates by prevalence. Sample size fixed at 100. The MLE non-convergence and quasi-complete separation lines overlap completely.
Figure 6: Bias by prevalence for the logistic regression (MLE vs Firth with brglm2 and logistf). Sample size fixed at 100.
Figure 7: Standard errors by prevalence for the logistic regression (MLE vs Firth with brglm2 and logistf). Sample size fixed at 100.

Discussion

  • The MLE failed often. At a prevalence of 5%, quasi-complete separation (and thus MLE failure) occurred in 56% of datasets with \(n = 50\) and 28% with \(n = 100\) (Figure 2). With \(n = 100\), the rate of non-convergence fell from 64% at 2% prevalence to about 6% at 10% (Figure 5). Both Firth implementations converged in practically all datasets.
  • Firth reduces bias. Among datasets where the MLE fit converged, bias in the log odds ratio was 0.28 for MLE and 0.08 for Firth at \(n = 100\) and 5% prevalence (Figure 3, Figure 6). Firth’s standard errors were somewhat larger in the smallest samples, where MLE standard errors were available only for converged fits (Figure 4, Figure 7).
  • No penalty in larger samples. Once separation becomes rare (roughly \(n \geq 200\) at 5% prevalence, or prevalence \(\geq 10\%\) at \(n = 100\)), MLE and Firth are essentially identical (Figure 3, Figure 6).
  • Firth estimates agreed across packages brglm2 and logistf gave similar estimates in the simulation scenarios.

Moving forward

Recommendation

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.

Code and SAP wording

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

  • Pre-specify rather than switch. Choosing the method because the standard model failed makes the analysis data-dependent. If rare events or separation are plausible, specify Firth regression as the primary analysis.
  • Predicted probabilities. Firth regression shrinks the intercept towards 0.5, so predicted risks can be biased upwards when events are rare. If absolute risks matter, consider the FLIC or FLAC variants in the logistf package.

Software

  • The 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.
  • Alternative implementations include the logistf package (Heinze, Ploner, and Jiricka 2024), which also provides profile likelihood confidence intervals and tests (the default).
  • For time-to-event analysis, the same problem can occur. The 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).

Session info

sessionInfo()
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         

References

Firth, David. 1993. “Bias Reduction of Maximum Likelihood Estimates.” Biometrika 80 (1): 27–38. https://doi.org/10.1093/biomet/80.1.27.
Heinze, Georg, Meinhard Ploner, and Lena Jiricka. 2024. logistf: Firth’s Bias-Reduced Logistic Regression. https://CRAN.R-project.org/package=logistf.
Heinze, Georg, Meinhard Ploner, Lena Jiricka, and Gregor Steiner. 2023. coxphf: Cox Regression with Firth’s Penalized Likelihood. https://doi.org/10.32614/CRAN.package.coxphf.
Kosmidis, Ioannis, and David Firth. 2009. “Bias Reduction in Exponential Family Nonlinear Models.” Biometrika 96 (4): 793–804. https://doi.org/10.1093/biomet/asp055.
———. 2021. “Jeffreys-Prior Penalty, Finiteness and Shrinkage in Binomial-Response Generalized Linear Models.” Biometrika 108 (1): 71–82. https://doi.org/10.1093/biomet/asaa052.

Footnotes

  1. This is a conditional bias, which favours the MLE because its worst (separated) datasets are excluded.↩︎

  2. For glm (MLE) and brglm2 this is the usual model-based standard error; for logistf it is taken from the penalized likelihood fit.↩︎