Skip to content

Implementation of Haldane correction for odds ratio #7011

Open

Description

@PebetoUofC

Describe the bug

Haldane correction for 2*2 contingency tables adds 0.5 to all cells if any cell is equal to zero. Note that this is what is described in the documentation:

https://www.statsmodels.org/stable/generated/statsmodels.stats.contingency_tables.Table2x2.html#statsmodels.stats.contingency_tables.Table2x2

but this is not what is implemented. Only the cells with 0 are added 0.5.

A possible reference: https://besjournals.onlinelibrary.wiley.com/doi/pdf/10.1111/j.2041-210x.2012.00250.x

Code Sample, a copy-pastable example if possible

import statsmodels.api as sm

# Case reference
c = 10
d = 20
a = 30
b = 0   
x = np.array([[c, d], [a, b]])
table = sm.stats.Table2x2(x)
odds_ratio = table.oddsratio
confint = table.oddsratio_confint()
odds_ratio, confint

# Test 1 - this is what I would have expected
c = 10+0.5
d = 20+0.5
a = 30+0.5
b = 0+0.5   
x = np.array([[c, d], [a, b]])
table = sm.stats.Table2x2(x, shift_zeros=False)
odds_ratio = table.oddsratio
confint = table.oddsratio_confint()
odds_ratio, confint

# Test 2 - this is what is implemented
c = 10
d = 20
a = 30
b = 0+0.5   
x = np.array([[c, d], [a, b]])
table = sm.stats.Table2x2(x, shift_zeros=False)
odds_ratio = table.oddsratio
confint = table.oddsratio_confint()
odds_ratio, confint

Expected Output

Expected Test 1 for output instead of Test 2
Ref gives: (0.008333333333333333, (0.0004603352790378829, 0.15085622937608822))
Test 1 gives:
(0.008396641343462616, (0.00046584641453094707, 0.15134512932064634))
Test 2 gives:
(0.008333333333333333, (0.0004603352790378829, 0.15085622937608822))

Output of import statsmodels.api as sm; sm.show_versions()

Details

INSTALLED VERSIONS

Python: 3.8.3.final.0

statsmodels

Installed: 0.11.1 (c:\users\admin\appdata\local\programs\python\python38\lib\site-packages\statsmodels)

Required Dependencies

cython: Not installed
numpy: 1.18.4 (c:\users\admin\appdata\local\programs\python\python38\lib\site-packages\numpy)
scipy: 1.4.1 (c:\users\admin\appdata\local\programs\python\python38\lib\site-packages\scipy)
pandas: 1.0.4 (c:\users\admin\appdata\local\programs\python\python38\lib\site-packages\pandas)
dateutil: 2.8.1 (c:\users\admin\appdata\local\programs\python\python38\lib\site-packages\dateutil)
patsy: 0.5.1 (c:\users\admin\appdata\local\programs\python\python38\lib\site-packages\patsy)

Optional Dependencies

matplotlib: 3.2.2 (c:\users\admin\appdata\local\programs\python\python38\lib\site-packages\matplotlib)
backend: module://ipykernel.pylab.backend_inline
cvxopt: Not installed
joblib: 0.15.1 (c:\users\admin\appdata\local\programs\python\python38\lib\site-packages\joblib)

Developer Tools

IPython: 7.15.0 (c:\users\admin\appdata\local\programs\python\python38\lib\site-packages\IPython)
jinja2: 2.11.2 (c:\users\admin\appdata\local\programs\python\python38\lib\site-packages\jinja2)
sphinx: Not installed
pygments: 2.6.1 (c:\users\admin\appdata\local\programs\python\python38\lib\site-packages\pygments)
pytest: Not installed
virtualenv: Not installed

Activity

  1. added this to the 0.13 milestone on Aug 31, 2020
  2. josef-pkt commented on Aug 31, 2020

    @josef-pkt
    Member

    related: in meta-analysis https://www.statsmodels.org/dev/generated/statsmodels.stats.meta_analysis.effectsize_2proportions.html
    I have 4 options, None and 3 ways of adjusting counts
    AFAIR, some R packages for meta-analysis have more options.

    #4137 is a generic SUMM issue for empty cells or zeros.

    (I don't remember how much empty cells are handled in stats.proportions. It's not systematically treated and the literature has many different ways of handling them. In two sample odds-ratio test, treatment of zeros is part of method, e.g. ['logit', 'logit-adjusted', 'logit-smoothed'] but that's not intended just for zeros.
    )

  3. josef-pkt commented on Sep 8, 2021

    @josef-pkt
    Member

    I will fix the docstring for 0.13 so the docstring corresponds to the code

    We should add more options, including Haldane (as described in docstring) for the treatment of zeros.
    For 0.14, we still need to decide which options to add.

    I could just add the simple option, but I'm not even sure about that

    The references are not clear about Anscombe-Haldane correction.
    AFAICS, it is unconditional, i.e. always add 0.5, not just in the case if there is at least one zero. (similar to continuity correction which is also unconditional.
    Further references go back to confidence intervals for odds-ratio of two independent proportions.

    e.g. https://www.statsmodels.org/dev/generated/statsmodels.stats.proportion.confint_proportions_2indep.html
    logit-adjusted always adds 0.5 to each cells
    Checking PASS documentation again, they mention that the log odds ratio base statistic converge faster to normality than odds-ratio "wolf" statistics and provide better intervals.
    (AFAICS No plain wolf in the stats.proportion version.

    Contingency tables Table2x2.oddsratio_confint also uses endpoint transformation from log_oddsratio_confint.
    So, the contingency table version and the stats version should agree.

    This would mean that ‘logit-adjusted’ (default) in stats.proportion.confint_proportions_2indep would require to always add 0.5, i.e. unconditional correction.

    Users can easily add 0.5 to their table before creating a contingency table instance. So not high priority.

    I guess going back to cases for zero corrections in meta-analysis packages or references will be useful for general options to adjust tables.

    more general question:
    Do we want to add more options to contingency tables that target specific statistics, e.g. to confidence intervals?
    in analogy to the stats.proportion cases

  4. modified the milestones: 0.13, 0.14 on Feb 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions