Propensity Score Matching (PSM) Python-based code
Authors/Creators
Description
OVERVIEW / FINAL SUMMARY
This repository provides 4 variants of a free, Python-based code for performing propensity score (PS) matching. An initiative of the Camargo Cohort Study (Cantabria, Spain), developed with the aim of sharing the tool and spreading the use of PS matching.
The code overcomes compatibility issues with R versions and R packages, and implements (i) logistic regression to compute PS, (ii) 1:N matching using the K-nearest neighbour (KNN) algorithm with a customisable caliper, (iii) sampling with or without replacement, (iv) visualisations to assess matching quality and (v) statistics to evaluate the balance.
Outputs:
- Matched pairs stored as '.csv' file, allowing a Coxreg to be performed ('SET' in SPSS).
- Diagnostic plots stored in the specified output folder, providing a view of SMD and PS distribution.
- Statistics for matching validation: SMD, variance ratio (VR), McFadden's pseudo-R², and L1 multivariate imbalance.
The code has been developed using information from the Matplotlib, Numpy and Seaborn libraries and with OpenAI's ChatGPT support and refinements.
No funding was received for conducting this work and there are no financial or non-financial interests to disclose.
**** METHODOLOGICAL NOTE ****
Carry-Over Effect of the Propensity Score on Non-Included Covariates
In our implementation of Propensity Score Matching (PSM), we observed that balance improvement was not limited to covariates explicitly included in the propensity score (PS) model. Standardized mean differences (SMDs) also decreased for certain variables that were not part of the logistic regression used to estimate the PS.
This phenomenon can be understood as a structural carry-over effect. The PS acts as a low-dimensional summary of the multivariable structure underlying exposure assignment. When upstream variables (e.g., age, BMI, cardiometabolic factors) are included in the model, they often represent central nodes within a correlated biological or clinical network.
If a non-included variable is correlated with included covariates, matching on the PS indirectly reduces imbalance in that variable through shared multivariable structure. This reflects propagation of balance across correlated dimensions of the covariate space rather than direct adjustment.
Importantly, this does not eliminate unmeasured confounding nor replace appropriate model specification. However, it suggests that a well-specified PS may capture broader latent susceptibility patterns beyond the explicitly modeled predictors (see SMD_Lineplot below)
Technical info
|
CODE |
REPLACEMENT |
CUSTOMISABLE RATIO AND CALIPER |
MATCHED PAIRS |
PSM ASSESSMENT |
|
PS matching code 1 |
Without
|
Ratio: line 73 Caliper: line 84 |
.csv file |
SMD (barplot and lineplot) (.png) |
|
PS matching code 2 |
Without
|
Ratio: line 88 Caliper: line 89 |
.csv file |
SMD, VR and pseudo-R² (.csv, .txt) |
|
PS matching code 3 |
Without
|
Ratio: line 163 Caliper: line 168 |
.csv file |
Lineplot with improvements (.png) Balance report (SMD, VR, pseudo-R² and L1 imbalance) (.docx) |
|
|
|
|
|
|
|
PS matching code 4 |
With
|
Ratio: line 89 Caliper: line 100 |
.csv file |
SMD (barplot and lineplot) (.png) |
Notes
Notes
Files
SMD_lineplot.png
Files
(121.9 kB)
Additional details
Additional titles
- Alternative title
- SUMMARY
Dates
- Updated
-
2025-03-03Python-based code for implementing PSM
Software
- Repository URL
- https://github.com/epsar-co/Propensity-Score-Matching-Python-based-code.git
- Programming language
- Python
References
- Staffa SJ, Zurakowski D. Five Steps to Successfully Implement and Evaluate Propensity Score Matching in Clinical Research Studies. Anesth Analg. 2018;127:1066-1073. doi: 10.1213/ANE.0000000000002787.
- Thoemmes, F. Propensity score matching in SPSS. 2012. Available at: https://arxiv.org/pdf/1201.6385.
- Stuart EA. Matching methods for causal inference: A review and a look forward. Stat Sci. 2010;25:1-21. doi: 10.1214/09-STS313.
- Brookhart MA, Schneeweiss S, Rothman KJ, Glynn RJ, Avorn J, Stürmer T. Variable selection for propensity score models. Am J Epidemiol. 2006;163:1149-56. doi: 10.1093/aje/kwj149.
- Austin PC. An Introduction to Propensity Score Methods for Reducing the Effects of Confounding in Observational Studies. Multivariate Behav Res. 2011 May;46(3):399-424. doi: 10.1080/00273171.2011.568786.
- Zhang Z, Kim HJ, Lonjon G, Zhu Y; written on behalf of AME Big-Data Clinical Trial Collaborative Group. Balance diagnostics after propensity score matching. Ann Transl Med. 2019 Jan;7(1):16. doi: 10.21037/atm.2018.12.10.