Skip to content

BUG/ENH: discrete margins, handle extra param (e.g. NegativeBinomial) - #3811

Merged
josef-pkt merged 4 commits into
statsmodels:masterfrom
josef-pkt:fix_negbin_margeff
Aug 18, 2017
Merged

josef-pkt merged 4 commits into
statsmodels:masterfrom
josef-pkt:fix_negbin_margeff

Conversation

@josef-pkt

Copy link
Copy Markdown
Member

closes #3809

this is a quickly done fix based on the exceptions.

no unit tests yet, but results for default margeff of poisson, generalized poisson and negative binomial are in the same neighborhood in datasets.randhie example.

>>> np.column_stack((margin_poi.margeff, margin_nb1.margeff, margin_gp1.margeff))
array([[-0.15027281, -0.16679301, -0.20048418],
       [-0.70677348, -0.77079325, -0.91461298],
       [ 0.10094501,  0.11860651,  0.12731912],
       [-0.0989064 , -0.10977451, -0.10183608],
       [ 0.77721772,  0.77404021,  0.59241921],
       [ 0.09708707,  0.10984954,  0.09931454],
       [-0.03614158, -0.12703383, -0.10461374],
       [ 0.15462413,  0.04965824, -0.13230278],
       [ 0.58957703,  0.51223776,  0.32332714]])

and similar for standard errors

@josef-pkt

Copy link
Copy Markdown
Member Author

margin_gp1_el = res1.get_margeff(method='eyex') still fails with shape mismatch (GeneralizedPoisson example)

@coveralls

coveralls commented Jul 7, 2017 •

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.004%) to 90.856% when pulling 9c2bc52 on josef-pkt:fix_negbin_margeff into 359f08b on statsmodels:master.

@josef-pkt

Copy link
Copy Markdown
Member Author

zero-inflated and hurdle models will be even more difficult with two sub-models for zero-modification and main model.

@josef-pkt

Copy link
Copy Markdown
Member Author

margin_gp1_el1 = res1.get_margeff(dummy=True) also breaks with another shape error

I fixed eyex and eydx, but fixing the shape wherever it breaks is very "hacky"

@josef-pkt

Copy link
Copy Markdown
Member Author

one possibility: use exog_idx as an index everywhere to make computation only with selected exog and params, better would be params_index. Using integer index instead of boolean has the advantage that the length doesn't matter, and we don't get shape mismatch if boolean mask and base array don't agree.

params_index might help in the two-part model like zero-inflated models to select between inflation exog and main model exog.

@josef-pkt

Copy link
Copy Markdown
Member Author

general: dummy_idx and count_idx are not attached to the margeff, so it is not visible which exog are treated as dummy or as count variables.

@coveralls

coveralls commented Jul 7, 2017 •

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.003%) to 90.857% when pulling bb9bd2c on josef-pkt:fix_negbin_margeff into 359f08b on statsmodels:master.

@josef-pkt
josef-pkt force-pushed the fix_negbin_margeff branch from bb9bd2c to eb7cb20 Compare July 8, 2017 07:49
@josef-pkt

Copy link
Copy Markdown
Member Author

amended last commit to fix also count=True case.
I also attach dummy_idx and count_idx to margin instance.

Note, randhie dataset does not have a count exog, but 'disea' works after rounding

exog2 = exog.copy()
exog2['disea'] = np.round(exog2['disea'])
res1c = GeneralizedPoisson(endog, exog2, p=1.2).fit(method='newton')

@josef-pkt

Copy link
Copy Markdown
Member Author

as reference: here is my gist to try out various results for genpoisson, and comparison to negbin
https://gist.github.com/josef-pkt/c4f5d0f315c0ce4e6ecc65f0512e8296
needs also the score_obs method from #3807 for cov_type examples

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.003%) to 90.857% when pulling eb7cb20 on josef-pkt:fix_negbin_margeff into 359f08b on statsmodels:master.

1 similar comment
@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.003%) to 90.857% when pulling eb7cb20 on josef-pkt:fix_negbin_margeff into 359f08b on statsmodels:master.

@josef-pkt

Copy link
Copy Markdown
Member Author

I think I want to merge the current version, or something close to it. Refactoring margins is too much for now, and would be better combined with the nonlinear prediction enhancement.
This might need to be less integrated with the model details e.g. with the attached model exog_names/ param_names and similar, so we can use either exog in two-part models.
Another option is to work more with link instances to keep the prediction and derivatives together.

This means that we don't support margins for zero-inflated and hurdle count models during this years GSOC.

@josef-pkt

Copy link
Copy Markdown
Member Author

margin_gp1c_elc.summary_frame() still fails with index error (wrong shape, index out of bounds when iterating over all exog_names)

Note, margeff attribute is ndarray, not wrapped with pandas (similar to t_test ContrastResults)

@coveralls

coveralls commented Jul 8, 2017 •

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.004%) to 90.856% when pulling edc8d15 on josef-pkt:fix_negbin_margeff into 359f08b on statsmodels:master.

@josef-pkt

Copy link
Copy Markdown
Member Author

this has no unit tests yet.

@josef-pkt

josef-pkt commented Aug 4, 2017 •

Copy link
Copy Markdown
Member Author

unit tests and comparison to Stata:

It's the first time that I use margins in Stata, I don't see the Stata commands that were used to create unit test for margins.

using cpunish data with Poisson and NegativeBinomial (I used it for GLM Poisson weights unit tests)
alpha in negbin is essentially zero, 8.814e-08 in statsmodels with method='nm' and 2.82e-08 in Stata with nbreg default. I.e. results are essentially identical to Poisson, and we can write unit tests comparing NegativeBinomial and Poisson.

Margins with continuous and dummy agree at several decimals. I haven't figured out the Stata equivalent of count=True

@josef-pkt

Copy link
Copy Markdown
Member Author

This pull request introduces 1 alert - view on lgtm.com

new alerts:

  • 1 for eq not overridden when adding attributes

Comment posted by lgtm.com

@josef-pkt

Copy link
Copy Markdown
Member Author

fails on all travis, passes on appveyor, passes on my Windows.

The problem is that the dataset is "difficult" and NegativeBinomial does not have hessian/cov_params available (not invertible).

@coveralls

coveralls commented Aug 16, 2017 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.01%) to 81.261% when pulling 0038b48 on josef-pkt:fix_negbin_margeff into 143cc11 on statsmodels:master.

@josef-pkt

Copy link
Copy Markdown
Member Author

amended last commit and force pushed to change the example

  • endog - 1: the data is zero truncated
  • rescale all exog by exog.max(0) (rounded max for easier use in Stata)
  • I didn't change negbin method='nm' which has higher agreement than the default method which also converges

appveyor has the unrelated ARMA failure.

rebase and then merge.

@josef-pkt

Copy link
Copy Markdown
Member Author

This pull request introduces 1 alert - view on lgtm.com

new alerts:

  • 1 for eq not overridden when adding attributes

Comment posted by lgtm.com

@coveralls

coveralls commented Aug 17, 2017 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.01%) to 81.261% when pulling a49b574 on josef-pkt:fix_negbin_margeff into 143cc11 on statsmodels:master.

@josef-pkt

Copy link
Copy Markdown
Member Author

This pull request introduces 1 alert - view on lgtm.com

new alerts:

  • 1 for eq not overridden when adding attributes

Comment posted by lgtm.com

@josef-pkt

Copy link
Copy Markdown
Member Author

this is just one commit behind master, AFAICS. no more rebase needed, merging

@josef-pkt
josef-pkt merged commit 6625458 into statsmodels:master Aug 18, 2017
@josef-pkt josef-pkt mentioned this pull request Apr 9, 2018
@josef-pkt josef-pkt added this to the 0.9 milestone Apr 15, 2018
@josef-pkt
josef-pkt deleted the fix_negbin_margeff branch April 18, 2018 14:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG/ENH: margins, get_margeff for negative binomial, generalized poisson

2 participants