Skip to content

BUG: mnlogit ynames fails with float endog #3960

Closed

Description

@josef-pkt

If endog in mnlogit is a float that is not an integer, then the construction of ynames raises an exception
https://stackoverflow.com/questions/46514008/mnlogit-with-float-dependent-variable
reproducible example below

Note: I'm not sure what the behavior with float should be. There is also the possibility to allow QMLE for simplex/compositional data.

import statsmodels.formula.api as sm
import pandas as pd
df = pd.DataFrame({"A": [0., 1.1, 0, 0, 1.1], "B": [0, 1, 0, 1, 1]})
result = sm.mnlogit(formula="A ~ B", data=df).fit()
>>> result.summary()
    ynames = ['='.join([yname, name]) for name in ynames]
TypeError: sequence item 1: expected str instance, numpy.float64 found
>>>     ynames = ['='.join([yname, name]) for name in ynames]
  File "m:\...\statsmodels\discrete\discrete_model.py", line 3916, in <listcomp>
  File "m:\...\statsmodels\discrete\discrete_model.py", line 3520, in summary
    yname, yname_list = self._get_endog_name(yname, yname_list)
  File "m:\...\statsmodels\discrete\discrete_model.py", line 3916, in _get_endog_name
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>

Activity

  1. josef-pkt commented on Oct 2, 2017

    @josef-pkt
    MemberAuthor

    The specific code

    MultinomialResults has two methods to construct the multivariate specific ynames
    _get_endog_name uses the endog level names to construct yname_list
    _maybe_convert_ynames_int converts ints or integer valued floats to string, but leaves other 'levels' unchanged.

    If the levels are then not strings, then the string join raises an exception, as in the exampl
    TypeError: sequence item 1: expected str instance, numpy.float64 found

    possible solution use str or repr for anything else in the string concatenation, i.e. extend _maybe_convert_ynames_int

    aside _ynames_map seems to be just an alias, not other use found in search in module
    self._ynames_map = ynames

  2. josef-pkt commented on Oct 2, 2017

    @josef-pkt
    MemberAuthor

    QMLE with 2d endog float in simplex:

    (not tried yet)
    I guess that we can handle the special case when the user provided endog is already 2-d, and then assume that in that case the values are floats in a simplex or in {0, 1} multinomial.

    possible problem: patsy also does some conversion, so we might not see the original user provided endog. (Related open issues ...?? where patsy does something different than our code.)
    I haven't checked the details in how this works in current code.

  3. added this to the 0.9 milestone on Oct 7, 2017
  4. josef-pkt commented on Oct 7, 2017

    @josef-pkt
    MemberAuthor

    suggested quickfix for 0.9: add the str conversion for anything else

  5. jbrockmendel commented on Oct 7, 2017

    @jbrockmendel
    Contributor
  6. josef-pkt commented on Oct 7, 2017

    @josef-pkt
    MemberAuthor

    @jbrockmendel Do you know a case where pandas series name is anything else than string or None?
    AFAIR, we didn't not get failing examples for series name, but maybe the problems with some names and name concatenation in ARIMA is similar.

  7. jbrockmendel commented on Oct 7, 2017

    @jbrockmendel
    Contributor

    I'm not aware of any in the existing test cases, but pandas will allow anything hashable as a Series.name. Not a big deal (and definitely shouldn't be allowed to distract from the OP topic).

  8. josef-pkt commented on Oct 7, 2017

    @josef-pkt
    MemberAuthor

    a reminder about not forgetting py 2 - 3 changes
    statsmodels.compat.asstr2 ? (asstr doesn't work for float)

  9. josef-pkt commented on Oct 7, 2017

    @josef-pkt
    MemberAuthor

    more test cases:
    pds = pd.Series(np.arange(5), name=0)

    >>> pds = pd.Series(np.arange(5), name=3.5)
    >>> pds.name
    3.5
    >>> type(pds.name)
    <class 'float'>
    

    ...

  10. self-assigned this
    on May 9, 2019
  11. added 2 commits that reference this issue on Jun 8, 2019
    baefc4d
    d2c9f46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions