Skip to content

A Class for Brant test for OrderedModel (using Python) #8442

Open

Description

@fbarfi

Is your feature request related to a problem? Please describe

Absence of brant test for PRA in ordered models

Solution

I have written a class that works for OrderedModels.from_formula by getting inspiration from the brant test in R

I am wondering whether it might be useful as an addendum to OrderedModels in statstmodels.

I lack any experience with submitting patches to existing packages such as statsmodels.

Activity

  1. josef-pkt commented on Oct 14, 2022

    @josef-pkt
    Member

    Yes, it would be a good addition to statsmodels. We had a request for it before, #7892
    I would like to see more diagnostic and specification tests for different models in statsmodels.

    To submit a full pull request, you would need to set up a development environment for statsmodels.
    However, most of it could be written in a special module and would not need a full pull request. Destination would be in a diagnostic or _diagnostic module in statsmodels.discrete.
    The advantages of a pull request is that we have github support for discussion and changes during development.

    Note, we cannot translate code from R (or other packages) that is GPL licensed, because that's incompatible with our license.

  2. fbarfi commented on Oct 14, 2022

    @fbarfi
    Author
  3. josef-pkt commented on Oct 14, 2022

    @josef-pkt
    Member

    As a note it is not a translation from R. It is basically going back to Brant’s original paper published in 1990 while getting some insights from how it was done in R.

    Great, no problem then
    I often look at R (and Stata, ..) docs for information and design issues, and R and Stata example for unit tests.

  4. fbarfi commented on Oct 14, 2022

    @fbarfi
    Author
  5. josef-pkt commented on Oct 15, 2022

    @josef-pkt
    Member

    First, I often help or finish up larger PRs to integrate them and make it consistent with our code.
    The time consuming part is understanding and coding the underlying statistics or econometrics.

    about testing
    unit tests are in the test modules with results from other packages (for me mainly R or Stata)
    for example
    https://github.com/statsmodels/statsmodels/blob/main/statsmodels/miscmodels/tests/test_ordinal_model.py#L171
    contains some test for diagnostics in ordered Logit.
    expected results are in from .results.results_ordinal_model import res_ord_logit as res2

    location for code diagnostic test
    This is still somewhat unclear. I only started some time ago to add diagnostic and specification tests for discrete models.
    The code is currently spread over several diagnostic modules.
    In future the actual module location will be less important because we will have get_diagnostic in results classes.
    I started that with Poisson and count models.
    (Note, modules or functions with leading underline in name are private and might change location. Names without leading underline indicates public, user facing modules or functions.)
    I guess a good location would be a new module discrete _diagnostic_multinomial given that this is a model specific diagnostic test.
    (for example, discrete._diagnostics_count currently has zero-inflation tests for Poisson with public access through discrete.diagnostic and PoissonResults.get_diagnostic)

  6. josef-pkt commented on Oct 15, 2022

    @josef-pkt
    Member

    related question:

    Do you think there is demand from the user side for generalized order logit such as the following for Stata?
    Williams, R. (2006). Generalized Ordered Logit/Partial Proportional Odds Models for Ordinal Dependent Variables. The Stata Journal, 6(1), 58–82. https://doi.org/10.1177/1536867X0600600104

    I haven't looked at many details yet, but most likely it can resuse most code of the current OrderedModel classes
    This would be one alternative if the proportional odds or constant parameter assumption does not hold.

  7. fbarfi commented on Oct 15, 2022

    @fbarfi
    Author

    Yes there is an important demand for generalized ordered logic . The brant test is extremely important to be had in statsmodels because most of the datasets on ordinal response variable violated the parallel regression assumption, which you cannot statistically establish if there is no test for it (since it is a post-estimation test). However, once you discover that the PRA is violated, there isn't much alternative in statstmodels. I know from my teaching years ago using stata that gologit2 was a life saver -- having an equivalent one in statsmodels will definitely be a wonderful addition.

  8. nkuehnle commented on Dec 20, 2022

    @nkuehnle

    Is your feature request related to a problem? Please describe

    Absence of brant test for PRA in ordered models

    Solution

    I have written a class that works for OrderedModels.from_formula by getting inspiration from the brant test in R

    I am wondering whether it might be useful as an addendum to OrderedModels in statstmodels.

    I lack any experience with submitting patches to existing packages such as statsmodels.

    Do you mind sharing this, perhaps we can help you get this implemented. I'm personally looking at doing some ordinal regression in statsmodels right now for inference, but the lack of a way to conveniently test for the proportional odds assumption is a major turn-off and a reason to defer to R instead at the moment.

  9. fbarfi commented on Dec 20, 2022

    @fbarfi
    Author

    Thank you for coming back to me on this. Sure. I have I have it ready. I can attach the python file here if you want.

  10. fbarfi commented on Jan 12, 2023

    @fbarfi
    Author

    yes here it is attached.
    pybrant.zip

  11. fbarfi commented on Jan 20, 2023

    @fbarfi
    Author

    @nkuehnle
    Did you get a chance to check the zip folder? thanks.

  12. OskarBienko commented on Mar 8, 2023

    @OskarBienko

    @fbarfi @nkuehnle I've checked the zip folder and unfortunately the code is not working at all. One error originating from line 78 was easy to solve:
    var_names = model.model.design_info.column_names

    It should've been:
    var_names = model.design_info.column_names

    After I fixed this line, the attribute error occurs:

    image

    I haven't solved it yet. I wonder if you tested the class before uploading it.

  13. 1 remaining item

  14. OskarBienko commented on Mar 20, 2023

    @OskarBienko

    Hi @fbarfi , any comments?

  15. fbarfi commented on Mar 20, 2023

    @fbarfi
    Author

    Hi, Oskar
    Apologies I've been traveling to conferences lately. I'll get to it soon and get back to you on that. Thanks!

  16. OskarBienko commented on Aug 12, 2023

    @OskarBienko

    Hello again, did you maybe have a time to look at that problem?

  17. fbarfi commented on Aug 12, 2023

    @fbarfi
    Author

    I am terribly sorry for my tardiness. I am attaching a zip folder which contains an example where I try pybrant with no problem as you can see for yourself. Let me know if this example works for you too.
    Best.
    Example.zip

  18. fbarfi commented on Dec 15, 2023

    @fbarfi
    Author

    HI there.
    Since we communicated last time, I have given the pybrant code to many of my statistics students to use it in their research papers. So far it is working as it should. Best.

  19. frasalvi commented on Jan 25, 2024

    @frasalvi

    Hello @fbarfi, I have tried your Python implementation and compared it with R's brant: the results turned out to be completely different. I haven't dived into your code, so I'm not sure of what's going there... but all the normal estimated parameters are equal in the two languages.

  20. fbarfi commented on Jun 8, 2024

    @fbarfi
    Author

    Hello @frasalvi , I am attaching an improved version of the PyBrant class. I have also added a unitest class and all tests are ok. Thanks.
    PyBrant.zip

  21. danielfonner commented on Jul 31, 2024

    @danielfonner

    To @OskarBienko 's issue, I believe the model must be fit using the formula API to define the model. When I create a model in this manner, I also get the attribute error:

    mod_log = OrderedModel(y, X, distr='logit')
    res_log = mod_log.fit(method='bfgs', disp=False)
    

    As @fbarfi has in their example, the formula would need to be defined as such:

    flist = []
    for v in X.columns:
            flist.append(v)
    s = ' + '
    
    formula = 'y ~ '+ s.join(flist)
    
    # Run model
    mod_log = OrderedModel.from_formula(formula=formula,
                             data=pd.concat([y,X],axis=1),
                            distr='logit')
    
    res_log = mod_log.fit(method='bfgs',disp=False)
    

    When I switched to using the formula API approach, the error went away for me. That idea came to me after reading this stackOverflow thread.

  22. fbarfi commented on Jul 31, 2024

    @fbarfi
    Author

    thank you OskarBievnko I think you nailed it down. My original idea was to make work with formula API .

  23. fbarfi commented on Nov 4, 2024

    @fbarfi
    Author

    py_brant_with_test.zip
    Hello I have made some improvements on the pybrant class. Now it works with and without formula for ordered models. I just updated the zip folder with a synthetic data creation code, a notebook using it with pybrant.
    thanks. I

  24. fbarfi commented on Oct 12, 2025

    @fbarfi
    Author

    HI, it's been almost a year now I have not heard anything on my pybrant suggestion? Any reason why? The class is working well and I have been using with my students with no problems. Thanks.

  25. added a commit that references this issue on Sep 13, 2026
    5659791
  26. Shikhar-Kesharwani commented on Sep 13, 2026

    @Shikhar-Kesharwani

    Implemented in PR #10248. Added brant_test() and BrantResults class supporting omnibus and variable-by-variable Wald $\chi^2$ tests, directly callable via OrderedResults.brant_test(). Benchmark parity verified against R's brant package on the UCLA dataset.

  27. added a commit that references this issue on Sep 13, 2026
    99cfe5c
  28. added a commit that references this issue on Oct 27, 2026
    52b522d
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

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions