Skip to content

Commit 66c35ac

Browse files
Merge pull request #265 from CITCOM-project/test-categorical-ci
Test categorical confidence intervals
2 parents 7f46e82 + d5921c4 commit 66c35ac

File tree

2 files changed

+32
-24
lines changed

2 files changed

+32
-24
lines changed

tests/data/scarf_data.csv

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
length_in,large_gauge,color,completed
2+
55,1,orange,1
3+
55,0,orange,1
4+
55,0,brown,1
5+
60,0,brown,1
6+
60,0,grey,0
7+
70,0,grey,1
8+
70,0,orange,0
9+
82,1,grey,1
10+
82,0,brown,0
11+
82,0,orange,0
12+
82,1,brown,0

tests/testing_tests/test_estimators.py

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
CausalForestEstimator,
88
LogisticRegressionEstimator,
99
InstrumentalVariableEstimator,
10-
CubicSplineRegressionEstimator
10+
CubicSplineRegressionEstimator,
1111
)
1212
from causal_testing.specification.variable import Input
1313
from causal_testing.utils.validation import CausalValidator
@@ -78,21 +78,7 @@ class TestLogisticRegressionEstimator(unittest.TestCase):
7878

7979
@classmethod
8080
def setUpClass(cls) -> None:
81-
cls.scarf_df = pd.DataFrame(
82-
[
83-
{"length_in": 55, "large_gauge": 1, "color": "orange", "completed": 1},
84-
{"length_in": 55, "large_gauge": 0, "color": "orange", "completed": 1},
85-
{"length_in": 55, "large_gauge": 0, "color": "brown", "completed": 1},
86-
{"length_in": 60, "large_gauge": 0, "color": "brown", "completed": 1},
87-
{"length_in": 60, "large_gauge": 0, "color": "grey", "completed": 0},
88-
{"length_in": 70, "large_gauge": 0, "color": "grey", "completed": 1},
89-
{"length_in": 70, "large_gauge": 0, "color": "orange", "completed": 0},
90-
{"length_in": 82, "large_gauge": 1, "color": "grey", "completed": 1},
91-
{"length_in": 82, "large_gauge": 0, "color": "brown", "completed": 0},
92-
{"length_in": 82, "large_gauge": 0, "color": "orange", "completed": 0},
93-
{"length_in": 82, "large_gauge": 1, "color": "brown", "completed": 0},
94-
]
95-
)
81+
cls.scarf_df = pd.read_csv("tests/data/scarf_data.csv")
9682

9783
# Yes, this probably shouldn't be in here, but it uses the scarf data so it makes more sense to put it
9884
# here than duplicating the scarf data for a single test
@@ -416,12 +402,11 @@ def test_program_11_2_with_robustness_validation(self):
416402

417403

418404
class TestCubicSplineRegressionEstimator(TestLinearRegressionEstimator):
419-
420405
@classmethod
421-
422406
def setUpClass(cls):
423407

424408
super().setUpClass()
409+
425410
def test_program_11_3_cublic_spline(self):
426411

427412
"""Test whether the cublic_spline regression implementation produces the same results as program 11.3 (p. 162).
@@ -431,8 +416,7 @@ def test_program_11_3_cublic_spline(self):
431416

432417
df = self.chapter_11_df.copy()
433418

434-
cublic_spline_estimator = CubicSplineRegressionEstimator(
435-
"treatments", 1, 0, set(), "outcomes", 3, df)
419+
cublic_spline_estimator = CubicSplineRegressionEstimator("treatments", 1, 0, set(), "outcomes", 3, df)
436420

437421
model = cublic_spline_estimator._run_linear_regression()
438422

@@ -453,8 +437,6 @@ def test_program_11_3_cublic_spline(self):
453437
self.assertAlmostEqual(ate_1 * 2, ate_2)
454438

455439

456-
457-
458440
class TestCausalForestEstimator(unittest.TestCase):
459441
"""Test the linear regression estimator against the programming exercises in Section 2 of Hernán and Robins [1].
460442
@@ -527,15 +509,29 @@ def setUpClass(cls) -> None:
527509
df = pd.DataFrame({"X1": np.random.uniform(-1000, 1000, 1000), "X2": np.random.uniform(-1000, 1000, 1000)})
528510
df["Y"] = 2 * df["X1"] - 3 * df["X2"] + 2 * df["X1"] * df["X2"] + 10
529511
cls.df = df
512+
cls.scarf_df = pd.read_csv("tests/data/scarf_data.csv")
530513

531514
def test_X1_effect(self):
532515
"""When we fix the value of X2 to 0, the effect of X1 on Y should become ~2 (because X2 terms are cancelled)."""
533-
x2 = Input("X2", float)
534516
lr_model = LinearRegressionEstimator(
535-
"X1", 1, 0, {"X2"}, "Y", effect_modifiers={x2.name: 0}, formula="Y ~ X1 + X2 + (X1 * X2)", df=self.df
517+
"X1", 1, 0, {"X2"}, "Y", effect_modifiers={"x2": 0}, formula="Y ~ X1 + X2 + (X1 * X2)", df=self.df
536518
)
537519
test_results = lr_model.estimate_ate()
538520
ate = test_results[0]
539521
self.assertAlmostEqual(ate, 2.0)
540522

523+
def test_categorical_confidence_intervals(self):
524+
lr_model = LinearRegressionEstimator(
525+
treatment="color",
526+
control_value=None,
527+
treatment_value=None,
528+
adjustment_set={},
529+
outcome="length_in",
530+
df=self.scarf_df,
531+
)
532+
coefficients, [ci_low, ci_high] = lr_model.estimate_coefficient()
541533

534+
# The precise values don't really matter. This test is primarily intended to make sure the return type is correct.
535+
self.assertTrue(coefficients.round(2).equals(pd.Series({"color[T.grey]": 0.92, "color[T.orange]": -4.25})))
536+
self.assertTrue(ci_low.round(2).equals(pd.Series({"color[T.grey]": -22.12, "color[T.orange]": -25.58})))
537+
self.assertTrue(ci_high.round(2).equals(pd.Series({"color[T.grey]": 23.95, "color[T.orange]": 17.08})))

0 commit comments

Comments
 (0)