Matching techniques for observational studies. Inspired by and adapted from Jasjeet Singh Sekhon's Matching package in R. I wrote an adaptation in Python that is better suited for my work at Mozilla which incorporates:
This package was used to support this research project.
Install through pip!
$ pip install pymatch
The best way to get familiar with the package is to work through an example. The example below leaves out much of the theory behind matching and focuses on the application within
pymatch. If interested, Sekhon gives a nice overview in his Introduction to the Matching package in R.
The following example demonstrates how to the use the
pymatch package to match Lending Club Loan Data. Follow the link to download the dataset from Kaggle (you'll have to create an account, it's fast and free!). You can follow along this document or download the corresponding Example.ipynb notebook (just be sure to change the path when loading data!).
Here we match Lending Club users that fully paid off loans (control) to those that defaulted (test). The example is contrived, however a use case for this could be that we want to analyze user sentiment with the platform. Users that default on loans may have worse sentiment because they are predisposed to a bad situation--influencing their perception of the product. Before analyzing sentiment, we can match users that paid their loans in full to users that defaulted based on the characteristics we can observe. If matching is successful, we could then make a statement about the causal effect defaulting has on sentiment if we are confident our samples are sufficiently balanced and our model is free from omitted variable bias.
This example, however, only goes through the matching procedure, which can be broken down into the following steps:
import warnings warnings.filterwarnings('ignore') from pymatch.Matcher import Matcher import pandas as pd import numpy as np %matplotlib inline
Load the dataset (
loan.csv) and select a subset of columns.
path = "/Users/bmiroglio/Downloads/lending-club-loan-data/loan.csv" fields = \ [ "loan_amnt", "funded_amnt", "funded_amnt_inv", "term", "int_rate", "installment", "grade", "sub_grade", "loan_status" ] data = pd.read_csv(path)[fields]
Create test and control groups and reassign
loan_status to be a binary treatment indicator. This is our reponse in the logistic regression model(s) used to generate propensity scores.
test = data[data.loan_status == "Default"] control = data[data.loan_status == "Fully Paid"] test['loan_status'] = 1 control['loan_status'] = 0
Matcherprints the formula used to fit logistic regression model(s) and the number of records in the majority/minority class.
Matcherwill use all covariates in the dataset unless a formula is specified by the user. Note that this step is only fitting model(s), we assign propensity scores later.
excludeparameter will be ignored from the model fitting process. This parameter is particularly useful for unique identifiers like a
m = Matcher(test, control, yvar="loan_status", exclude=)
Formula: loan_status ~ loan_amnt+funded_amnt+funded_amnt_inv+term+int_rate+installment+grade+sub_grade n majority: 207723 n minority: 1219
There is a significant Class Imbalance in our data--the majority group (fully-paid loans) having many more records than the minority group (defaulted loans). We account for this by setting
balance=True when calling
Matcher.fit_scores() below. This tells
Matcher to sample from the majority group when fitting the logistic regression model(s) so that the groups are of equal size. When undersampling this way, it is highly recommended that
nmodels is explicitly assigned to a integer much larger than 1. This ensures that more of the majority group is contributing to the generation of propensity scores. The value of this integer should depend on the severity of the imbalance: here we use
# for reproducibility np.random.seed(20170925) m.fit_scores(balance=True, nmodels=100)
Fitting 100 Models on Balanced Samples... Average Accuracy: 70.21%
The average accuracy of our 100 models is 70.21%, suggesting that there's separability within our data and justifiying the need for the matching procedure. It's worth noting that we don't pay much attention to these logistic models since we are using them as a feature extraction tool (generation of propensity scores). The accuracy is a good way to detect separability at a glance, but we shouldn't spend time tuning and tinkering with these models. If our accuracy was close to 50%, that would suggest we cannot detect much separability in our groups given the features we observe and that matching is probably not necessary (or more features should be included if possible).
The plot above demonstrates the separability present in our data. Test profiles have a much higher propensity, or estimated probability of defaulting given the features we isolated in the data.
Matcher.match() method matches profiles that have propensity scores within some threshold.
i.e. for two scores
|s1 - s2| <=
By default matches are found from the majority group for the minority group. For example, if our test group contains 1,000 records and our control group contains 20,000,
iterate through the test (minority) group and find suitable matches from the control (majority) group. If a record in the minority group has no suitable matches, it is dropped from the final matched dataset. We need to ensure our threshold is small enough such that we get close matches and retain most (or all) of our data in the minority group.
Below we tune the threshold using
method="random". This matches a random profile that is within the threshold
as there could be many. This is much faster than the alternative method "min", which finds the closest match for every minority record.
It looks like a threshold of 0.0001 retains 100% of our data. Let's proceed with matching using this threshold.
Below we match one record from the majority group to each record in the minority group. This is done with replacement, meaning a single majority record can be matched to multiple minority records.
Matcher assigns a unique
record_id to each record in the test and control groups so this can be addressed after matching. If subsequent modeling is planned, one might consider weighting models using a weight vector of 1/
f for each record,
f being a record's frequency in the matched dataset. Thankfully
Matcher can handle all of this for you :).
m.match(method="min", nmatches=1, threshold=0.0001)
It looks like the bulk of our matched-majority-group records occur only once, 68 occur twice, ... etc. We can preemptively generate a weight vector using
Let's take a look at our matched data thus far. Note that in addition to the weight vector,
Matcher has also assigned a
match_id to each record indicating our (in this cased) paired matches since we use
nmatches=1. We can verify that matched records have
scores within 0.0001 of each other.
We must now determine if our data is "balanced" across our covariates. Can we detect any statistical differences between the covariates of our matched test and control groups?
Matcher is configured to treat categorical and continuous variables separately in this assessment.
For categorical variables, we look at plots comparing the proportional differences between test and control before and after matching.
For example, the first plot shows:
prop_controlfor all possible
prop_controlbeing the proportion of test and control records with a given term value, respectively. We want these (orange) bars to be small after matching.
categorical_results = m.compare_categorical(return_table=True)
Looking at the plots and test results, we did a pretty good job balancing our categorical features! The p-values from the Chi-Square tests are all > 0.05 and we can verify by observing the small proportional differences in the plots.
For continous variables we look at Empirical Cumulative Distribution Functions (ECDF) for our test and control groups before and after matching.
For example, the first plot pair shows:
ECDF for test vs ECDF for control before matching (left), ECDF for test vs ECDF for control after matching (right). We want the two lines to be very close to each other (or indistiguishable) after matching.
Some tests + metrics are included in the chart titles.
pymatch.functions.ks_boot()This is an adaptation of the
ks.boot()method in the R "Matching" package
Other included Stats:
abs(mean(control) - mean(test))/
cc = m.compare_continuous(return_table=True)
We want the pvalues from both the KS-test and the grouped permutation of the Chi-Square distance after matching to be > 0.05, and they all are! We can verify by looking at how close the ECDFs are between test and control.
We saw a very "clean" result from the above procedure, achieving balance among all the covariates. In my work at Mozilla, we see much hairier results using the same procedure, which will likely be your experience too. In the case that certain covariates are not well balanced, one might consider tinkering with the parameters of the matching process (
nmatches>1) or adding more covariates to the formula specified when we initialized the
In any case, in subsequent modeling, you can always control for variables that you haven't deemed "balanced".