py-why / EconML

ALICE (Automated Learning and Intelligence for Causation and Economics) is a Microsoft Research project aimed at applying Artificial Intelligence concepts to economic decision making. One of its goals is to build a toolkit that combines state-of-the-art machine learning techniques with econometrics in order to bring automation to complex causal inference problems. To date, the ALICE Python SDK (econml) implements orthogonal machine learning algorithms such as the double machine learning work of Chernozhukov et al. This toolkit is designed to measure the causal effect of some treatment variable(s) t on an outcome variable y, controlling for a set of features x.
https://www.microsoft.com/en-us/research/project/alice/
Other
3.77k stars 713 forks source link

AttributeError: 'CausalEstimate' object has no attribute '_estimator_object' when using DoWhy integration #545

Open EgorKraevTransferwise opened 2 years ago

EgorKraevTransferwise commented 2 years ago

Hi! First of all, thanks for the wonderful packages econml and dowhy!

When trying to follow the example of "Case Study - Customer Segmentation at An Online Media Company - EconML + DoWhy" notebook on my own data, I run

# initiate an EconML cate estimator
est_nonparam = CausalForestDML(model_y=GradientBoostingRegressor(), model_t=GradientBoostingClassifier(), discrete_treatment=True,)
# fit through dowhy
est_nonparam_dw = est_nonparam.dowhy.fit(y, T, X=X, W=W, outcome_names=[outcome], treatment_names=[treatment],
                                         feature_names=features_X, confounder_names=features_float, inference="blb")
point = est_nonparam_dw.effect(X)
lb, ub = est_nonparam_dw.effect_interval(X)

but this generates the below error.

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
C:\Users\EGOR~1.KRA\AppData\Local\Temp/ipykernel_57188/1486408213.py in <module>
      4 est_nonparam_dw = est_nonparam.dowhy.fit(y, T, X=X, W=W, outcome_names=[outcome], treatment_names=[treatment],
      5                                          feature_names=features_X, confounder_names=features_float, inference="blb")
----> 6 point = est_nonparam_dw.effect(X)
      7 lb, ub = est_nonparam_dw.effect_interval(X)

~\.conda\envs\generic3.9\lib\site-packages\econml\dowhy.py in __getattr__(self, attr)
    223         elif attr.startswith('dowhy__'):
    224             return getattr(self.dowhy_, attr[len('dowhy__'):])
--> 225         elif hasattr(self.estimate_._estimator_object, attr):
    226             if hasattr(self.dowhy_, attr):
    227                 warnings.warn("This call is ambiguous, "

AttributeError: 'CausalEstimate' object has no attribute '_estimator_object'

Any idea what could be going wrong there? The code seems to be pretty exactly cut and paste from the notebook (except that the trreatment is categorical), but the code in the example notebook runs fine.

On the other hand,

est3 = CausalForestDML(model_y=GradientBoostingRegressor(), 
                       model_t=GradientBoostingClassifier(),
                       discrete_treatment=True,
                      )
est3.fit(y, T, X=X, W=W)
te_pred3 = est3.effect(X)
lb3, ub3 = est3.effect_interval(X, alpha=0.01)

runs fine, so it doesn't seem to be a data issue.

PhilrainV commented 2 years ago

I also encountered this problem, have you solved it? Can you tell me how to solve it?

EgorKraevTransferwise commented 2 years ago

I switched to calling things the other way around, so using the econml adapter from the DoWhy CausalModel, is much more convenient anyway, rather than the EconML dowhy adapter.

So for example

model = CausalModel(
    data=used_df,
    treatment=treatment,
    outcome=outcome,
    common_causes=features_W,
    effect_modifiers=features_X,
)

estimate = model.estimate_effect(
                identified_estimand,
                method_name="backdoor.econml.dml.CausalForestDML",
                control_value=0,
                treatment_value=1,
                target_units="ate",  # condition used for CATE
                confidence_intervals=True,
                method_params={...},
            )

and this estimate then contains the econml object

PhilrainV commented 2 years ago

I can conduct experiments directly using dowhy's tools, but I need to use EconML's machine learning library for this experiment, so I need to use EconML's fit() function, so do you know any other solutions, thank you.

EgorKraevTransferwise commented 2 years ago

Why not do the fit using the dowhy wrapper, then extract the model object from it (estimate.estimator.estimator in the snippet above) and do whatever you need there?