There is a newer version of the record available.

Published January 12, 2025 | Version v6

Propensity Score Matching Python-based code

  • 1. EDMO icon University of Cantabria
  • 2. ROR icon Servicio Cántabro de Salud
  • 3. ROR icon Instituto de Investigación Marqués de Valdecilla

Description

This repository offers a free, Python-based code for performing propensity score (PS) matching. As an initiative of the Camargo Cohort Study, it has been developed for clinicians and researchers with the aim of sharing the tool and disseminating 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/without replacement, and (iv) visualisations to assess matching quality.

Outputs: Matched pairs stored as .csv file. Matched pairs can be easily identified, allowing a Coxreg to be performed. In addition, 5 diagnostic plots are saved in the specified output folder.

The code has been developed with support and refinement from OpenAI's ChatGPT.

Notes

The code has been tested and works with datasets in SPSS v25, 28 and 29 ('open script'). 
Python, v3.10 and 3.11.
Regarding R, versions 4.3.0 and 4.4.0, and 'Reticulate' package, 1.39 and 1.40. 
 
Usage
Refine the code with your current research:
- Rename C:\PATH_TO_YOUR_DATASET.sav
- Rename COVS with your data (name, not label)
- Choose the ratio (1:1, 1:2...) and the caliper 
- Choose bar colors and adjust the limits of the x-axis and y-axis to the desired range
- Rename C:\PATH_TO_YOUR_FOLDER
Run the script [RStudio, SPSS (File / Open script)...].
 
A second code with the same functions has been added to perform sampling with replacement.
Both codes provide 5 plots for testing the quality of matching (PS distribution and SMD)
 

Notes

You are welcome to post your comments and suggestions on this site

Files

PS_boxplot.png

Files (290.7 kB)

Name Size Download all
md5:b05166e598e481957dfa4688a2a143eb
35.5 kB Preview Download
md5:f0adb2da820c0f5a51ffcb1caf32e35e
68.6 kB Preview Download
md5:3407b5bd0ae4203645a550092604e91e
45.8 kB Preview Download
md5:d06169ae8d607026c43c5db61787a5a5
11.3 kB Download
md5:a0488ea4c27484f2808e0b93bb0b2b34
10.8 kB Download
md5:73e40f927929f1bd2394a049acd88702
58.0 kB Preview Download
md5:390d9fc407b70120e59648769a739352
60.8 kB Preview Download

Additional details

Dates

Updated
2025-01-11