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
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()
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.
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.
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.
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.
Activity
josef-pkt commentedon May 24, 2017
To make it more precise.
The following assert should hold, according to this
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.
josef-pkt commentedon May 24, 2017
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-13is my usual starting point if precision depends on floating point operations with not very large rounding errors.)josef-pkt commentedon May 24, 2017
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.
jktis commentedon May 24, 2017
indeed. without the constant, the dummy=True option adjusts the correct parameter and computes it correctly.
josef-pkt commentedon May 24, 2017
a bit more in this direction const_idx is correctly zero, in the example
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.
josef-pkt commentedon May 24, 2017
@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)
josef-pkt commentedon May 24, 2017
When I comment out the if with
dummy_ind[dummy_ind > const_idx] -= 1then all unit test still pass, and a unit test adjusted with prepend=True also passes.This would indicate that the
_get_dummy_indexis only used in the case with all columns, or that there is insufficient test coverage to see where it breaks._get_count_indexhas the same adjustment to the index, and so will most likely have the same bug.TST: add failing unit test for statsmodels#3695
TST: add failing unit test for statsmodels#3695
Merge pull request #3696 from josef-pkt/bug_margins_prepend_3695
josef-pkt commentedon May 25, 2017
@jktis I merged the fix into master