Skip to content

Commit 7d21e99

Browse files
authored
Merge pull request #311 from NathanielF/inv_propensity_model
Inverse propensity model
2 parents 1bc3162 + 0a54532 commit 7d21e99

17 files changed

+3338
-7
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,17 @@ Instrumental Variable regression is an appropriate technique when you wish to es
188188

189189
![](https://raw.githubusercontent.com/pymc-labs/CausalPy/main/docs/source/_static/iv_reg1.png)
190190

191+
192+
### Inverse Propensity Score Weighting
193+
194+
Propensity scores are often used to address the risks of bias or confounding introduced in an observational study by
195+
selection effects into the treatment condition. Propensity scores can be used in a number of ways, but here we demonstrate
196+
their usage within corrective weighting schemes aimed to recover as-if random allocation of subjects to the treatment condition.
197+
The technique "up-weights" or "down-weights" individual observations to better estimate a causal estimand such as the average treatment
198+
effect.
199+
200+
![](https://raw.githubusercontent.com/pymc-labs/CausalPy/main/docs/source/_static/propensity_weight.png)
201+
191202
## Learning resources
192203

193204
Here are some general resources about causal inference:

causalpy/data/datasets.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"anova1": {"filename": "ancova_generated.csv"},
3535
"geolift1": {"filename": "geolift1.csv"},
3636
"risk": {"filename": "AJR2001.csv"},
37+
"nhefs": {"filename": "nhefs.csv"},
3738
}
3839

3940

causalpy/data/nhefs.csv

Lines changed: 1567 additions & 0 deletions
Large diffs are not rendered by default.

causalpy/data_validation.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,29 @@ def _input_validation(self):
146146
the assumption of a simple IV experiment.
147147
The coefficients should be interpreted appropriately."""
148148
)
149+
150+
151+
class PropensityDataValidator:
152+
"""Mixin class for validating the input data and model formula for Propensity Weighting experiments."""
153+
154+
def _input_validation(self):
155+
"""Validate the input data and model formula for correctness"""
156+
treatment = self.formula.split("~")[0]
157+
test = treatment.strip() in self.data.columns
158+
test = test & (self.outcome_variable in self.data.columns)
159+
if not test:
160+
raise DataException(
161+
f"""
162+
The treatment variable:
163+
{treatment} must appear in the data to be used
164+
as an outcome variable. And {self.outcome_variable}
165+
must also be available in the data to be re-weighted
166+
"""
167+
)
168+
T = self.data[treatment.strip()]
169+
check_binary = len(np.unique(T)) > 2
170+
if check_binary:
171+
raise DataException(
172+
"""Warning. The treatment variable is not 0-1 Binary.
173+
"""
174+
)

0 commit comments

Comments
 (0)