Skip to main content

Cochran-Mantel-Haenzsel Chi2 Test

Project description

CMH

Implementation of the Cochran-Mantel-Haenzsel Chi2 Test, based on/ported from "Categorical Data Analysis", page 295 by Agresti (2002) and R implementation of the function mantelhaen.test().

Usage

import pandas as pd
from cmh import CMH

df = pd.DataFrame(
    [
        ['S1', 'no', 'yes'],
        ['S1', 'no', 'yes'],
        ['S1', 'no', 'yes'],
        ['S1', 'no', 'yes'],
        ['S1', 'no', 'yes'],
        ['S1', 'no', 'yes'],
        ['S1', 'yes', 'yes'],
        ['S1', 'yes', 'yes'],
        ['S1', 'yes', 'yes'],
        ['S1', 'yes', 'yes'],
        ['S1', 'yes', 'yes'],
        ['S1', 'yes', 'yes'],

        ['S2', 'yes', 'yes'],
        ['S2', 'yes', 'yes'],
        ['S2', 'yes', 'yes'],
        ['S2', 'yes', 'yes'],
        ['S2', 'yes', 'yes'],
        ['S2', 'no', 'yes'],
        ['S2', 'no', 'yes'],
        ['S2', 'no', 'yes'],
        ['S2', 'no', 'yes'],
        ['S2', 'no', 'no'],
        ['S2', 'no', 'no'],
        ['S2', 'no', 'no'],
        ['S2', 'no', 'no'],

    ],
    columns=['stratum', 'A', 'B']
)

# CMH() will automatically count frequencies of the columns in the dataframe.
result = CMH(df, 'A', 'B', stratifier='stratum')
print(result)

# Will print:
#
#         Cochran-Mantel-Haenszel Chi2 test
#
# "A" x "B", stratified by "stratum"
#
# Cochran-Mantel-Haenszel M^2 = 3.33333, dof = 1, p-value = 0.0679

# Individual components of the result can be accessed via attributes:
print(result.dof)
print(result.p)

# If you're working in a Jupyter Notebook, you can also use `display()` for
# a nicely formatted result.
display(result)

```

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

CMH-1.0.0.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

CMH-1.0.0-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file CMH-1.0.0.tar.gz.

File metadata

  • Download URL: CMH-1.0.0.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.8.10

File hashes

Hashes for CMH-1.0.0.tar.gz
Algorithm Hash digest
SHA256 54031a9389641e7fd623ed86054e03e71aa290fb21219c392b54a7e5b0e8205a
MD5 47a0d0edc88aaf8386c1fa3c5fc114e8
BLAKE2b-256 f4aceb314d624d04ec3aaf8749e4156fe570cf65fe54e2b5c2a0aedd0c701f2b

See more details on using hashes here.

File details

Details for the file CMH-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: CMH-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.8.10

File hashes

Hashes for CMH-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 351fbdb4a097a595dff91030263f07925d079c4abad79807abb0b8f22dc340d2
MD5 cd62bbd9c5440ca843e3a93367a85a92
BLAKE2b-256 61b7464552aa72bdf7e8361455a747d9ad3fe4f06b36ade5199b69120245ede7

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page