Matching
match_optimal
match_optimal(df: DataFrame, treatment_col: str = TREATMENT_COL, ps_col: str = PS_COL, pid_col: str = PID_COL, n_controls: int = 1, caliper: float = None) -> pd.DataFrame
Matches treated individuals to control individuals based on propensity scores with the option to specify the number of controls per treated individual and a caliper.
This function uses optimal matching to minimize the total distance between treated and control subjects, which typically produces better overall balance than greedy matching approaches.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
DataFrame containing treated and control individuals. |
required |
treatment_col
|
str
|
Column name indicating treatment status. |
TREATMENT_COL
|
ps_col
|
str
|
Column name for propensity score. |
PS_COL
|
pid_col
|
str
|
Column name for individual ID. |
PID_COL
|
n_controls
|
int
|
Number of controls to match for each treated individual. Must be >= 1. Common values: - 1: 1:1 matching (most common, maximizes precision) - 2-5: small ratios for bias-variance tradeoff - 10+: large ratios when controls are abundant |
1
|
caliper
|
float
|
Maximum allowable distance (propensity score difference) for matching. Must be >= 0 when provided. If None, no caliper is applied. Common values: - 0.1: loose caliper, allows moderate PS differences - 0.05: moderate caliper, good balance of matches vs quality - 0.01-0.02: tight caliper, ensures close PS matches - 0.25*std(PS): standard recommendation (Rosenbaum & Rubin, 1985) |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: DataFrame with treated_pid, control_pid and distance columns. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If n_controls < 1 or caliper < 0. |
Source code in CausalEstimate/matching/matching.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | |
match_eager
match_eager(df: DataFrame, treatment_col: str = TREATMENT_COL, ps_col: str = PS_COL, pid_col: str = PID_COL, caliper: float = None, n_controls: int = 1, strict: bool = True) -> pd.DataFrame
Performs a greedy nearest-neighbor matching based on propensity scores, allowing multiple controls per treated subject. By default, n_controls=1.
This function uses a greedy approach that matches each treated subject to their nearest available control(s) in order. While faster than optimal matching, it may not achieve the best overall balance across all matches.
Matching proceeds in multiple "passes": - Pass 1: each treated tries to find its first best control - Pass 2: each treated tries to find its second best control, etc.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Input dataframe. |
required |
treatment_col
|
str
|
Name of treatment column (1=treated, 0=control). |
TREATMENT_COL
|
ps_col
|
str
|
Name of propensity score column. |
PS_COL
|
pid_col
|
str
|
Name of patient ID column. |
PID_COL
|
caliper
|
float
|
Maximum allowed absolute difference in PS for matching. Must be >= 0 when provided. If no control is within the caliper, that treated subject remains unmatched (or raises ValueError if strict=True). Common values: - 0.1: loose caliper, allows moderate PS differences - 0.05: moderate caliper, good balance of matches vs quality - 0.01-0.02: tight caliper, ensures close PS matches |
None
|
n_controls
|
int
|
How many distinct control matches to find per treated subject. Must be >= 1. Common values: - 1: 1:1 matching (most common, maximizes precision) - 2-5: small ratios for bias-variance tradeoff - 10+: large ratios when controls are abundant |
1
|
strict
|
bool
|
If True, raise a ValueError if any treated subject fails to find a control at any pass. If False, skip unmatched passes. |
True
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame with columns [treated_pid, control_pid, distance]. |
DataFrame
|
This may contain up to (n_controls * number_of_treated) rows, |
DataFrame
|
if all can be matched on every pass. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If strict=True and a treated subject cannot be matched on any pass, or if n_controls < 1 or caliper < 0. |
Source code in CausalEstimate/matching/matching.py
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | |