Skip to main content

A Python implmentation of Coarsened Exact Matching for causal inference

Project description

cem

pypi build docs

A Python implmentation of Coarsened Exact Matching (CEM). This is more or less based off the original R-package created by Iacus et al. I make no claim to originality and thank the authors for their research.

Original paper: https://gking.harvard.edu/files/abs/cem-plus-abs.shtml

Usage

from cem import CEM

df

+----+------+------+------+-----+-----+
|    |   X1 |   X2 |   X3 |   T |   Y |
+====+======+======+======+=====+=====+
|  0 |    1 |  0.5 |    1 |   1 |   1 |
+----+------+------+------+-----+-----+
|  1 |    2 |  6.2 |    0 |   0 |   1 |
+----+------+------+------+-----+-----+
|  2 |    3 |  2.4 |    1 |   0 |   0 |
+----+------+------+------+-----+-----+
|  3 |    2 |  6.3 |    0 |   1 |   0 |
+----+------+------+------+-----+-----+
|  4 |    3 |  1.9 |    0 |   1 |   1 |
+----+------+------+------+-----+-----+

c = CEM(df, "T", "Y", ["X1", "X2"], measure='l2')

# "bins" can be an int (number of quantiles or equal width bins) or sequence of scalars (quantiles for "qcut" or bin edges for "cut")
schema = {
        "X1": {"bins": [0, .25, .75, 1], "cut": "qcut"},
        "X2": {"bins": 3, "cut": "cut"},
}

# Check the multidimensional (L2) imbalance before and after matching
c.preimbalance # 0.65
c.imbalance(schema) # 0.36

# Get the weights for each example after matching using the coarsening schema
weights = c.match(schema)

+----+-----------+
|    |   weights |
+====+===========+
|  0 |      1    |
+----+-----------+
|  1 |      0.14 |
+----+-----------+
|  2 |      0.26 |
+----+-----------+
|  3 |      1    |
+----+-----------+
|  4 |      1    |
+----+-----------+

# ..perform weighted regression or weighted difference of means to find your treatment effect

Note: Numbers in the example above are just for show.

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

cem-0.1.3.tar.gz (12.2 kB view hashes)

Uploaded Source

Built Distribution

cem-0.1.3-py2.py3-none-any.whl (7.6 kB view hashes)

Uploaded Python 2 Python 3

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