Skip to content

ENH Contingency tables - #2418

Merged
josef-pkt merged 34 commits into
statsmodels:masterfrom
kshedden:table_homog
Nov 4, 2015
Merged

josef-pkt merged 34 commits into
statsmodels:masterfrom
kshedden:table_homog

Conversation

@kshedden

Copy link
Copy Markdown
Contributor

I added several methods for contingency tables. Some of these are based on code taken from the sandbox while others are all new.

These procedures are often known by the name of their inventor, but I prefer to give names based on what they do. Stata takes a similar approach.

Here are the new procedures:

symmetry : based on code taken from sandbox ('bowker test'), assesses symmetry of the underlying joint distribution for a square table.

ordinal_association : all new code, assesses association between rows and columns that have an ordinal interpretation. It implements Agresti's "linear by linear association test", which is a generalized version of the Cochran-Armitage trend test. Basically a Pearson chi^2 test for ordinal tables.

stratified_association : all new code, implements the "Cochran-Mantel-Haenszel test" and associated estimate and CI's for the common odds ratio in a set of stratified 2x2 tables.

homogeneity : all new code, compares row and column marginal distributions for a square table. Generalizes McNemar's test to two factors with more than two levels.

mcnemar : pulled from sandbox, special case of homogeneity test for 2x2 tables

cochrans_q : pulled from sandbox, generalization of McNemar's test to multiple factors with two levels each.

Design issues:

All functions take a contingency table as the input. There is no facility for passing in the data itself. The table can be an ndarray or Pandas.

Most functions have the option of returning multiple return values or a bunch. In many cases the bunch contains additional information not available otherwise. There are no proper results classes here.

Test coverage is decent but it uses "nice" data sets. There is little testing for corner cases or pathological examples (e.g. all zeros, non-integers, negative values).

Missing pieces:

This is a large area so this is far from complete. There is very little "exact inference" here, and no special facilities for large and/or sparse tables.

References:

R "coin" package : http://cran.r-project.org/web/packages/coin/coin.pdf

Stata : http://www.stata.com/manuals13/rsymmetry.pdf

SAS : http://www2.sas.com/proceedings/forum2008/382-2008.pdf

R Mantel-Haenszel : https://stat.ethz.ch/R-manual/R-devel/library/stats/html/mantelhaen.test.html

Background about common odds ratios : http://www.biostat.umn.edu/~dipankar/bmtry711.11/lecture_15.pdf
http://www.itl.nist.gov/div898/software/dataplot/refman1/auxillar/mantel.htm

@josef-pkt

Copy link
Copy Markdown
Member

It might be best to import true division for this: from __future__ import division to avoid worrying about integer division with counts

@kshedden If you have an overview, then the overview documentation page would be useful, that can also point to the scipy.stats functions fisher exact and independence tests
something like http://statsmodels.sourceforge.net/devel/diagnostic.html#diagnostics

@coveralls

Copy link
Copy Markdown

Coverage Status

Changes Unknown when pulling 7e4c01c on kshedden:table_homog into * on statsmodels:master*.

@coveralls

Copy link
Copy Markdown

Coverage Status

Changes Unknown when pulling d202eb1 on kshedden:table_homog into * on statsmodels:master*.

@coveralls

Copy link
Copy Markdown

Coverage Status

Changes Unknown when pulling 749fcfe on kshedden:table_homog into * on statsmodels:master*.

@coveralls

Copy link
Copy Markdown

Coverage Status

Changes Unknown when pulling 29a292e on kshedden:table_homog into * on statsmodels:master*.

@kshedden

Copy link
Copy Markdown
Contributor Author

This is now partially refactored into classes. A class encapsulates several methods that can be applied to the same type of table. For example, there is a set of procedures that can be applied to a collection of 2x2 tables, so these have now become methods of the StratifiedTables class. The other class I currently have is called TableSymmetry and encapsulates two methods that make sense for square tables with the same categories in the rows and columns.

Each class has individual methods for calculating the results separately, and a summary method that calculates all results and returns a summary table object.

These aren't model classes so there is no "fit" method, but otherwise they behave like model classes.

@josef-pkt

Copy link
Copy Markdown
Member

I briefly browsed the classs, I was worried that it becomes too "heavy" for simple hypothesis tests.

I think it looks good, it's more like Results classes than model classes.
One possibility, since I'm not very familiar with how to do these things in pandas, would be to add a from_data classmethod that constructs the table for the class.

@kshedden

Copy link
Copy Markdown
Contributor Author

Agreed, it is intended to be lightweight. Nothing should be computed until needed. Since there are quite a few methods for contingency tables, I think it will be helpful to group them according to the high-level analysis goal or the type of input table. Some of the methods cache intermediate results that can be re-used by other methods acting on the same data. There are also some "diagnostic" methods to probe, say, where a given test result is coming from. It is easier to do this with a class than with a bunch of disconnected functions.

The downside is if, say, someone is doing a simulation study and just wants 10,000 p-values, there may be a slight overhead for class creation compared to a simple function call. But I have tried to make it so that overhead is negligible.

I added some from_data class methods. The __init__ interface always takes the contingency table, but from_data calculates the table for you from data.

@kshedden kshedden changed the title Contingency tables ENH Contingency tables Jun 2, 2015
@coveralls

Copy link
Copy Markdown

Coverage Status

Changes Unknown when pulling 12da93c on kshedden:table_homog into * on statsmodels:master*.

Comment thread statsmodels/stats/contingency_tables.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should also have the shift_zeros=True option

@josef-pkt

Copy link
Copy Markdown
Member

Looks very good to me, brings the things nicely together (and is a bit similar to the weightstats classes)

3 types of comments

  • naming convention, API: we still don't have a strict convention for what tests should return
  • future proofing: make it easier to add enhancements without breaking backwards compatibility
  • conceptual: To me contingency tables look like just a way of arranging the data. I feel more comfortable with looking at the underlying sampling properties, i.e.
    • 1 sample proportions
    • 2 independent samples: table with one margin fixed
    • k independent samples: e.g. stratified 2x2 tables
    • 2 paired sample: Square table with only total nobs fixed.

related is what the tests are conditioning on. (for #2605 I was looking at unconditional exact tests, that only take those numbers fixed that are fixed by the sampling design)

For the current methods and results it would maybe be enough to specify what the conditioning is on. and for stats usage to specify which table and method applies for which sampling case.

Comment thread statsmodels/stats/contingency_tables.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

under independence

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure anymore we have to specify under independence for each attribute.

@josef-pkt

Copy link
Copy Markdown
Member

I went through it again, It looks very good overall, and about ready to merge

The only critical issue are the residuals, like resid_pearson naming and unit tests (none as far as I can see)

@josef-pkt

Copy link
Copy Markdown
Member

I started to look at an example, first impression

  • I think we need __repr__ and/or __str__ to the Bunch and classes
  • it's confusing that nominal_association does not and ordinal_association wants a call ()
  • The example from the unit tests get_rdataset("Arthritis", "vcd") is not correctly ordered. Either we need to add some ordering support, or have examples that use ordered category levels.

I started to read some of Friendly's notes/vignettes for vcd, vcd-extra, and similar lecture notes by others. Some of this could go in follow-up PRs, and is not needed for this PR.

@josef-pkt

Copy link
Copy Markdown
Member

I'm fine with merging when you are done with this round.

I'll send a message to the mailing list for feedback after merge, and we can review some pieces again later or in the context of #2605 (two sample proportions)

Also, now that I have a bit the connection I'd like to look into the relationship with loglinear/Poisson models, similar for #2605 I wanted to see how much we can replicate and have more general models with GLM Binomial (without any exact part).

josef-pkt added a commit that referenced this pull request Nov 4, 2015
@josef-pkt
josef-pkt merged commit d622ef3 into statsmodels:master Nov 4, 2015
@josef-pkt

Copy link
Copy Markdown
Member

merged, Thanks @kshedden

we will need to start a changelog for 0.8 to add big enhancements, and others

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-12.5%) to 72.414% when pulling a48d3a2 on kshedden:table_homog into fcb31d8 on statsmodels:master.

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.

3 participants