24
$\begingroup$

I would like to run an ordinal logistic regression in Python - for a response variable with three levels and with a few explanatory factors. The statsmodels package supports binary logit and multinomial logit (MNLogit) models, but not ordered logit. Since the underlying math is not that different, I wonder if it can be implemented easily using these? (Alternatively, other Python packages that work are appreciated.)

$\endgroup$
5
  • 1
    $\begingroup$ The only code in python that I know of is by Fabian see the statsmodels issue github.com/statsmodels/statsmodels/issues/807 . I think it wouldn't be difficult to implement for statsmodels, but nobody volunteered yet. $\endgroup$
    – Josef
    Aug 23, 2015 at 14:37
  • 3
    $\begingroup$ This is not Python, but in R the orm function in the rms package efficiently handles thousands of levels of the response variable. $\endgroup$ Dec 30, 2015 at 12:59
  • 2
    $\begingroup$ In conjunction w/ @FrankHarrell's comment above, note that you can call R functions from Python w/ rpy2 (see also: A Slug's Guide to Python). $\endgroup$ Dec 30, 2015 at 16:24
  • 1
    $\begingroup$ This is arguably on-topic since the question doesn't seem to be a pure code request - whether one can cobble an ordered logit model out of the computational ingredients of binary logit and MNLogit seems to me to be a question with a statistical character (even if the ultimate solution turns out to be something like "no, use a different package") $\endgroup$
    – Silverfish
    Dec 30, 2015 at 16:57
  • 1
    $\begingroup$ Indeed, I ended up using R modules through rpy2, as well as simplifying my model specification to binary logit. $\endgroup$
    – Hadi
    Dec 31, 2015 at 19:32

3 Answers 3

14
$\begingroup$

statsmodels now supports Ordinal Regression:

from statsmodels.miscmodels.ordinal_model import OrderedModel

see their documentation here

$\endgroup$
1
  • 2
    $\begingroup$ statsmodels now supports Ordinal Regression, but not in the released version. They say that installing the dev version of statsmodels is okay for everyday use. So I did: pip3 install git+git://github.com/someuser/someproject.git $\endgroup$
    – CPBL
    Jun 9, 2021 at 22:14
8
$\begingroup$

Have you tried Mord? It seems there are very few packages to do the same, and it is one of them; though, as Fabian himself suspects, code may not scale properly. Source: Logistic ordinal regression in Python

$\endgroup$
0
$\begingroup$

If you want more flexibility to customize the distributional assumptions, you could do a Bayesian ordered logistic regression using PyMC. You would need to use the pymc.OrderedLogistic base class.

The documentation has the tutorial Regression Models with Ordered Categorical Outcomes which should provide a sructured guide for how to setup the code.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.