Skip to content

Commit e3c36a4

Browse files
committed
Cleaned add_..._terms_to_df
1 parent 02b4725 commit e3c36a4

File tree

5 files changed

+0
-61
lines changed

5 files changed

+0
-61
lines changed

causal_testing/testing/estimators.py

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -331,54 +331,6 @@ def add_modelling_assumptions(self):
331331
"do not need to be linear."
332332
)
333333

334-
# def add_squared_term_to_df(self, term_to_square: str):
335-
# """Add a squared term to the linear regression model and df.
336-
#
337-
# This enables the user to capture curvilinear relationships with a linear regression model, not just straight
338-
# lines, while automatically adding the modelling assumption imposed by the addition of this term.
339-
#
340-
# :param term_to_square: The term (column in data and variable in DAG) which is to be squared.
341-
# """
342-
# new_term = str(term_to_square) + "^2"
343-
# self.df[new_term] = self.df[term_to_square] ** 2
344-
# self.adjustment_set.add(new_term)
345-
# self.modelling_assumptions += (
346-
# f"Relationship between {self.treatment} and {self.outcome} varies quadratically" f"with {term_to_square}."
347-
# )
348-
# self.square_terms.append(term_to_square)
349-
350-
# def add_inverse_term_to_df(self, term_to_invert: str):
351-
# """Add an inverse term to the linear regression model and df.
352-
#
353-
# This enables the user to capture curvilinear relationships with a linear regression model, not just straight
354-
# lines, while automatically adding the modelling assumption imposed by the addition of this term.
355-
#
356-
# :param term_to_square: The term (column in data and variable in DAG) which is to be squared.
357-
# """
358-
# new_term = "1/" + str(term_to_invert)
359-
# self.df[new_term] = 1 / self.df[term_to_invert]
360-
# self.adjustment_set.add(new_term)
361-
# self.modelling_assumptions += (
362-
# f"Relationship between {self.treatment} and {self.outcome} varies inversely" f"with {term_to_invert}."
363-
# )
364-
# self.inverse_terms.append(term_to_invert)
365-
366-
# def add_product_term_to_df(self, term_a: str, term_b: str):
367-
# """Add a product term to the linear regression model and df.
368-
#
369-
# This enables the user to capture interaction between a pair of variables in the model. In other words, while
370-
# each covariate's contribution to the mean is assumed to be independent of the other covariates, the pair of
371-
# product terms term_a*term_b a are restricted to vary linearly with each other.
372-
#
373-
# :param term_a: The first term of the product term.
374-
# :param term_b: The second term of the product term.
375-
# """
376-
# new_term = str(term_a) + "*" + str(term_b)
377-
# self.df[new_term] = self.df[term_a] * self.df[term_b]
378-
# self.adjustment_set.add(new_term)
379-
# self.modelling_assumptions += f"{term_a} and {term_b} vary linearly with each other."
380-
# self.product_terms.append((term_a, term_b))
381-
382334
def estimate_unit_ate(self) -> float:
383335
"""Estimate the unit average treatment effect of the treatment on the outcome. That is, the change in outcome
384336
caused by a unit change in treatment.

examples/covasim_/doubling_beta/causal_test_beta.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ def doubling_beta_CATE_on_csv(observational_data_path: str, simulate_counterfact
5454
formula="cum_infections ~ beta + np.power(beta, 2) + avg_age + contacts")
5555

5656
# Add squared terms for beta, since it has a quadratic relationship with cumulative infections
57-
# linear_regression_estimator.add_squared_term_to_df('beta')
5857
causal_test_result = causal_test_engine.execute_test(linear_regression_estimator, causal_test_case, 'ate')
5958

6059
# Repeat for association estimate (no adjustment)
@@ -63,7 +62,6 @@ def doubling_beta_CATE_on_csv(observational_data_path: str, simulate_counterfact
6362
'cum_infections',
6463
df=past_execution_df,
6564
formula="cum_infections ~ beta + np.power(beta, 2)")
66-
# no_adjustment_linear_regression_estimator.add_squared_term_to_df('beta')
6765
association_test_result = causal_test_engine.execute_test(no_adjustment_linear_regression_estimator, causal_test_case, 'ate')
6866

6967
# Store results for plotting
@@ -86,7 +84,6 @@ def doubling_beta_CATE_on_csv(observational_data_path: str, simulate_counterfact
8684
'cum_infections',
8785
df=counterfactual_past_execution_df,
8886
formula="cum_infections ~ beta + np.power(beta, 2) + avg_age + contacts")
89-
# counterfactual_linear_regression_estimator.add_squared_term_to_df('beta')
9087
counterfactual_causal_test_result = causal_test_engine.execute_test(linear_regression_estimator, causal_test_case, 'ate')
9188
results_dict['counterfactual'] = {'ate': counterfactual_causal_test_result.test_value.value,
9289
'cis': counterfactual_causal_test_result.confidence_intervals,

examples/poisson/run_causal_tests.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,7 @@ class MyJsonUtility(JsonUtility):
150150
def add_modelling_assumptions(self, estimation_model: Estimator):
151151
# Add squared intensity term as a modelling assumption if intensity is the treatment of the test
152152
if "intensity" in estimation_model.treatment[0]:
153-
estimation_model.add_squared_term_to_df("intensity")
154153
estimation_model.intercept = 0
155-
if isinstance(estimation_model, WidthHeightEstimator):
156-
estimation_model.add_product_term_to_df("width", "intensity")
157-
estimation_model.add_product_term_to_df("height", "intensity")
158154

159155

160156
if __name__ == "__main__":

tests/testing_tests/test_causal_test_engine.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,6 @@ def test_execute_test_observational_linear_regression_estimator_squared_term(sel
220220
self.causal_test_engine.scenario_execution_data_df,
221221
formula=f"C ~ A + {'+'.join(self.minimal_adjustment_set)} + (D ** 2)"
222222
)
223-
# estimation_model.add_squared_term_to_df("D")
224223
causal_test_result = self.causal_test_engine.execute_test(estimation_model, self.causal_test_case)
225224
self.assertAlmostEqual(round(causal_test_result.test_value.value, 1), 4, delta=1)
226225

tests/testing_tests/test_estimators.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ def test_program_11_3(self):
168168
"""Test whether our linear regression implementation produces the same results as program 11.3 (p. 144)."""
169169
df = self.chapter_11_df.copy()
170170
linear_regression_estimator = LinearRegressionEstimator("treatments", 100, 90, set(), "outcomes", df, formula="outcomes ~ treatments + np.power(treatments, 2)")
171-
# linear_regression_estimator.add_squared_term_to_df("treatments")
172171
model = linear_regression_estimator._run_linear_regression()
173172
ate, _ = linear_regression_estimator.estimate_unit_ate()
174173
self.assertEqual(
@@ -210,7 +209,6 @@ def test_program_15_1A(self):
210209
# terms_to_square = ["age", "wt71", "smokeintensity", "smokeyrs"]
211210
# terms_to_product = [("qsmk", "smokeintensity")]
212211
# for term_to_square in terms_to_square:
213-
# linear_regression_estimator.add_squared_term_to_df(term_to_square)
214212
# for term_a, term_b in terms_to_product:
215213
# linear_regression_estimator.add_product_term_to_df(term_a, term_b)
216214

@@ -242,7 +240,6 @@ def test_program_15_no_interaction(self):
242240
formula="wt82_71 ~ qsmk + age + np.power(age, 2) + wt71 + np.power(wt71, 2) + smokeintensity + np.power(smokeintensity, 2) + smokeyrs + np.power(smokeyrs, 2)")
243241
# terms_to_square = ["age", "wt71", "smokeintensity", "smokeyrs"]
244242
# for term_to_square in terms_to_square:
245-
# linear_regression_estimator.add_squared_term_to_df(term_to_square)
246243
ate, [ci_low, ci_high] = linear_regression_estimator.estimate_unit_ate()
247244
self.assertEqual(round(ate, 1), 3.5)
248245
self.assertEqual([round(ci_low, 1), round(ci_high, 1)], [2.6, 4.3])
@@ -271,7 +268,6 @@ def test_program_15_no_interaction_ate(self):
271268
formula="wt82_71 ~ qsmk + age + np.power(age, 2) + wt71 + np.power(wt71, 2) + smokeintensity + np.power(smokeintensity, 2) + smokeyrs + np.power(smokeyrs, 2)")
272269
# terms_to_square = ["age", "wt71", "smokeintensity", "smokeyrs"]
273270
# for term_to_square in terms_to_square:
274-
# linear_regression_estimator.add_squared_term_to_df(term_to_square)
275271
ate, [ci_low, ci_high] = linear_regression_estimator.estimate_ate()
276272
self.assertEqual(round(ate, 1), 3.5)
277273
self.assertEqual([round(ci_low, 1), round(ci_high, 1)], [2.6, 4.3])
@@ -300,7 +296,6 @@ def test_program_15_no_interaction_ate_calculated(self):
300296
formula="wt82_71 ~ qsmk + age + np.power(age, 2) + wt71 + np.power(wt71, 2) + smokeintensity + np.power(smokeintensity, 2) + smokeyrs + np.power(smokeyrs, 2)")
301297
# terms_to_square = ["age", "wt71", "smokeintensity", "smokeyrs"]
302298
# for term_to_square in terms_to_square:
303-
# linear_regression_estimator.add_squared_term_to_df(term_to_square)
304299
ate, [ci_low, ci_high] = linear_regression_estimator.estimate_ate_calculated(
305300
{k: self.nhefs_df.mean()[k] for k in covariates}
306301
)

0 commit comments

Comments
 (0)