Skip to content

ENH: Firth's penalized logit, GLM #3561

Open

Description

@josef-pkt

question on mailing list
general penalized discussions in #2282, #2293

This is a specific issue for implementing Firth's bias correcting penalization, which is different from the L1, L2, CAD penalization.
We could use L2 type penalization for complete separation. The original motivation for the Firth type penalization was bias correction. Penalization is related to logdet information matrix or hessian.

The bias correction can also be used for other models, mainly GLM/LEF, in a similar way, including multinomial.

todo; add references

implementation for Logit/GLM-Binomial (given my partial overview)

  • adjust endog: similar to continuity correction or removing zeros in multinomial tests, we can adjust endog proportional to diagonal of hat matrix (*)
  • adjust estimating equations and use IRLS or similar
  • use penalized mixin, main problem might be the nonlinearity of the penalization function and the lack of explicit first and second derivatives. (SAS has some formulas, don't find right now, either for glm or for phreg)

(*) this article has a list of pseudo-responses for several standard GLM models (family-link combinations)
Kosmidis, Ioannis, and David Firth. 2009. “Bias Reduction in Exponential Family Nonlinear Models.” Biometrika 96 (4): 793–804. doi:10.1093/biomet/asp055.
(I didn't read it. I don't know if this is just for the IRLS/MLE optimization or if we can use other results like cov_params based on this. predict and resid_xxx is likely wrong if we just change endog.)

also in Kosmidis' thesis section 5.2.3 Refinement of the pseudo-data representation: Obtaining the bias-reduced estimates using already implemented software

update Kosmidis Firth 2009 show that the penalized likelihood representation only works under some assumptions on the link function which is satisfied by canonical links. In general, the bias correction is added to the estimating equation and we might have to solve that explicitly, i.e. IRLS/GEE/GMM style.

SAS
http://support.sas.com/documentation/cdl/en/statug/68162/HTML/default/viewer.htm#statug_logistic_details05.htm#statug.logistic.logisticdfirth the only mention I find for logistic except for examples
http://support.sas.com/documentation/cdl/en/statug/68162/HTML/default/viewer.htm#statug_logistic_examples15.htm
and phreg
http://support.sas.com/documentation/cdl/en/statug/68162/HTML/default/viewer.htm#statug_phreg_details23.htm

Activity

  1. josef-pkt commented on Mar 18, 2017

    @josef-pkt
    MemberAuthor

    Firth type bias correction for related models

    there ares some articles on conditional logit or case-control studies
    Heinze, Georg, and Rainer Puhr. 2010. “Bias-Reduced and Separation-Proof Conditional Logistic Regression with Small or Sparse Data Sets.” Statistics in Medicine 29 (7–8): 770–77. doi:10.1002/sim.3794.

    and some on multinomial
    Bull, Shelley B., Carmen Mak, and Celia M. T. Greenwood. 2002. “A Modified Score Function Estimator for Multinomial Logistic Regression in Small Samples.” Computational Statistics & Data Analysis 39 (1): 57–74. doi:10.1016/S0167-9473(01)00048-2.

    the original Firth article also has a short section on Poisson

    Lipsitz, Stuart R., Garrett M. Fitzmaurice, Scott E. Regenbogen, Debajyoti Sinha, Joseph G. Ibrahim, and Atul A. Gawande. 2013. “Bias Correction for the Proportional Odds Logistic Regression Model with Application to a Study of Surgical Complications.” Journal of the Royal Statistical Society: Series C (Applied Statistics) 62 (2): 233–50. doi:10.1111/j.1467-9876.2012.01057.x.

    Heinze, Georg, and Michael Schemper. 2001. “A Solution to the Problem of Monotone Likelihood in Cox Regression.” Biometrics 57 (1): 114–19. doi:10.1111/j.0006-341X.2001.00114.x.

  2. josef-pkt commented on Mar 18, 2017

    @josef-pkt
    MemberAuthor

    Greenland and co-authors have some articles on alternative priors
    (note, Firth penalization depends on data and is not "prior" information, (similar to empirical Bayesian methods))

    Greenland, Sander, and Mohammad Ali Mansournia. 2015. “Penalization, Bias Reduction, and Default Priors in Logistic and Related Categorical and Survival Regressions.” Statistics in Medicine 34 (23): 3133–43. doi:10.1002/sim.6537.
    Greenland, Sander. 2007. “Prior Data for Non-Normal Priors.” Statistics in Medicine 26 (19): 3578–90. doi:10.1002/sim.2788.

    related: this sounds interesting based on abstract
    Held, Leonhard, and Rafael Sauter. 2016. “Adaptive Prior Weighting in Generalized Regression.” Biometrics, May, n/a-n/a. doi:10.1111/biom.12541.

  3. josef-pkt commented on Mar 18, 2017

    @josef-pkt
    MemberAuthor

    not papers:

    https://www3.nd.edu/~rwilliam/stats3/RareEvents.pdf
    http://www.europeansurveyresearch.org/conf/uploads/494/678/167/PresentationLeitg_b.pdf
    http://prema.mf.uni-lj.si/files/FLICFLAC_final_9e6.pdf
    The authors of the last slides have several presentation slides, but I don't see an article
    results look interesting:

    • firth penalization is good in terms of unbiased parameter estimates but biased towards zero (prob 0.5) in prediction (and larger MSE)
    • F(1,1) penalization of Greenland at al. is good, unbiased for prediction but parameter estimates are biased towards zero
    • Ridge Regression is in between. (compromise between parameter estimates and prediction error)

    the article was just published
    Puhr, Rainer, Georg Heinze, Mariana Nold, Lara Lusa, and Angelika Geroldinger. 2017. “Firth’s Logistic Regression with Rare Events: Accurate Effect Estimates and Predictions?” Statistics in Medicine, January, n/a-n/a. doi:10.1002/sim.7273.

  4. josef-pkt commented on Mar 18, 2017

    @josef-pkt
    MemberAuthor

    reference packages

    besides Firth logit in R and Stata user package, Kosmidis has several articles and a brglm package that seems to cover quite a large range of models: http://www.ucl.ac.uk/~ucakiko/software.html
    refers to his thesis in docstring for details

  5. josef-pkt commented on Mar 18, 2017

    @josef-pkt
    MemberAuthor

    Note: bias reduction with Jeffreys prior as in Firth type penalization looks relatively easy to implement at least for LEF/GLM type models.

    other bias reductions like those based on asymptotic expansion (saddle point, Bartlett ?) looked much more difficult and require more special case derivation and handling, based on what I looked at in the past, e.g. articles by Cordeiro and Cribari-Neto.

    Kosmidis has an overview article:
    Kosmidis, Ioannis. 2014. “Bias in Parametric Estimation: Reduction and Useful Side-Effects.” Wiley Interdisciplinary Reviews: Computational Statistics 6 (3): 185–96. doi:10.1002/wics.1296.

  6. rawls238 commented on Jun 27, 2017

    @rawls238

    is there any interest in having someone pick this up? What is the current state of things? I need to use Firth's penalized logit for a project and as far as I can tell the only way to do this currently in Python is to call out to R. As such, I'd be happy to start looking into it. Let me know if there are any WIP versions of implementing this and if not, some pointers for where this would fit into statsmodels would be much appreciated so that I can start implementing ASAP.

  7. josef-pkt commented on Jun 27, 2017

    @josef-pkt
    MemberAuthor

    I was reading around a lot as you can see from this issue, but didn't write any code or example at all.

    The general penalization setup is a bit in limbo, and requires to finish two stalled PRs. (I'm just before a shortened week on the beach and won't be able to look at this for some time.)
    The extra work there would be mainly to add the function/classes for the penalization, i.e. the version of the prior, including derivatives. In that case the usual scipy optimizers are used for minimizing the penalized log-likelihood. The GAM PR uses this for penalized splines in GLM.

    The second more immediate way to implement this is by data augmentation. Greenland has some articles for various priors/penalization functions. However, IIRC, Kosmidis has in several articles, and maybe thesis, the implementation of Firth's penalization by adding a term to the response variable endog/y. The latter version seems to be the easiest implementation based on the existing models.

    One of the tasks here will be to find test cases and write unit tests, and then figure out how to reproduce the results.
    For example starting with a new Model that delegates most of the computation to an attached GLM (or Logit/Probit) model but adds and keeps track of the adjustments of endog. I think the Firth penalized parameter estimates will be easy to get from the adjusted endog model, but prediction and residual might be on the original arrays, and I don't know or remember how cov_params/bse need to be adjusted.

    Given that I didn't write any prototype or trial code, I am mostly guessing on how well this would work. But based on the reading, going through endog adjustments should give you a working solution relatively fast, with more time needed to get all the extras right.

    After next week I would be available to help out with any model reuse and integration as necessary.
    Note, the R code is almost all GPL, so we cannot use any translation of existing code in statsmodels (unless there is the explicit permission by the author and copyright holder of the original code to translate and distribute under BSD-3)

  8. added this to the 0.10 milestone on Apr 22, 2018
  9. ikosmidis commented on Aug 19, 2019

    @ikosmidis

    I just discovered this thread as I was checking whether there are any Python implementations of Jeffreys-penalized logistic regression.

    I'm the author of the brglm and brglm2 R packages that do bias reduction for binomial GLMs and for all GLMs respectively, though using different algorithms, and I was thiking of doing a Python one, too. However, I may be slow doing this, so I decided to write here, in case someone better in Python than me wants to pick this up (or work with me) towards a Python implementation.

    We have recently co-authored a paper that proves the finiteness of the bias reduced estimator (through penalization of the Jeffreys prior) for any logistic regressions with n < p and full rank X.

    Kosmidis I and Firth D (2019). Jeffreys-prior penalty, finiteness and shrinkage in binomial-response generalized linear models. arXiv:1704.07868

    Algorithm 1 of the paper has an algorithm that can be used to implement maximum Jeffreys-penalized likelihood for any binomial regression model (including logistic regression), through repeated ML fits.

    I reckon, the Python implementation would be simply translating the pseudo-code in our paper to Python. On the ingedients, for the proposed algorithm, what is needed is just:

    • Maximum likelihood procedure for binomial-response GLMs (e.g. using statsmodels)
    • Implementation of the inverse link function G, which I reckon Python implements for popular links, and of g and g', which are the first and second derivatives of G.

    These are easy to get analytically, but in case it helps, I have already done the work in R. Below is an R function and examples that give you the R implementations for G ,g, g', required by Algorithm 1, for various links, using my enrichwith R package

    library("enrichwith")
    get_G <- function(link_name) {
     link <- make.link(link_name)
     enrich(link)[c("linkinv", "mu.eta", "d2mu.deta")]
    }
    get_G("logit")
    get_G("probit")
    get_G("cloglog")
    get_G("cauchit")

    G is linkinv, g is mu.eta and g' is d2mu.deta

  10. josef-pkt commented on Aug 20, 2019

    @josef-pkt
    MemberAuthor

    we have some derivatives for the link functions
    https://www.statsmodels.org/dev/generated/statsmodels.genmod.families.links.Logit.html

    statsmodels GLM is largely generic and takes functions and derivatives from the link and family classes

    Hi Ioannis,

    It's good to see more interest of statisticians to include or provide also Python code.
    I have been taking a break from statsmodels, but hope to get back into contributing to statsmodels in a few weeks.

    Anything that is intended to go into statsmodels needs to be compatible with the BSD license, which GPL is not. We also cannot translate GPL code to Python without permission to release it under BSD.
    Given that you are the author of the R code you can provide that permission and contribute to statsmodels under BSD.
    Note, giving us permission to use and translate code does not require a change in the license of the R code.

    I have not looked at Firth penalization since this issue in 2017. but last year I added GAM and some generic penalized MLE features to statsmodels.

    I just read your paper, it's well written, and I think the algorithm is relatively straightforward to implement. I was already guessing that in 2017 in my previous comment.
    There will be some trickier parts to connect the algorithm to a full model so that we get all the usual results statistic.

    One part that I'm not sure about in your paper is the computation of the covariance. Did you ever look at sandwich covariance matrices for this as in the Huber/White tradition?
    If I read your algorithm correctly, then you are using the usual GLM expected information matrix evaluated at the penalized estimate.

    In general I like to consider penalized MLE as an M-estimator and us the corresponding sandwich form. In GAM I use currently the same default as mgcv which is the "Bayesian" form and not the sandwich type (HC0).

  11. ikosmidis commented on Aug 27, 2019

    @ikosmidis

    I am happy to provide permission to translate code from my brglm and enrichwith R packages (released under GPL) into statsmodels (under BSD) for implementing Jeffreys penalized likelihood in statsmodels, if that is necessary. The only thing I ask is that the right attribution to my work is made, and appropriate references are provided in the manual pages. Translating code, though, may not be necessary, because Algorithm 1 in my paper provides very prescriptive pseudo-code already.

    On the computation of covariance:

    • It is definitely possible to use sandwich covariance matrices, but depending on what statsmodels assumes for the models, it may not be necessary. The implied adjustment to the gradient of the log-likelihood is dominated asymptotically by the gradient. So the approximate distribution of the reduced-bias estimator is the same to the approximate distribution of the MLE, with covariance matrix the inverse of the expected information matrix (covariance matrix of the gradient of the log-likelihood). So the expected information at the reduced-bias estimates can be used.
    • by the above argument, the observed information at the estimates can also be used
    • if you are thinking in terms of estimating equations, then yes. A sandwich estimator is a safe choice, and an empirical version of it can be easily computed:
      The only ingredients to have in place are:
    1. an implementation of the hessian of the penalized likelihood (whose inverse will be the bread), which can be computed using numerical differentiation at convergence
    2. an implementation of the expected information matrix at the estimates (which will be what will be in the sandwich)
  12. Hoeze commented on Dec 14, 2022

    @Hoeze

    Hi, are there any updates on this issue?

  13. PranavAchar01 commented on Nov 11, 2026

    @PranavAchar01
    Contributor

    Hi @josef-pkt, I'd like to work on this. My plan, in small PRs:

    1. FirthLogit as a subclass of Logit that overrides loglike, score and hessian with the Jeffreys-penalized versions, so the usual fit, results and summary keep working. Checked against R logistf, including completely separated data.
    2. Penalized likelihood ratio tests and profile penalized likelihood intervals (Heinze and Schemper 2002).
    3. FLIC and FLAC for predictions (Puhr et al. 2017).
    4. GLM: canonical links first, then the brglm2 adjusted score for other links.

    Would you prefer a subclass, a fit(method="firth") option, or a penalty object in the penalized framework? I'm starting on the subclass version for step 1 and can switch to whatever you prefer.

  14. ikosmidis commented on Nov 11, 2026

    @ikosmidis
  15. PranavAchar01 commented on Nov 11, 2026

    @PranavAchar01
    Contributor

    Thanks @ikosmidis, I hadn't seen firthmodels. It covers scikit-learn style Firth logit and Cox. @josef-pkt, would statsmodels still want a native FirthLogit (statsmodels results, summary, formula API, and later GLM families via the brglm2 adjusted score), or is pointing users to firthmodels enough? I have a working FirthLogit on a branch, matched to logistf and brglm2, if that's useful.

  16. ikosmidis commented on Nov 11, 2026

    @ikosmidis

    I think firtmodels has also a statsmodels adapter, too (and a formula interface). At least this is what I remember last I talked to jzluo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions