Skip to content

chisquare_effectsize outputs array instead of scalar when probs1 and probs2 are two dimensional arrays #6184

Open

Description

@AlexGroves

Describe the bug

GofChisquarePower chisquare_effectsize outputs a two element array instead of scalar when probs1 and probs2 are two dimensional arrays. It should output a numpy float. The problem seems to be the .sum(axis) on line 482. axis needs to be removed.

Code Sample, a copy-pastable example if possible

probs0 = np.ones(2, 2)*0.5
probs1 = probs0.copy()

chisquare_effectsize(probs0, probs1, correction=None, cohen=True, axis=0)

Outputs array([0., 0.])

Expected Output

Output should be 0.

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

Details

[paste the output of import statsmodels.api as sm; sm.show_versions() here below this line]
import statsmodels.api as sm; sm.show_versions()

INSTALLED VERSIONS

Python: 3.7.4.final.0
OS: Darwin 18.7.0 Darwin Kernel Version 18.7.0: Tue Aug 20 16:57:14 PDT 2019; root:xnu-4903.271.2~2/RELEASE_X86_64 x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8

Statsmodels

Installed: 0.10.0 (/Users/user/miniconda/envs/analysis/lib/python3.7/site-packages/statsmodels)

Required Dependencies

cython: Not installed
numpy: 1.17.2 (/Users/user/miniconda/envs/analysis/lib/python3.7/site-packages/numpy)
scipy: 1.2.1 (/Users/user/miniconda/envs/analysis/lib/python3.7/site-packages/scipy)
pandas: 0.24.2 (/Users/user/miniconda/envs/analysis/lib/python3.7/site-packages/pandas)
dateutil: 2.8.0 (/Users/user/miniconda/envs/analysis/lib/python3.7/site-packages/dateutil)
patsy: 0.5.1 (/Users/user/miniconda/envs/analysis/lib/python3.7/site-packages/patsy)

Optional Dependencies

matplotlib: 3.1.0 (/Users/user/miniconda/envs/analysis/lib/python3.7/site-packages/matplotlib)
backend: MacOSX
cvxopt: Not installed
joblib: 0.13.2 (/Users/user/miniconda/envs/analysis/lib/python3.7/site-packages/joblib)

Developer Tools

IPython: 7.8.0 (/Users/user/miniconda/envs/analysis/lib/python3.7/site-packages/IPython)
jinja2: 2.10.1 (/Users/user/miniconda/envs/analysis/lib/python3.7/site-packages/jinja2)
sphinx: Not installed
pygments: 2.4.2 (/Users/user/miniconda/envs/analysis/lib/python3.7/site-packages/pygments)
pytest: Not installed
virtualenv: Not installed

Activity

  1. josef-pkt commented on Oct 5, 2019

    @josef-pkt
    Member

    AFAIR, this was intentional to vectorize the test, i.e. apply several similar tests at the same time

    I assume what you want is to use probs0.ravel(), probs1.ravel() to compute it for the flattened data, is it? That's what removing the axis in sum would do. axis=None would be equivalent, but I don't know if we ever verified that case.
    We never flatten data in statsmodels because it's most of the time wrong in statistical applications.

  2. AlexGroves commented on Oct 7, 2019

    @AlexGroves
    Author

    @josef-pkt: I'm not quite sure I understand. Typically the probabilities are in an n x m matrix where either each row or each column sum to one, depending on how you've oriented your matrix. In the documentation for this function the axis variable seems to be the thing that allows you to define the orientation of the data, and this seems to be what it is doing in the code. However, if you input a 2x2 matrix in then the following line will result in a two element vector instead of the expected scalar:

    d2 = ((probs1 - probs0)**2 / probs0).sum(axis)
    

    Removing the axis argument would solve that problem.

    If I'm wrong, and instead it is expected that probs0 and probs1 are vectors then I don't understand what is being calculated, since the probabilities of such a vector should add up to two, not one...

    Or am I way off?

  3. josef-pkt commented on Oct 8, 2019

    @josef-pkt
    Member
  4. josef-pkt commented on Oct 8, 2019

    @josef-pkt
    Member
  5. zhaobinlidev commented on Mar 16, 2020

    @zhaobinlidev

    Technically, as Alex or josef have stated, we could calculate the chisquare_effectsize for 2 x 2 by using .ravel() removing the axis in line 480, chisquare_effectsize in gof.py

    d2 = ((probs1 - probs0)**2 / probs0).sum()

    Since chisquare_effectsize is in the gof (goodness of fit) module, perhaps we should have a separate effect size for chi square contingency test (implemented in statsmodels.stats.proportion.proportions_chisquare) with effect sizes of phi (for 2 x 2) and Cramer's V (for larger than 2 x 2)

    Thanks for your consideration

    Regards,
    ZhaoBin

  6. josef-pkt commented on Mar 16, 2020

    @josef-pkt
    Member

    @zhaobin-li

    Can you write a function and unit tests for the effect size equivalent to the statsmodels.stats.proportion.proportions_chisquare ?
    It could delegate to chisquare_effectsize if that makes sense.

    There are also the contingency table classes where it might be possible to add the relevant effect sizes as methods.

    I don't have an overview in this area right now.
    I'm currently stuck in and confused again about effect size definitions for versions of oneway anova.

    general issue about adding effect size measures
    #5896

  7. zhaobinlidev commented on Mar 17, 2020

    @zhaobinlidev

    Hi Josef,

    Sure, please assign the issue to me. Will be my 1st time contributing to statsmodel (or to open source) and I will go through the contributing guidelines. Coding up shouldn't be an issue

    Regards,
    ZhaoBin

  8. josef-pkt commented on Mar 17, 2020

    @josef-pkt
    Member

    The main setup cost is setting up the dev environment, check out the source and compile.

    If that's currently too much of a detour for you, then you could write the functions and unit test in a script file and I add them to a PR (with you as author).

  9. zhaobinlidev commented on Mar 19, 2020

    @zhaobinlidev

    Thanks Josef, I will give a try and if needed will get your help to add

  10. zhaobinlidev commented on Mar 21, 2020

    @zhaobinlidev

    Hi @josef-pkt,

    I have added the effect size functionality in the contingency table class together with the unit tests and added documentation to the docs/source/contingency_tables.rst.

    I saw that we also have to add the new functionality to the release notes of the upcoming version. The latest release notes is docs/source/release/version0.11.rst, which has already been released. Where do I get the release notes of the upcoming version?

    I am also running make html which generates the documentation in docs/build/html. The documentation is not on the original statsmodel github. Do I commit the whole documentation to the pull request?

    I am quite puzzled about compiling, since python is an interpreted program. I am running the effect size and the unit tests alright by importing the statsmodel module. How do I compile the program?

    Thank you.

    Regards,
    ZhaoBin

  11. josef-pkt commented on Mar 21, 2020

    @josef-pkt
    Member

    @zhaobin-li Thanks

    We don't have release notes for the next release yet. We will have to create one. You could add a sentence or paragraph to the PR comments, that can be copied when we have the release notes.

    Doc build is automatic for dev, and will be updated for stable after the next release. There is nothing to add related to docs for a PR.
    We mainly build the docs locally to check for mistakes. But we also get the results from the doc build on our CI testing.

    compiling:
    I usually use python setup.py build_ext --inplace for the dev environment in my git checkout folder,
    That requires that cython is installed and a C compiler is available.
    C compiler depends on operating system.

  12. added a commit that references this issue on Mar 21, 2020
    58f34eb
  13. zhaobinlidev commented on Mar 21, 2020

    @zhaobinlidev

    Josef thanks for your support, I have done so and submitted a PR

  14. added a commit that references this issue on Mar 21, 2020
    89eed20
  15. zhaobinlidev commented on Mar 21, 2020

    @zhaobinlidev

    Hi @josef-pkt, I realized that I wrote unit tests only on numpy contingency tables. Will write them on pandas dataframe tables and commit again. Thanks

  16. added a commit that references this issue on Mar 23, 2020
    a410860
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions