Skip to content

logit marginal effects #3695

Closed
Closed

Description

@jktis

The dummy-option for the compution of the marginal effects from a logisitc regression result does not seem to work properly. When setting the option to True the marginal effect of the continues variable changes, not that of the dummy variable.

from __future__ import division
import numpy as np
from statsmodels.discrete.discrete_model import Logit

# generate data (the last column is a dummy variable)
n_rows = 10e5
x = np.c_[np.ones(n_rows),np.random.uniform(-20,20,size=(n_rows,)), np.random.chisquare(15,size=(n_rows,)), np.random.binomial(1, 0.7, n_rows)]
b_true = np.array([1.2, -0.0143, 0.096, 0.65])

xb = np.sum(x*b_true, axis=1)
p = 1/(1 + np.exp(-xb))
u = np.random.rand(n_rows)
y = np.zeros(n_rows)
y[u<p] = 1

# estimate
logit = Logit(y, x)
res = logit.fit(method='bfgs')
# marginal effects at the mean
me = res.get_margeff(at='mean').summary()
# computing them "by hand"
mean = np.mean(x,axis=0)
p_hat = 1/(1+np.exp(-np.sum(mean*res.params)))
me_own = p_hat*(1-p_hat)*params[1:]
# the marginal effects agree

# Using the dummy option the me of x_3 should change to:
p_1 = 1/(1+ np.exp(-np.sum(np.append(mean[:-1],1)*res.params)))
p_0 = 1/(1+ np.exp(-np.sum(np.append(mean[:-1],0)*res.params)))    
me_dummy = p_1 - p_0

# however, using the dummy-option changes the marginal effect of x_2 (not of x_3)
me_sm_dummy =  res.get_margeff(at='mean',dummy = True).summary()

statsmodels version: 0.6.1
python 2.7

Activity

  1. josef-pkt commented on May 24, 2017

    @josef-pkt
    Member

    To make it more precise.
    The following assert should hold, according to this

    me_sm_dummy =  res.get_margeff(at='mean',dummy = True)
    assert_allclose(me_sm_dummy.margeff[-1], me_dummy, rtol=1e-13)
    

    looks like an indexing bug, or a bug when trying to identify binary columns.
    But I'm not familiar enough with the implementation details to make a reliable guess.

  2. added
    type-bug-wrong
    on May 24, 2017
  3. josef-pkt commented on May 24, 2017

    @josef-pkt
    Member

    and the others should stay unchanged according to the docstring
    assert_allclose(me_sm_dummy.margeff[:-1], me_sm_dummy.margeff[:-1], rtol=1e-13)

    (aside rtol=1e-13 is my usual starting point if precision depends on floating point operations with not very large rounding errors.)

  4. josef-pkt commented on May 24, 2017

    @josef-pkt
    Member

    just a thought @jktis Can you try your example with the constant as last parameter instead of first?

    An obvious candidate for a bug could be mixing up exog indices with and without constant. Stata uses prepend=False, and maybe that's the only cases that are in the unit tests.

  5. jktis commented on May 24, 2017

    @jktis
    Author

    indeed. without the constant, the dummy=True option adjusts the correct parameter and computes it correctly.

  6. josef-pkt commented on May 24, 2017

    @josef-pkt
    Member

    a bit more in this direction const_idx is correctly zero, in the example

    def _get_dummy_index(X, const_idx):
        ...
        # adjust back for a constant because effects doesn't have one
        if const_idx is not None:
            dummy_ind[dummy_ind > const_idx] -= 1
    

    but I didn't figure out yet if the main computation uses original exog, or exog without constant column.

    I see three test cases and all have prepend=False.

  7. josef-pkt commented on May 24, 2017

    @josef-pkt
    Member

    @jktis thanks for reporting and checking.

    This looks like a serious bug, especially given that we switched to prepend=True by default (in contrast to Stata)

  8. josef-pkt commented on May 24, 2017

    @josef-pkt
    Member

    When I comment out the if with dummy_ind[dummy_ind > const_idx] -= 1 then all unit test still pass, and a unit test adjusted with prepend=True also passes.
    This would indicate that the _get_dummy_index is only used in the case with all columns, or that there is insufficient test coverage to see where it breaks.

    _get_count_index has the same adjustment to the index, and so will most likely have the same bug.

  9. added a commit that references this issue on May 25, 2017
    7b6c18a
  10. josef-pkt commented on May 25, 2017

    @josef-pkt
    Member

    @jktis I merged the fix into master

  11. added this to the 0.9 milestone on May 27, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    comp-discretecomp-genmodprio-hightype-bug-wrong

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions