Estimating effect of multiple treatments#
[1]:
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 | 0.178007 | -0.639310 | -1.788712 | 0.246032 | 3 | 1 | 4.647042 | 3.825705 | 65.322247 |
| 1 | 1.792623 | -1.013292 | 0.876567 | -1.763037 | 0 | 2 | 0.107641 | 6.266805 | 70.627946 |
| 2 | 0.204256 | -0.194886 | 1.185497 | -2.261566 | 1 | 1 | 0.332344 | 8.690260 | 96.238062 |
| 3 | 1.508591 | 0.997241 | -1.639269 | -2.041966 | 2 | 2 | -3.558085 | -0.574165 | -29.605238 |
| 4 | -1.209755 | -0.389969 | -0.644132 | -0.134412 | 2 | 1 | 6.137576 | 6.339628 | 79.985602 |
[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|W1,W3,W0,W2])
d[v₀ v₁]
Estimand assumption 1, Unconfoundedness: If U→{v0,v1} and U→y then P(y|v0,v1,W1,W3,W0,W2,U) = P(y|v0,v1,W1,W3,W0,W2)
### 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|W1,W3,W0,W2])
d[v₀ v₁]
Estimand assumption 1, Unconfoundedness: If U→{v0,v1} and U→y then P(y|v0,v1,W1,W3,W0,W2,U) = P(y|v0,v1,W1,W3,W0,W2)
## Realized estimand
b: y~v0+v1+W1+W3+W0+W2+v0*X0+v0*X1+v1*X0+v1*X1
Target units: ate
## Estimate
Mean value: 22.25845726793183
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|W1,W3,W0,W2])
d[v₀ v₁]
Estimand assumption 1, Unconfoundedness: If U→{v0,v1} and U→y then P(y|v0,v1,W1,W3,W0,W2,U) = P(y|v0,v1,W1,W3,W0,W2)
## Realized estimand
b: y~v0+v1+W1+W3+W0+W2+v0*X0+v0*X1+v1*X0+v1*X1
Target units:
## Estimate
Mean value: 22.25845726793183
### Conditional Estimates
__categorical__X0 __categorical__X1
(-3.306, -0.28] (-3.65, -0.846] -14.213706
(-0.846, -0.269] 5.928376
(-0.269, 0.25] 18.385412
(0.25, 0.853] 31.043649
(0.853, 4.86] 51.529347
(-0.28, 0.302] (-3.65, -0.846] -13.082238
(-0.846, -0.269] 8.109030
(-0.269, 0.25] 20.594350
(0.25, 0.853] 33.728952
(0.853, 4.86] 53.943140
(0.302, 0.802] (-3.65, -0.846] -10.653578
(-0.846, -0.269] 9.353780
(-0.269, 0.25] 22.335409
(0.25, 0.853] 34.773520
(0.853, 4.86] 56.268985
(0.802, 1.379] (-3.65, -0.846] -9.280687
(-0.846, -0.269] 10.641964
(-0.269, 0.25] 23.494075
(0.25, 0.853] 36.585261
(0.853, 4.86] 56.801042
(1.379, 4.106] (-3.65, -0.846] -8.579829
(-0.846, -0.269] 13.522220
(-0.269, 0.25] 26.088410
(0.25, 0.853] 38.839871
(0.853, 4.86] 60.410755
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.