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.152782 | 2.366620 | 0.467687 | 1.930517 | 3 | 1 | 6.768407 | 23.856222 | 2186.147887 |
| 1 | -0.447605 | 3.691053 | 0.736319 | 0.975951 | 1 | 2 | 4.852149 | 13.671995 | 1260.100285 |
| 2 | -0.077897 | 1.884326 | 1.728194 | 2.309277 | 0 | 1 | 6.484334 | 16.238016 | 1147.102010 |
| 3 | -0.951562 | 0.746449 | 1.539181 | 1.504921 | 1 | 1 | 5.836967 | 18.455299 | 326.102074 |
| 4 | 0.776670 | 1.522993 | -0.614915 | -0.777967 | 3 | 1 | -0.043317 | 7.587874 | 77.776876 |
[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|W0,W2,W3,W1])
d[v₀ v₁]
Estimand assumption 1, Unconfoundedness: If U→{v0,v1} and U→y then P(y|v0,v1,W0,W2,W3,W1,U) = P(y|v0,v1,W0,W2,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|W0,W2,W3,W1])
d[v₀ v₁]
Estimand assumption 1, Unconfoundedness: If U→{v0,v1} and U→y then P(y|v0,v1,W0,W2,W3,W1,U) = P(y|v0,v1,W0,W2,W3,W1)
## Realized estimand
b: y~v0+v1+W0+W2+W3+W1+v0*X1+v0*X0+v1*X1+v1*X0
Target units: ate
## Estimate
Mean value: 87.13499671049453
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|W0,W2,W3,W1])
d[v₀ v₁]
Estimand assumption 1, Unconfoundedness: If U→{v0,v1} and U→y then P(y|v0,v1,W0,W2,W3,W1,U) = P(y|v0,v1,W0,W2,W3,W1)
## Realized estimand
b: y~v0+v1+W0+W2+W3+W1+v0*X1+v0*X0+v1*X1+v1*X0
Target units:
## Estimate
Mean value: 87.13499671049453
### Conditional Estimates
__categorical__X1 __categorical__X0
(-2.964, -0.308] (-3.5829999999999997, -0.496] -52.444657
(-0.496, 0.0792] -18.056304
(0.0792, 0.589] 0.393878
(0.589, 1.182] 22.476104
(1.182, 3.923] 54.373769
(-0.308, 0.28] (-3.5829999999999997, -0.496] 2.230588
(-0.496, 0.0792] 35.139766
(0.0792, 0.589] 55.560245
(0.589, 1.182] 74.554758
(1.182, 3.923] 107.511877
(0.28, 0.783] (-3.5829999999999997, -0.496] 32.064804
(-0.496, 0.0792] 66.958371
(0.0792, 0.589] 87.093783
(0.589, 1.182] 107.294100
(1.182, 3.923] 142.282697
(0.783, 1.357] (-3.5829999999999997, -0.496] 64.469122
(-0.496, 0.0792] 98.853483
(0.0792, 0.589] 119.070601
(0.589, 1.182] 139.416182
(1.182, 3.923] 174.524617
(1.357, 4.616] (-3.5829999999999997, -0.496] 120.268491
(-0.496, 0.0792] 150.843577
(0.0792, 0.589] 171.065307
(0.589, 1.182] 194.557160
(1.182, 3.923] 228.234836
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.