Estimating effect of multiple treatments#
[1]:
import dowhy
dowhy.enable_notebook_rendering()
from dowhy import CausalModel
import dowhy.datasets
import warnings
warnings.filterwarnings('ignore')
[2]:
data = dowhy.datasets.linear_dataset(10, num_common_causes=4, num_samples=10000,
num_instruments=0, num_effect_modifiers=2,
num_treatments=2,
treatment_is_binary=False,
num_discrete_common_causes=2,
num_discrete_effect_modifiers=0,
one_hot_encode=False)
df=data['df']
df.head()
[2]:
| X0 | X1 | W0 | W1 | W2 | W3 | v0 | v1 | y | |
|---|---|---|---|---|---|---|---|---|---|
| 0 | -1.311765 | -1.568984 | -2.546688 | 0.395925 | 0 | 0 | -9.807547 | -13.050328 | -1182.114209 |
| 1 | -0.724059 | 0.059596 | 0.166227 | 0.088218 | 2 | 0 | 11.394021 | 8.411628 | -171.582040 |
| 2 | -0.595318 | 0.621521 | -1.560061 | 1.237462 | 2 | 0 | 8.885696 | -1.118210 | 113.406636 |
| 3 | -1.206896 | 0.251498 | 0.799066 | -0.512244 | 0 | 0 | 2.394178 | 2.807621 | 8.616098 |
| 4 | -1.948951 | -0.612659 | -1.610524 | 1.357947 | 2 | 0 | 9.447075 | -0.842088 | 176.154450 |
[3]:
model = CausalModel(data=data["df"],
treatment=data["treatment_name"], outcome=data["outcome_name"],
graph=data["gml_graph"])
[4]:
model.view_model()
from IPython.display import Image, display
display(Image(filename="causal_model.png"))
[5]:
identified_estimand= model.identify_effect(proceed_when_unidentifiable=True)
print(identified_estimand)
Estimand type: EstimandType.NONPARAMETRIC_ATE
### Estimand : 1
Estimand name: backdoor
Estimand expression:
d
─────────(E[y|W2,W0,W3,W1])
d[v₀ v₁]
Estimand assumption 1, Unconfoundedness: If U→{v0,v1} and U→y then P(y|v0,v1,W2,W0,W3,W1,U) = P(y|v0,v1,W2,W0,W3,W1)
### Estimand : 2
Estimand name: iv
No such variable(s) found!
### Estimand : 3
Estimand name: frontdoor
No such variable(s) found!
Linear model#
Let us first see an example for a linear model. The control_value and treatment_value can be provided as a tuple/list when the treatment is multi-dimensional.
The interpretation is change in y when v0 and v1 are changed from (0,0) to (1,1).
[6]:
linear_estimate = model.estimate_effect(identified_estimand,
method_name="backdoor.linear_regression",
control_value=(0,0),
treatment_value=(1,1),
method_params={'need_conditional_estimates': False})
print(linear_estimate)
*** Causal Estimate ***
## Identified estimand
Estimand type: EstimandType.NONPARAMETRIC_ATE
### Estimand : 1
Estimand name: backdoor
Estimand expression:
d
─────────(E[y|W2,W0,W3,W1])
d[v₀ v₁]
Estimand assumption 1, Unconfoundedness: If U→{v0,v1} and U→y then P(y|v0,v1,W2,W0,W3,W1,U) = P(y|v0,v1,W2,W0,W3,W1)
## Realized estimand
b: y~v0+v1+W2+W0+W3+W1+v0*X1+v0*X0+v1*X1+v1*X0
Target units: ate
## Estimate
Mean value: -18.564925008337156
You can estimate conditional effects, based on effect modifiers.
[7]:
linear_estimate = model.estimate_effect(identified_estimand,
method_name="backdoor.linear_regression",
control_value=(0,0),
treatment_value=(1,1))
print(linear_estimate)
*** Causal Estimate ***
## Identified estimand
Estimand type: EstimandType.NONPARAMETRIC_ATE
### Estimand : 1
Estimand name: backdoor
Estimand expression:
d
─────────(E[y|W2,W0,W3,W1])
d[v₀ v₁]
Estimand assumption 1, Unconfoundedness: If U→{v0,v1} and U→y then P(y|v0,v1,W2,W0,W3,W1,U) = P(y|v0,v1,W2,W0,W3,W1)
## Realized estimand
b: y~v0+v1+W2+W0+W3+W1+v0*X1+v0*X0+v1*X1+v1*X0
Target units:
## Estimate
Mean value: -18.564925008337156
### Conditional Estimates
__categorical__X1 __categorical__X0
(-3.913, -1.025] (-4.197, -1.517] -116.733201
(-1.517, -0.919] -57.451380
(-0.919, -0.433] -20.763457
(-0.433, 0.166] 15.228586
(0.166, 2.977] 80.317656
(-1.025, -0.458] (-4.197, -1.517] -115.985904
(-1.517, -0.919] -56.184739
(-0.919, -0.433] -19.930239
(-0.433, 0.166] 16.547837
(0.166, 2.977] 76.264074
(-0.458, 0.0599] (-4.197, -1.517] -114.229124
(-1.517, -0.919] -55.978954
(-0.919, -0.433] -18.292503
(-0.433, 0.166] 17.296747
(0.166, 2.977] 80.426287
(0.0599, 0.653] (-4.197, -1.517] -113.502305
(-1.517, -0.919] -56.126682
(-0.919, -0.433] -18.553484
(-0.433, 0.166] 19.415950
(0.166, 2.977] 79.025513
(0.653, 3.382] (-4.197, -1.517] -114.555769
(-1.517, -0.919] -53.714405
(-0.919, -0.433] -17.248778
(-0.433, 0.166] 18.916412
(0.166, 2.977] 81.751067
dtype: float64
More methods#
You can also use methods from EconML or CausalML libraries that support multiple treatments. You can look at examples from the conditional effect notebook: https://py-why.github.io/dowhy/example_notebooks/dowhy-conditional-treatment-effects.html
Propensity-based methods do not support multiple treatments currently.