You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
A Class for Brant test for OrderedModel (using Python) #8442
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.
Hello,
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.
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.
My biggest obstacle is to learn how to go through the process of ‘adding’ it as a possible diagnostic test within statsmodels. By that I mean that I have never been through the steps of testing and so on. All what I did is to write the code for the class and test it for a good number of data sets, comparing the results with what one gets using the brant test in R. Any guide is this respect will be appreciated.
From: Josef Perktold ***@***.***>
Date: Friday, October 14, 2022 at 2:48 PM
To: statsmodels/statsmodels ***@***.***>
Cc: Arfi,Badredine ***@***.***>, Author ***@***.***>
Subject: Re: [statsmodels/statsmodels] A Class for Brant test for OrderedModel (using Python) (Issue #8442)
[External Email]
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.
—
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.
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)
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.
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.
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.
@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:
I haven't solved it yet. I wonder if you tested the class before uploading it.
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
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.
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.
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:
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.
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
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.
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.
Activity
josef-pkt commentedon Oct 14, 2022
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
_diagnosticmodule 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.
fbarfi commentedon Oct 14, 2022
josef-pkt commentedon Oct 14, 2022
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.
fbarfi commentedon Oct 14, 2022
josef-pkt commentedon Oct 15, 2022
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 res2location 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
diagnosticmodules.In future the actual module location will be less important because we will have
get_diagnosticin 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_multinomialgiven 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)
josef-pkt commentedon Oct 15, 2022
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.
fbarfi commentedon Oct 15, 2022
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.
nkuehnle commentedon Dec 20, 2022
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.
fbarfi commentedon Dec 20, 2022
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.
fbarfi commentedon Jan 12, 2023
yes here it is attached.
pybrant.zip
fbarfi commentedon Jan 20, 2023
@nkuehnle
Did you get a chance to check the zip folder? thanks.
OskarBienko commentedon Mar 8, 2023
@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_namesIt should've been:
var_names = model.design_info.column_namesAfter I fixed this line, the attribute error occurs:
I haven't solved it yet. I wonder if you tested the class before uploading it.
1 remaining item
OskarBienko commentedon Mar 20, 2023
Hi @fbarfi , any comments?
fbarfi commentedon Mar 20, 2023
Hi, Oskar
Apologies I've been traveling to conferences lately. I'll get to it soon and get back to you on that. Thanks!
OskarBienko commentedon Aug 12, 2023
Hello again, did you maybe have a time to look at that problem?
fbarfi commentedon Aug 12, 2023
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
fbarfi commentedon Dec 15, 2023
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.
frasalvi commentedon Jan 25, 2024
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.fbarfi commentedon Jun 8, 2024
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
danielfonner commentedon Jul 31, 2024
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:
As @fbarfi has in their example, the formula would need to be defined as such:
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.
fbarfi commentedon Jul 31, 2024
thank you OskarBievnko I think you nailed it down. My original idea was to make work with formula API .
fbarfi commentedon Nov 4, 2024
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
fbarfi commentedon Oct 12, 2025
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.
ENH: Add Brant test, standardized beta coefficients, and Dunn post-ho…
Shikhar-Kesharwani commentedon Sep 13, 2026
Implemented in PR #10248. Added$\chi^2$ tests, directly callable via
brant_test()andBrantResultsclass supporting omnibus and variable-by-variable WaldOrderedResults.brant_test(). Benchmark parity verified against R'sbrantpackage on the UCLA dataset.ENH: Add Brant test for proportional odds assumption in OrderedModel (c…
ENH: Add Brant test for proportional odds assumption in OrderedModel (c…