19

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

mdewey
  • 16,541
  • 22
  • 30
  • 57
Hadi
  • 199
  • 1
  • 1
  • 4
  • 1
    The only code in python that I know of is by Fabian see the statsmodels issue https://github.com/statsmodels/statsmodels/issues/807 . I think it wouldn't be difficult to implement for statsmodels, but nobody volunteered yet. – Josef Aug 23 '15 at 14:37
  • 3
    This is not Python, but in R the `orm` function in the `rms` package efficiently handles thousands of levels of the response variable. – Frank Harrell Dec 30 '15 at 12:59
  • 2
    In conjunction w/ @FrankHarrell's comment above, note that you can call R functions from Python w/ [rpy2](http://rpy.sourceforge.net/rpy2/doc-dev/html/introduction.html) (see also: [A Slug's Guide to Python](https://sites.google.com/site/aslugsguidetopython/data-analysis/pandas/calling-r-from-python)). – gung - Reinstate Monica Dec 30 '15 at 16:24
  • 1
    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") – Silverfish Dec 30 '15 at 16:57
  • 1
    Indeed, I ended up using R modules through rpy2, as well as simplifying my model specification to binary logit. – Hadi Dec 31 '15 at 19:32

2 Answers2

7

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

Manu
  • 91
  • 1
  • 4
7

statsmodels now supports Ordinal Regression:

from statsmodels.miscmodels.ordinal_model import OrderedModel

see their documentation here

user3623641
  • 71
  • 1
  • 1
  • 1
    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` – CPBL Jun 09 '21 at 22:14