Skip to content

Handle integer treated variable in RegressionDiscontinuity experiment class #506

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

Merged
merged 1 commit into from
Jul 29, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions causalpy/experiments/regression_discontinuity.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,12 @@ def input_validation(self):
"""The treated variable should be dummy coded. Consisting of 0's and 1's only.""" # noqa: E501
)

# Convert integer treated variable to boolean if needed
if self.data["treated"].dtype in ["int64", "int32"]:
# Make a copy to avoid SettingWithCopyWarning
self.data = self.data.copy()
self.data["treated"] = self.data["treated"].astype(bool)

def _is_treated(self, x):
"""Returns ``True`` if `x` is greater than or equal to the treatment threshold.

Expand Down
49 changes: 49 additions & 0 deletions causalpy/tests/test_input_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,3 +383,52 @@ def test_rkink_epsilon_check():
kink_point=kink,
epsilon=-1,
)


# RegressionDiscontinuity


def setup_regression_discontinuity_data(threshold=0.5):
"""Create data for a regression discontinuity test."""
np.random.seed(42)
x = np.random.uniform(0, 1, 100)
treated = np.where(x > threshold, 1, 0)
y = 2 * x + treated + np.random.normal(0, 1, 100)
return pd.DataFrame({"x": x, "treated": treated, "y": y})


def test_regression_discontinuity_int_treatment():
"""Test that RegressionDiscontinuity works with integer treatment variables."""
threshold = 0.5
df = setup_regression_discontinuity_data(threshold)
assert df["treated"].dtype == np.int64 # Ensure treatment is int

# This should work now with our fix
result = cp.RegressionDiscontinuity(
df,
formula="y ~ 1 + x + treated + x:treated",
model=cp.pymc_models.LinearRegression(sample_kwargs=sample_kwargs),
treatment_threshold=threshold,
)

# Check that the treatment variable was converted to bool
assert result.data["treated"].dtype == bool


def test_regression_discontinuity_bool_treatment():
"""Test that RegressionDiscontinuity works with boolean treatment variables."""
threshold = 0.5
df = setup_regression_discontinuity_data(threshold)
df["treated"] = df["treated"].astype(bool) # Convert to bool
assert df["treated"].dtype == bool # Ensure treatment is bool

# This should work as before
result = cp.RegressionDiscontinuity(
df,
formula="y ~ 1 + x + treated + x:treated",
model=cp.pymc_models.LinearRegression(sample_kwargs=sample_kwargs),
treatment_threshold=threshold,
)

# Check that the treatment variable is still bool
assert result.data["treated"].dtype == bool
6 changes: 3 additions & 3 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.