Causal Inference using PSM
Background
Propensity score matching is a statistical technique used to estimate the effect of a treatment or intervention on an outcome of interest. It is commonly used in observational studies, where the assignment of treatment or exposure to a particular group is not randomized.
The idea behind propensity score matching is to balance the characteristics of the treatment and control groups by matching individuals with similar propensity scores, which are the probabilities of receiving the treatment or intervention based on observed covariates. This helps to control for confounding factors and reduce selection bias, allowing for a more accurate estimation of the treatment effect.
Overall, propensity score matching is a useful tool for researchers to make causal inferences in observational studies, although it is important to consider the limitations and assumptions of this method.
Installation Guide
This function has been uploaded to pypi so you can type on your prompt as code below
pip install propensio
Then import the library
from propensio.matching import PropensityScoreMatch
If error, download matching.py then
from matching import *
Requirements Library
This python requires related package more importantly python_requires='>=3.1', so that package can be install Make sure the other packages meet the requirements below
- pandas>=1.1.5
- numpy>=1.18.5,<2.0.0
- scipy>=1.2.0
- matplotlib>=3.1.0
- statsmodels>=0.8.0
- scikit-learn>=0.24.0
Usage Guide
This is a Python class named PropensityScoreMatch. It is designed to perform propensity score matching, a technique used to balance the distribution of confounding variables between treatment and control groups in observational studies. The class has four input arguments:
- df: a pandas DataFrame containing the data to be analyzed.
- features: a list of column names in df that contain the variables used to calculate propensity scores.
- treatment: a string that specifies the name of the column in df that contains the treatment variable.
- outcome: a string that specifies the name of the column in df that contains the outcome variable.
The output of the class includes the following attributes:
- df_matched: a DataFrame containing the data for the matched pairs of treated and control observations.
- df_TE: a DataFrame containing the treatment effect estimates for each observation.
- df_smd: a DataFrame containing the standardized mean differences before and after matching.
- ATT: Average Treatment Effect on the Treated.
- ATE: Average Treatment Effect.
- ATC: Average Treatment Effect on the Control.
In addition, the class provides a method for visualizing the results:
- plot_smd(): generates a plot of standardized mean differences (SMDs) before vs. after matching for each feature.
Example Usage
Importing libraries
import pandas as pd
import numpy as np
import propensio
Initiating model
# Load built-in sample data
df = propensio.load_dataset('stroke')
obj_cols = df.select_dtypes(include=['object']).columns
df = pd.get_dummies(df, columns=obj_cols).fillna(0)
df_model = df[['age', 'hypertension', 'heart_disease', 'bmi', 'stroke',
'gender_Male', 'smoking_status_smokes', 'avg_glucose_level']]
features = ['age', 'hypertension', 'heart_disease', 'bmi', 'gender_Male', 'avg_glucose_level']
treatment = 'smoking_status_smokes'
outcome = 'stroke'
psm = propensio.PropensityScoreMatch(df_model, features, treatment, outcome)
The model will automatically print ATT, ATE, and ATC upon fitting:
ATT: 0.0123
ATE: 0.0118
ATC: 0.0112
Accessing results
print(psm.ATT) # Average Treatment Effect on the Treated
print(psm.ATE) # Average Treatment Effect
print(psm.ATC) # Average Treatment Effect on the Control
psm.df_matched # Matched dataframe
psm.df_smd # Standardized Mean Difference table
psm.df_TE # Treatment effect dataframe
Evaluating SMD Plot
psm.plot_smd()
Evaluating Distribution
import matplotlib.pyplot as plt
import seaborn as sns
def hist_all_features(df, features, hue):
width = 6*len(features)
fig, axes = plt.subplots(ncols=len(features), figsize=(width, 5))
for i in range(len(features)):
sns.histplot(data=df, x=features[i], ax=axes[i], hue=hue)
plt.show()
features_plot = ['age','hypertension','heart_disease','bmi','gender_Male','avg_glucose_level','proba']
hist_all_features(psm.df, features_plot, hue='smoking_status_smokes')
Output:
hist_all_features(psm.df_matched, features_plot, hue='smoking_status_smokes')
fig, axes = plt.subplots(ncols=2, figsize=(12, 5))
# Comparing Stroke Mean without Matching
stroke_by_treatment = psm.df.groupby(treatment)[[outcome]].mean()
stroke_by_treatment.plot(kind='bar', ax=axes[0], title='Before Matching')
# Comparing Stroke Mean After Matching
stroke_by_treatment = psm.df_matched.groupby(treatment)[[outcome]].mean()
stroke_by_treatment.plot(kind='bar', ax=axes[1], title='After Matching')
plt.show()
Further Analysis
Rather than direct comparison between matched test variant and control, you better try use Average Treatment Effect for deeper anaylysis. Here, medium article that I recommend ATE Causal Inference
Real Application
This was applied for marketing cases when dealing with above-the-line campaigns. This quite works to handle revenue dilution, more plausible incremental impact. Thanks to Rizli Anshari, Amel Dayani, Bintang who have also contributed to this development
Metadata
Release files for propensio 1.0.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| propensio-1.0.6.tar.gz | 124.3 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| propensio-1.0.6-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 250.4 kB
Release files / propensio-1.0.6.tar.gz
| Download URL | propensio-1.0.6.tar.gz |
|---|---|
| Size | 124.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f1ec5d28fc15d5ec6b8921bbc96891cd78dc3926b1a5b31cae2de2be0bc3f1e7
|
|
BLAKE2b-256 checksum How to use checksums |
261af415e8ff70334058515610778fd7f65da26a544b6b993583a90da8651ea7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.6
|
Release files / propensio-1.0.6-py3-none-any.whl
| Download URL | propensio-1.0.6-py3-none-any.whl |
|---|---|
| Size | 126.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
85b0363b5dc83a1061e34103ac0ce89a1036c0d8547ff789377fd78d0a0d9e3e
|
|
BLAKE2b-256 checksum How to use checksums |
ceef729e1e19983ed173669f4858d566c8bd90f787195735d645cc66ddf22c53
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.6
|
Security for propensio 1.0.6
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.