@@ -335,10 +335,15 @@ def estimate_unit_ate(self) -> float:
335
335
:return: The unit average treatment effect and the 95% Wald confidence intervals.
336
336
"""
337
337
model = self ._run_linear_regression ()
338
- assert self .treatment in model .params , f"{ self .treatment } not in { model .params } "
339
- unit_effect = model .params [[self .treatment ]].values [0 ] # Unit effect is the coefficient of the treatment
340
- [ci_low , ci_high ] = self ._get_confidence_intervals (model )
341
-
338
+ newline = "\n "
339
+ print (model .conf_int ())
340
+ treatment = [self .treatment ]
341
+ if str (self .df .dtypes [self .treatment ]) == "object" :
342
+ reference = min (self .df [self .treatment ])
343
+ treatment = [x .replace ("[" , "[T." ) for x in dmatrix (f"{ self .treatment } -1" , self .df .query (f"{ self .treatment } != '{ reference } '" ), return_type = "dataframe" ).columns ]
344
+ assert set (treatment ).issubset (model .params .index .tolist ()), f"{ treatment } not in\n { ' ' + str (model .params .index ).replace (newline , newline + ' ' )} "
345
+ unit_effect = model .params [treatment ] # Unit effect is the coefficient of the treatment
346
+ [ci_low , ci_high ] = self ._get_confidence_intervals (model , treatment )
342
347
return unit_effect , [ci_low , ci_high ]
343
348
344
349
def estimate_ate (self ) -> tuple [float , list [float , float ], float ]:
@@ -353,19 +358,14 @@ def estimate_ate(self) -> tuple[float, list[float, float], float]:
353
358
# Create an empty individual for the control and treated
354
359
individuals = pd .DataFrame (1 , index = ["control" , "treated" ], columns = model .params .index )
355
360
356
- # This is a temporary hack
357
- # for t in self.square_terms:
358
- # individuals[t + "^2"] = individuals[t] ** 2
359
- # for a, b in self.product_terms:
360
- # individuals[f"{a}*{b}"] = individuals[a] * individuals[b]
361
-
362
361
# It is ABSOLUTELY CRITICAL that these go last, otherwise we can't index
363
362
# the effect with "ate = t_test_results.effect[0]"
364
363
individuals .loc ["control" , [self .treatment ]] = self .control_value
365
364
individuals .loc ["treated" , [self .treatment ]] = self .treatment_value
366
365
367
366
# Perform a t-test to compare the predicted outcome of the control and treated individual (ATE)
368
367
t_test_results = model .t_test (individuals .loc ["treated" ] - individuals .loc ["control" ])
368
+ print ("t_test_results" , t_test_results .effect )
369
369
ate = t_test_results .effect [0 ]
370
370
confidence_intervals = list (t_test_results .conf_int ().flatten ())
371
371
return ate , confidence_intervals
@@ -473,21 +473,16 @@ def _run_linear_regression(self) -> RegressionResultsWrapper:
473
473
cols = [self .treatment ]
474
474
cols += [x for x in self .adjustment_set if x not in cols ]
475
475
treatment_and_adjustments_cols = reduced_df [cols + ["Intercept" ]]
476
- for col in treatment_and_adjustments_cols :
477
- if str (treatment_and_adjustments_cols .dtypes [col ]) == "object" :
478
- treatment_and_adjustments_cols = pd .get_dummies (
479
- treatment_and_adjustments_cols , columns = [col ], drop_first = True
480
- )
481
476
model = smf .ols (formula = self .formula , data = self .df ).fit ()
482
477
return model
483
478
484
- def _get_confidence_intervals (self , model ):
479
+ def _get_confidence_intervals (self , model , treatment ):
485
480
confidence_intervals = model .conf_int (alpha = 0.05 , cols = None )
486
481
ci_low , ci_high = (
487
- confidence_intervals [0 ][[ self . treatment ] ],
488
- confidence_intervals [1 ][[ self . treatment ] ],
482
+ confidence_intervals [0 ]. loc [ treatment ],
483
+ confidence_intervals [1 ]. loc [ treatment ],
489
484
)
490
- return [ci_low . values [ 0 ] , ci_high . values [ 0 ] ]
485
+ return [ci_low , ci_high ]
491
486
492
487
493
488
class InstrumentalVariableEstimator (Estimator ):
0 commit comments