Skip to content

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 61 additions & 16 deletions causalpy/experiments/diff_in_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def __init__(
formula: str,
time_variable_name: str,
group_variable_name: str,
post_treatment_variable_name: str = "post_treatment",
model=None,
**kwargs,
) -> None:
Expand All @@ -95,6 +96,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)
Expand Down Expand Up @@ -128,6 +130,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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

# 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")
Expand Down Expand Up @@ -173,7 +181,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
Expand All @@ -189,7 +197,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))

Expand All @@ -198,32 +209,66 @@ 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 = (
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 post_treatment:a and post_treatment*b. It should work because we'll always get a coefficient for post_treatment:a, but it is worth adding the test

Copy link
Author

Choose a reason for hiding this comment

The 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 FormulaExeption and DataException

but @drbenvincent can you elaborate on this specific test. Are we also checking the coefficient value where two interaction terms are used?

Copy link
Collaborator

Choose a reason for hiding this comment

The 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?

Copy link
Author

Choose a reason for hiding this comment

The 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 post_treatment * group + post_treatment * group * male which might be uncommon but it’s entirely possible in our setup.

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 post_treatment to move forward

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 the input data and model formula for correctness"""
if "post_treatment" not in self.formula:
raise FormulaException(
"A predictor called `post_treatment` should be in the formula"
)

if "post_treatment" not in self.data.columns:
raise DataException(
"Require a boolean column labelling observations which are `treated`"
)
# Check if post_treatment_variable_name is in formula
if self.post_treatment_variable_name not in self.formula:
if self.post_treatment_variable_name == "post_treatment":
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've got a minor preference to just give one generic exception message, rather than a custom one dependent on self.post_treatment_variable_name. That will also cut down on the number of tests required to achieve high test coverage.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah absolutely!! More generic ones like "Missing required variable '{self.post_treatment_variable_name}' in formula" can be used

# Default case - user didn't specify custom name, so guide them to use "post_treatment"
raise FormulaException(
"Missing 'post_treatment' in formula.\n"
"Note: post_treatment_variable_name might have been set to 'post_treatment' by default.\n"
"Add 'post_treatment' to formula (e.g., 'y ~ 1 + group*post_treatment').\n"
"Or to use custom name, provide additional argument post_treatment_variable_name='your_post_treatment_variable_name'."
)
else:
# Custom case - user specified custom name, so remind them what they specified
raise FormulaException(
f"Missing required variable '{self.post_treatment_variable_name}' in formula.\n\n"
f"Since you specified post_treatment_variable_name='{self.post_treatment_variable_name}', "
f"please ensure formula includes '{self.post_treatment_variable_name}'"
)

# Check if post_treatment_variable_name is in data columns
if self.post_treatment_variable_name not in self.data.columns:
if self.post_treatment_variable_name == "post_treatment":
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above. Just give one more generic exception message, regardless of what self.post_treatment_variable_name is.

# Default case - user didn't specify custom name, so guide them to use "post_treatment"
raise DataException(
"Missing 'post_treatment' column in dataset.\n"
"Note: post_treatment_variable_name might have been set to 'post_treatment' by default.\n"
"Ensure dataset has boolean column 'post_treatment'.\n"
"or to use custom name, provide additional argument post_treatment_variable_name='your_post_treatment_variable_name'."
)
else:
# Custom case - user specified custom name, so remind them what they specified
raise DataException(
f"Missing required column '{self.post_treatment_variable_name}' in dataset.\n\n"
f"Since you specified post_treatment_variable_name='{self.post_treatment_variable_name}', "
f"please ensure dataset has boolean column named '{self.post_treatment_variable_name}'"
)

if "unit" not in self.data.columns:
raise DataException(
Expand Down
8 changes: 4 additions & 4 deletions docs/source/_static/interrogate_badge.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.