@@ -158,6 +158,7 @@ def test_program_11_2(self):
158
158
model = linear_regression_estimator ._run_linear_regression ()
159
159
ate , _ = linear_regression_estimator .estimate_unit_ate ()
160
160
161
+ print (model .summary ())
161
162
self .assertEqual (round (model .params ["Intercept" ] + 90 * model .params ["treatments" ], 1 ), 216.9 )
162
163
163
164
# Increasing treatments from 90 to 100 should be the same as 10 times the unit ATE
@@ -166,13 +167,13 @@ def test_program_11_2(self):
166
167
def test_program_11_3 (self ):
167
168
"""Test whether our linear regression implementation produces the same results as program 11.3 (p. 144)."""
168
169
df = self .chapter_11_df .copy ()
169
- linear_regression_estimator = LinearRegressionEstimator ("treatments" , 100 , 90 , set (), "outcomes" , df )
170
- linear_regression_estimator .add_squared_term_to_df ("treatments" )
170
+ 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")
171
172
model = linear_regression_estimator ._run_linear_regression ()
172
173
ate , _ = linear_regression_estimator .estimate_unit_ate ()
173
174
self .assertEqual (
174
175
round (
175
- model .params ["Intercept" ] + 90 * model .params ["treatments" ] + 90 * 90 * model .params ["treatments^2 " ], 1
176
+ model .params ["Intercept" ] + 90 * model .params ["treatments" ] + 90 * 90 * model .params ["np.power( treatments, 2) " ], 1
176
177
),
177
178
197.1 ,
178
179
)
@@ -198,13 +199,19 @@ def test_program_15_1A(self):
198
199
"smokeintensity" ,
199
200
"smokeyrs" ,
200
201
}
201
- linear_regression_estimator = LinearRegressionEstimator ("qsmk" , 1 , 0 , covariates , "wt82_71" , df )
202
- terms_to_square = ["age" , "wt71" , "smokeintensity" , "smokeyrs" ]
203
- terms_to_product = [("qsmk" , "smokeintensity" )]
204
- for term_to_square in terms_to_square :
205
- linear_regression_estimator .add_squared_term_to_df (term_to_square )
206
- for term_a , term_b in terms_to_product :
207
- linear_regression_estimator .add_product_term_to_df (term_a , term_b )
202
+ linear_regression_estimator = LinearRegressionEstimator ("qsmk" , 1 , 0 , covariates , "wt82_71" , df ,
203
+ formula = """wt82_71 ~ qsmk +
204
+ age + np.power(age, 2) +
205
+ wt71 + np.power(wt71, 2) +
206
+ smokeintensity + np.power(smokeintensity, 2) +
207
+ smokeyrs + np.power(smokeyrs, 2) +
208
+ (qsmk * smokeintensity)""" )
209
+ # terms_to_square = ["age", "wt71", "smokeintensity", "smokeyrs"]
210
+ # terms_to_product = [("qsmk", "smokeintensity")]
211
+ # for term_to_square in terms_to_square:
212
+ # linear_regression_estimator.add_squared_term_to_df(term_to_square)
213
+ # for term_a, term_b in terms_to_product:
214
+ # linear_regression_estimator.add_product_term_to_df(term_a, term_b)
208
215
209
216
model = linear_regression_estimator ._run_linear_regression ()
210
217
self .assertEqual (round (model .params ["qsmk" ], 1 ), 2.6 )
@@ -230,10 +237,11 @@ def test_program_15_no_interaction(self):
230
237
"smokeintensity" ,
231
238
"smokeyrs" ,
232
239
}
233
- linear_regression_estimator = LinearRegressionEstimator ("qsmk" , 1 , 0 , covariates , "wt82_71" , df )
234
- terms_to_square = ["age" , "wt71" , "smokeintensity" , "smokeyrs" ]
235
- for term_to_square in terms_to_square :
236
- linear_regression_estimator .add_squared_term_to_df (term_to_square )
240
+ linear_regression_estimator = LinearRegressionEstimator ("qsmk" , 1 , 0 , covariates , "wt82_71" , df ,
241
+ formula = "wt82_71 ~ qsmk + age + np.power(age, 2) + wt71 + np.power(wt71, 2) + smokeintensity + np.power(smokeintensity, 2) + smokeyrs + np.power(smokeyrs, 2)" )
242
+ # terms_to_square = ["age", "wt71", "smokeintensity", "smokeyrs"]
243
+ # for term_to_square in terms_to_square:
244
+ # linear_regression_estimator.add_squared_term_to_df(term_to_square)
237
245
ate , [ci_low , ci_high ] = linear_regression_estimator .estimate_unit_ate ()
238
246
self .assertEqual (round (ate , 1 ), 3.5 )
239
247
self .assertEqual ([round (ci_low , 1 ), round (ci_high , 1 )], [2.6 , 4.3 ])
@@ -258,10 +266,11 @@ def test_program_15_no_interaction_ate(self):
258
266
"smokeintensity" ,
259
267
"smokeyrs" ,
260
268
}
261
- linear_regression_estimator = LinearRegressionEstimator ("qsmk" , 1 , 0 , covariates , "wt82_71" , df )
262
- terms_to_square = ["age" , "wt71" , "smokeintensity" , "smokeyrs" ]
263
- for term_to_square in terms_to_square :
264
- linear_regression_estimator .add_squared_term_to_df (term_to_square )
269
+ linear_regression_estimator = LinearRegressionEstimator ("qsmk" , 1 , 0 , covariates , "wt82_71" , df ,
270
+ formula = "wt82_71 ~ qsmk + age + np.power(age, 2) + wt71 + np.power(wt71, 2) + smokeintensity + np.power(smokeintensity, 2) + smokeyrs + np.power(smokeyrs, 2)" )
271
+ # terms_to_square = ["age", "wt71", "smokeintensity", "smokeyrs"]
272
+ # for term_to_square in terms_to_square:
273
+ # linear_regression_estimator.add_squared_term_to_df(term_to_square)
265
274
ate , [ci_low , ci_high ] = linear_regression_estimator .estimate_ate ()
266
275
self .assertEqual (round (ate , 1 ), 3.5 )
267
276
self .assertEqual ([round (ci_low , 1 ), round (ci_high , 1 )], [2.6 , 4.3 ])
@@ -286,10 +295,11 @@ def test_program_15_no_interaction_ate_calculated(self):
286
295
"smokeintensity" ,
287
296
"smokeyrs" ,
288
297
}
289
- linear_regression_estimator = LinearRegressionEstimator ("qsmk" , 1 , 0 , covariates , "wt82_71" , df )
290
- terms_to_square = ["age" , "wt71" , "smokeintensity" , "smokeyrs" ]
291
- for term_to_square in terms_to_square :
292
- linear_regression_estimator .add_squared_term_to_df (term_to_square )
298
+ linear_regression_estimator = LinearRegressionEstimator ("qsmk" , 1 , 0 , covariates , "wt82_71" , df ,
299
+ formula = "wt82_71 ~ qsmk + age + np.power(age, 2) + wt71 + np.power(wt71, 2) + smokeintensity + np.power(smokeintensity, 2) + smokeyrs + np.power(smokeyrs, 2)" )
300
+ # terms_to_square = ["age", "wt71", "smokeintensity", "smokeyrs"]
301
+ # for term_to_square in terms_to_square:
302
+ # linear_regression_estimator.add_squared_term_to_df(term_to_square)
293
303
ate , [ci_low , ci_high ] = linear_regression_estimator .estimate_ate_calculated (
294
304
{k : self .nhefs_df .mean ()[k ] for k in covariates }
295
305
)
@@ -377,7 +387,7 @@ def test_X1_effect(self):
377
387
"""When we fix the value of X2 to 0, the effect of X1 on Y should become ~2 (because X2 terms are cancelled)."""
378
388
x2 = Input ("X2" , float )
379
389
lr_model = LinearRegressionEstimator (
380
- ( "X1" ,), 1 , 0 , {"X2" }, ( "Y" ,) , effect_modifiers = {x2 : 0 }, formula = "Y ~ X1 + X2 + (X1 * X2)" , df = self .df
390
+ "X1" , 1 , 0 , {"X2" }, "Y" , effect_modifiers = {x2 : 0 }, formula = "Y ~ X1 + X2 + (X1 * X2)" , df = self .df
381
391
)
382
392
test_results = lr_model .estimate_ate ()
383
393
ate = test_results [0 ]
0 commit comments