-
Notifications
You must be signed in to change notification settings - Fork 78
Refactored scikit-learn flavour of DifferenceInDifferences and allowed custom column names for post_treatment variable. #515
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,8 @@ | |
Difference in differences | ||
""" | ||
|
||
import re | ||
|
||
import arviz as az | ||
import numpy as np | ||
import pandas as pd | ||
|
@@ -84,6 +86,7 @@ def __init__( | |
formula: str, | ||
time_variable_name: str, | ||
group_variable_name: str, | ||
post_treatment_variable_name: str = "post_treatment", | ||
model=None, | ||
**kwargs, | ||
) -> None: | ||
|
@@ -95,6 +98,7 @@ def __init__( | |
self.formula = formula | ||
self.time_variable_name = time_variable_name | ||
self.group_variable_name = group_variable_name | ||
self.post_treatment_variable_name = post_treatment_variable_name | ||
self.input_validation() | ||
|
||
y, X = dmatrices(formula, self.data) | ||
|
@@ -128,6 +132,12 @@ def __init__( | |
} | ||
self.model.fit(X=self.X, y=self.y, coords=COORDS) | ||
elif isinstance(self.model, RegressorMixin): | ||
# For scikit-learn models, automatically set fit_intercept=False | ||
# This ensures the intercept is included in the coefficients array rather than being a separate intercept_ attribute | ||
# without this, the intercept is not included in the coefficients array hence would be displayed as 0 in the model summary | ||
# TODO: later, this should be handled in ScikitLearnAdaptor itself | ||
if hasattr(self.model, "fit_intercept"): | ||
self.model.fit_intercept = False | ||
self.model.fit(X=self.X, y=self.y) | ||
else: | ||
raise ValueError("Model type not recognized") | ||
|
@@ -173,7 +183,7 @@ def __init__( | |
# just the treated group | ||
.query(f"{self.group_variable_name} == 1") | ||
# just the treatment period(s) | ||
.query("post_treatment == True") | ||
.query(f"{self.post_treatment_variable_name} == True") | ||
# drop the outcome variable | ||
.drop(self.outcome_variable_name, axis=1) | ||
# We may have multiple units per time point, we only want one time point | ||
|
@@ -189,7 +199,10 @@ def __init__( | |
# INTERVENTION: set the interaction term between the group and the | ||
# post_treatment variable to zero. This is the counterfactual. | ||
for i, label in enumerate(self.labels): | ||
if "post_treatment" in label and self.group_variable_name in label: | ||
if ( | ||
self.post_treatment_variable_name in label | ||
and self.group_variable_name in label | ||
): | ||
new_x.iloc[:, i] = 0 | ||
self.y_pred_counterfactual = self.model.predict(np.asarray(new_x)) | ||
|
||
|
@@ -198,31 +211,44 @@ def __init__( | |
# This is the coefficient on the interaction term | ||
coeff_names = self.model.idata.posterior.coords["coeffs"].data | ||
for i, label in enumerate(coeff_names): | ||
if "post_treatment" in label and self.group_variable_name in label: | ||
if ( | ||
self.post_treatment_variable_name in label | ||
and self.group_variable_name in label | ||
): | ||
self.causal_impact = self.model.idata.posterior["beta"].isel( | ||
{"coeffs": i} | ||
) | ||
elif isinstance(self.model, RegressorMixin): | ||
# This is the coefficient on the interaction term | ||
# TODO: CHECK FOR CORRECTNESS | ||
self.causal_impact = ( | ||
self.y_pred_treatment[1] - self.y_pred_counterfactual[0] | ||
).item() | ||
# Store the coefficient into dictionary {intercept:value} | ||
coef_map = dict(zip(self.labels, self.model.get_coeffs())) | ||
# Create and find the interaction term based on the values user provided | ||
interaction_term = ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice. We'll need more tests anyway to ensure test coverage, so when you do that can you add cases for when people specify formulas like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, will add some tests for a cases where a user provides post treatment variable name and check for but @drbenvincent can you elaborate on this specific test. Are we also checking the coefficient value where two interaction terms are used? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd not thought of that. I guess it's easy to find and interaction term of the post treatment variable and something else. But if there are two interaction terms, both including the post treatment variable, then that might get messy. Can we think of any situations where that be a good idea? If not, then maybe that could throw and exception and we just say we can't deal with a formula like that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since our users can write any formula freely—unlike other libraries that rely on closed systems—they could specify any formula like The users can obtain estimates for exactly what they define in the formula. However, we’ve built this did object specifically for two-way Diff-in-diff with a single interaction term ?-- thus the other features might get messed up as you said. So yeah @drbenvincent I agree that we could throw exception if we encounter any two interaction term with |
||
f"{self.group_variable_name}:{self.post_treatment_variable_name}" | ||
) | ||
matched_key = next((k for k in coef_map if interaction_term in k), None) | ||
att = coef_map.get(matched_key) | ||
self.causal_impact = att | ||
else: | ||
raise ValueError("Model type not recognized") | ||
|
||
return | ||
|
||
def input_validation(self): | ||
# Validate formula structure and interaction interaction terms | ||
self._validate_formula_interaction_terms() | ||
|
||
"""Validate the input data and model formula for correctness""" | ||
if "post_treatment" not in self.formula: | ||
# Check if post_treatment_variable_name is in formula | ||
if self.post_treatment_variable_name not in self.formula: | ||
raise FormulaException( | ||
"A predictor called `post_treatment` should be in the formula" | ||
f"Missing required variable '{self.post_treatment_variable_name}' in formula" | ||
) | ||
|
||
if "post_treatment" not in self.data.columns: | ||
# Check if post_treatment_variable_name is in data columns | ||
if self.post_treatment_variable_name not in self.data.columns: | ||
raise DataException( | ||
"Require a boolean column labelling observations which are `treated`" | ||
f"Missing required column '{self.post_treatment_variable_name}' in dataset" | ||
) | ||
|
||
if "unit" not in self.data.columns: | ||
|
@@ -236,6 +262,61 @@ def input_validation(self): | |
coded. Consisting of 0's and 1's only.""" | ||
) | ||
|
||
def _get_interaction_terms(self): | ||
""" | ||
Extract interaction terms from the formula. | ||
Returns a list of interaction terms (those with '*' or ':'). | ||
""" | ||
# Define interaction indicators | ||
INTERACTION_INDICATORS = ["*", ":"] | ||
|
||
# Remove whitespace | ||
formula = self.formula.replace(" ", "") | ||
|
||
# Extract right-hand side of the formula | ||
rhs = formula.split("~")[1] | ||
|
||
# Split terms by '+' or '-' while keeping them intact | ||
terms = re.split(r"(?=[+-])", rhs) | ||
|
||
# Clean up terms and get interaction terms (those with '*' or ':') | ||
interaction_terms = [] | ||
for term in terms: | ||
# Remove leading + or - for processing | ||
clean_term = term.lstrip("+-") | ||
if any(indicator in clean_term for indicator in INTERACTION_INDICATORS): | ||
interaction_terms.append(clean_term) | ||
|
||
return interaction_terms | ||
|
||
def _validate_formula_interaction_terms(self): | ||
""" | ||
Validate that the formula contains at most one interaction term and no three-way or higher-order interactions. | ||
Raises FormulaException if more than one interaction term is found or if any interaction term has more than 2 variables. | ||
""" | ||
# Define interaction indicators | ||
INTERACTION_INDICATORS = ["*", ":"] | ||
|
||
# Get interaction terms | ||
interaction_terms = self._get_interaction_terms() | ||
|
||
# Check for interaction terms with more than 2 variables (more than one '*' or ':') | ||
for term in interaction_terms: | ||
total_indicators = sum( | ||
term.count(indicator) for indicator in INTERACTION_INDICATORS | ||
) | ||
if ( | ||
total_indicators >= 2 | ||
): # 3 or more variables (e.g., a*b*c or a:b:c has 2 symbols) | ||
raise FormulaException( | ||
f"Formula contains interaction term with more than 2 variables: {term}. Only two-way interactions are allowed." | ||
) | ||
|
||
if len(interaction_terms) > 1: | ||
raise FormulaException( | ||
f"Formula contains more than 1 interaction term: {interaction_terms}. Maximum of 1 allowed." | ||
) | ||
|
||
def summary(self, round_to=None) -> None: | ||
"""Print summary of main results and model coefficients. | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice