Skip to content

ENH: zero-inflated count models - #3908

Merged
josef-pkt merged 55 commits into
statsmodels:masterfrom
josef-pkt:zi_mle
Sep 20, 2017
Merged

josef-pkt merged 55 commits into
statsmodels:masterfrom
josef-pkt:zi_mle

Conversation

@josef-pkt

@josef-pkt josef-pkt commented Sep 6, 2017 •

Copy link
Copy Markdown
Member

this is PR #3755 with some additional changes

Status
(after commit "TST: refactor, extend tests for init_keys and null model")

open:

  • missing skip for now

  • from_formula works for main model, exog_infl not supported, (most likely skip)

  • docstrings: incomplete

  • exposure: likely bug, try fix, or work around using offset internally, (but in the latter case predict might be wrong if exposure or offset are specified by user)

  • predict options: look fine but insufficient unit tests.

  • llnull, null convergence: currently can have llnull=np.nan, separate follow-up issue, refactor for all count or all discrete models

  • remove_data, pickle: most likely exog_infl and similar are not removed. I guess pickling shouldn't be a problem.

  • extras not verified or unit tested: there are several inherited method and options that don't have unit tests and that I did not check. robust cov_types not checked (everything inherited)

the two main things for correctness: fix exposure and unit tests for predict options - DONE
this looks fine now, but more unit tests will be helpful

Note: We can have some additional cross checks and improvements when Truncated and Hurdle PR is available.

@josef-pkt

josef-pkt commented Sep 6, 2017 •

Copy link
Copy Markdown
Member Author

llnull has no unit tests.
I added _get_init_kwds and unit tests still pass, even though llnull should be incorrect, AFAIU
#3903 (comment)

problem with a regression test is that the difference in llnull is very small in the test case, and is most likely smaller than the numerical noise across versions/machines implied by convergence tolerance.

assert_allclose(self.res1.llnull, llnull, rtol=1e-10)
...
Not equal to tolerance rtol=1e-10, atol=0
(mismatch 100.0%)
 x: array(-44199.27456065713)
 y: array(-44199.274632565)

@josef-pkt

Copy link
Copy Markdown
Member Author

This pull request introduces 24 alerts - view on lgtm.com

new alerts:

  • 24 for Module-level cyclic import

Comment posted by lgtm.com

@josef-pkt

Copy link
Copy Markdown
Member Author

amended commit for missing comma

Now with

  • init keys in ZeroInflated models and
  • llnull refactoring to ignore extra exog (_null_drop_keys) in DiscreteResults
  • internal option to attache res_null to results instance, used for unit testing, if results._attach_nullmodel is not False or not available

@coveralls

coveralls commented Sep 6, 2017 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.1%) to 81.46% when pulling 01eb70d on josef-pkt:zi_mle into d0af408 on statsmodels:master.

@codecov-io

codecov-io commented Sep 6, 2017 •

Copy link
Copy Markdown

Codecov Report

Merging #3908 into master will increase coverage by 0.17%.
The diff coverage is 93.25%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #3908      +/-   ##
==========================================
+ Coverage   78.74%   78.91%   +0.17%     
==========================================
  Files         537      541       +4     
  Lines       79703    80648     +945     
  Branches     9120     9181      +61     
==========================================
+ Hits        62763    63646     +883     
- Misses      14883    14923      +40     
- Partials     2057     2079      +22
Impacted Files Coverage Δ
statsmodels/distributions/__init__.py 100% <100%> (ø) ⬆️
statsmodels/discrete/tests/test_discrete.py 98.19% <100%> (+0.01%) ⬆️
statsmodels/distributions/discrete.py 100% <100%> (ø) ⬆️
statsmodels/discrete/tests/test_count_model.py 100% <100%> (ø)
statsmodels/distributions/tests/test_discrete.py 100% <100%> (ø) ⬆️
statsmodels/api.py 90.69% <100%> (+0.22%) ⬆️
statsmodels/discrete/_diagnostics_count.py 70.47% <70.47%> (ø)
statsmodels/discrete/discrete_model.py 86.83% <86.11%> (+0.05%) ⬆️
statsmodels/discrete/count_model.py 92.45% <92.45%> (ø)
statsmodels/discrete/tests/test_diagnostic.py 95.23% <95.23%> (ø)
... and 5 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d0af408...0099d47. Read the comment docs.

@josef-pkt

Copy link
Copy Markdown
Member Author

This pull request introduces 24 alerts - view on lgtm.com

new alerts:

  • 24 for Module-level cyclic import

Comment posted by lgtm.com

@josef-pkt

Copy link
Copy Markdown
Member Author

data handling:

inflate_const uses wrong index if not prepend=False

from_formula works for exog without changes
e.g.

all_ex = ' + '.join(['sex', 'age', 'nmarried', 'nchildren', 'religious',
                     'education' , 'occupation', 'rate_marriage'])
res0f = sm.ZeroInflatedNegativeBinomialP.from_formula('naffairs ~ ' + all_ex,                                                    
        exog_infl=exog, p=2, data=data).fit(method='nm', maxiter=5000)

@josef-pkt

Copy link
Copy Markdown
Member Author

This pull request introduces 24 alerts - view on lgtm.com

new alerts:

  • 24 for Module-level cyclic import

Comment posted by lgtm.com

@josef-pkt

Copy link
Copy Markdown
Member Author

This pull request introduces 24 alerts - view on lgtm.com

new alerts:

  • 24 for Module-level cyclic import

Comment posted by lgtm.com

@josef-pkt

Copy link
Copy Markdown
Member Author

I added comments about status to the top comment, PR description

@josef-pkt

Copy link
Copy Markdown
Member Author

Fixed slicing of offset and exposure for predict. The code needs refactor with a model method to provide Prob[y=0 | model_main]

not changed: if offset or exposure are specified in model but only the exog are specified in predict, then currently a shape mismatch exception is raise, i.e. uses internal offset and exposure, GLM defaults to zeros
'``
# # TODO: this raises with shape mismatch,
# # i.e. uses offset or exposure from model -> fix it or not?
# GLM.predict to setting offset and exposure to zero'


predict prob-zero has loose unit tests, i.e. large problems/bugs in the computation will not cause a failure (e.g. I multiplied the llf in prob-zero computation by 200, and the discrepancy in the test `TestZeroInflatedNegativeBinomialP_predict2` is actually smaller, and no test failed.)

@coveralls

coveralls commented Sep 7, 2017 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.1%) to 81.464% when pulling 1859f52 on josef-pkt:zi_mle into d0af408 on statsmodels:master.

@coveralls

coveralls commented Sep 7, 2017 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.1%) to 81.473% when pulling c262946 on josef-pkt:zi_mle into d0af408 on statsmodels:master.

@coveralls

coveralls commented Sep 7, 2017 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.2%) to 81.482% when pulling 0510a13 on josef-pkt:zi_mle into d0af408 on statsmodels:master.

@josef-pkt

Copy link
Copy Markdown
Member Author

This pull request introduces 24 alerts - view on lgtm.com

new alerts:

  • 24 for Module-level cyclic import

Comment posted by lgtm.com

@josef-pkt

josef-pkt commented Sep 7, 2017 •

Copy link
Copy Markdown
Member Author

predict exog_infl cannot be list, raises exception no shape attribute

res.predict(exog=[[1, 0], [1, 1]], exog_infl=np.asarray([[1], [1]]), which='prob')

exog_infl needs to be specified in predict if exog is specified even if exog_infl=None in model (constant only exog_infl.

@josef-pkt

Copy link
Copy Markdown
Member Author

I think I'm giving up here soon, and merge.

Unit tests are not complete, but the core generic ZI methods have at least one test case each (I think).
Basic functionality works well, and looks very good in predictions in examples.

There is still a lot of work, but that can be left to follow-ups.

this now includes some diagnostics for count models, that I used to check the results in examples. Not public yet, because API is unclear and no "verification". (Checked mainly by whether it "looks good" in examples.)

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.2%) to 81.495% when pulling 0099d47 on josef-pkt:zi_mle into d0af408 on statsmodels:master.

1 similar comment
@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.2%) to 81.495% when pulling 0099d47 on josef-pkt:zi_mle into d0af408 on statsmodels:master.

@josef-pkt

Copy link
Copy Markdown
Member Author

This pull request introduces 24 alerts - view on lgtm.com

new alerts:

  • 24 for Module-level cyclic import

Comment posted by lgtm.com

@josef-pkt

Copy link
Copy Markdown
Member Author

doc build doesn't show count models. I would expect that there are at least warnings, if not errors in the docstrings

the new models are not yet included in sphinx rst.

@coveralls

coveralls commented Sep 20, 2017 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.2%) to 81.496% when pulling 512a85a on josef-pkt:zi_mle into d0af408 on statsmodels:master.

@josef-pkt

Copy link
Copy Markdown
Member Author

After being quite for some time the appveyor hang in statspace simulate is back in one python version (3.6)

Otherwise all green

@josef-pkt

Copy link
Copy Markdown
Member Author

merging, still experimental in some fringes

@evgenyzhurko Thanks for the work in the GSOC. That's the second third of the project.
We still have one third to merge. If you have time, then I would like to allocate two weeks in one to three months to finish up and merge the last group of models. (Rather than having it as an unfinished PR that lingers for years.)

@josef-pkt
josef-pkt merged commit 56aa49e into statsmodels:master Sep 20, 2017
@josef-pkt

Copy link
Copy Markdown
Member Author

This pull request introduces 24 alerts - view on lgtm.com

new alerts:

  • 24 for Module-level cyclic import

Comment posted by lgtm.com

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.

4 participants