Skip to content

ENH: adding pred_table to GLM, closes #1653 - #6252

Open
tleininger wants to merge 1 commit into
statsmodels:mainfrom
tleininger:glm_pred_table
Open

tleininger wants to merge 1 commit into
statsmodels:mainfrom
tleininger:glm_pred_table

Conversation

@tleininger

Copy link
Copy Markdown

This closes #1653, adding pred_table to genmod.generalized_linear_model.GLM. The core functionality is copied from the Discrete case. Here, it only works for Binomial families and otherwise returns None. Unit tests, similar to those employed in the discrete case, have also been added.

Details

Notes:

  • It is essential that you add a test when making code changes. Tests are not
    needed for doc changes.
  • When adding a new function, test values should usually be verified in another package (e.g., R/SAS/Stata).
  • When fixing a bug, you must add a test that would produce the bug in master and
    then show that it is fixed with the new code.
  • New code additions must be well formatted. Changes should pass flake8. If on Linux or OSX, you can
    verify you changes are well formatted by running
    git diff upstream/master -u -- "*.py" | flake8 --diff --isolated
    
    assuming flake8 is installed. This command is also available on Windows
    using the Windows System for Linux once flake8 is installed in the
    local Linux environment. While passing this test is not required, it is good practice and it help
    improve code quality in statsmodels.
  • Docstring additions must render correctly, including escapes and LaTeX.

@bashtage

Copy link
Copy Markdown
Member

I think it should raise NotImplementedError with a message that explains why for models where it isn't possible.

@bashtage

Copy link
Copy Markdown
Member

The other way to go would be to poke it on to the results if the family is Binomial, although this would keep it out of the docs which is a bad idea.

@josef-pkt

Copy link
Copy Markdown
Member

I think, this only handles the binary case in Binomial.
I'm not sure right now how Binomial count models would work for this.

@josef-pkt

Copy link
Copy Markdown
Member

This could be turned into a standalone function that can be reused by all binary models, or similarly for multinomial models.

@codecov

codecov Bot commented Nov 15, 2019 •

Copy link
Copy Markdown

Codecov Report

Merging #6252 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #6252      +/-   ##
==========================================
+ Coverage   84.57%   84.57%   +<.01%     
==========================================
  Files         643      643              
  Lines      101010   101030      +20     
  Branches    11077    11078       +1     
==========================================
+ Hits        85428    85450      +22     
+ Misses      13136    13135       -1     
+ Partials     2446     2445       -1
Impacted Files Coverage Δ
statsmodels/genmod/generalized_linear_model.py 91.42% <100%> (+0.1%) ⬆️
statsmodels/genmod/tests/test_glm.py 99.5% <100%> (ø) ⬆️
statsmodels/sandbox/stats/multicomp.py 47.29% <0%> (+0.36%) ⬆️

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 044456c...d5cd8f1. Read the comment docs.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.003%) to 86.992% when pulling d5cd8f1 on tleininger:glm_pred_table into 044456c on statsmodels:master.

@josef-pkt

Copy link
Copy Markdown
Member

Actually, I think this can be extended to any type of endog by using binning. The implementation already uses histogram which also works with continuous data.
similar to #1537 for count data.
For binary data it has obvious bins, in general we would need to find the bins from the data.

We still need a standalone function to reuse it across models, I think this falls under diagnostics

@tleininger

tleininger commented Nov 15, 2019 •

Copy link
Copy Markdown
Author

I had the idea of a standalone function as well but wanted to keep my first contribution a little more contained. Are you okay with leaving the standalone version for another, more extensive issue?

I like the histogram idea. I quickly tried the histogram approach and found that np.histogram2d bins things by moving anything on the bin threshold to the upper bucket, whereas the current pred_table() function for discrete models would put them in the lower bucket (uses pred < x). Is there a preference or standard here? Or, I could add the NotImplementedError for now and then work on the histogram as a new PR/issue.

On the binomial counts question, that's a good point. What should the desired output be? Currently it will have one entry in the confusion matrix per row of endog (rather than one per trial), which I think is reasonable. You could alternatively base the confusion matrix on trials.

>>> y = np.array([[10, 15]])
>>> X = np.array([[1]])
>>> fit = sm.GLM(y, X, family=sm.families.Binomial()).fit()
>>> fit.pred_table()
array([[ 1.,  0.],
       [ 0.,  0.]])

## alternative output using trials: np.array([[15., 10.], [0., 0.]])

@josef-pkt

Copy link
Copy Markdown
Member

Adding a standalone function is more contained in its impact.
The main advantage is that we don't have to worry about the impact on other model results or other models as in this case.

(e.g. I have most diagnostic functions as standalone functions, both because of reuse by several models, but mainly because I wasn't sure what the API should be for adding them as model methods)

I think the binary pred_table has to raise when ntrials > 1 in binomial.
Binomial is a bit ambiguously implemented because it's both a count model and a model of binary or rates. And my guess is that for pred_table it needs binning or counts and not the binary treatment when ntrials > 1.

I don't remember the details of bin edges. It will become relevant for integer counts, but shouldn't be a problem for Logit.
AFAIR, in some cases I added fudge terms like add 1e-8 to binedges to makes sure that noise and ambiguities don't affect the treatment at binedges.

@tleininger

Copy link
Copy Markdown
Author

Makes sense. I'll move this to diagnostics and see if I can come up with something sensible when ntrials > 1. With a fudge term, I should be able to make this work for any endog.

@bashtage bashtage added this to the 0.11 milestone Nov 25, 2019
@bashtage bashtage modified the milestones: 0.11, 0.12 Jan 24, 2020
@bashtage bashtage modified the milestones: 0.12, 0.13 Aug 11, 2020
@josef-pkt josef-pkt modified the milestones: 0.13, 0.14 Aug 25, 2021
@josef-pkt josef-pkt modified the milestones: 0.14, 0.15 Dec 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add (pred_table) to appropriate GLM results

4 participants