Estimating effect of multiple treatments
[1]:
import numpy as np
import pandas as pd
import logging
import dowhy
from dowhy import CausalModel
import dowhy.datasets
import econml
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.602969 | 1.966963 | 1.243967 | 2.240089 | 2 | 1 | 21.080652 | 30.114339 | 2821.894254 |
1 | -0.931480 | -1.374446 | 0.390370 | -0.850660 | 1 | 0 | 3.769289 | 2.199193 | -12.839981 |
2 | -0.723208 | 1.196814 | 1.845801 | 1.776400 | 2 | 1 | 22.262222 | 28.480012 | 851.861567 |
3 | -0.457406 | -0.054408 | 0.576599 | 0.100132 | 2 | 2 | 11.240550 | 22.306454 | -237.724246 |
4 | -0.337283 | 1.350149 | 1.603840 | -1.216596 | 3 | 2 | 18.035292 | 25.399685 | 1754.213578 |
[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,W1,W3,W0])
d[v₀ v₁]
Estimand assumption 1, Unconfoundedness: If U→{v0,v1} and U→y then P(y|v0,v1,W2,W1,W3,W0,U) = P(y|v0,v1,W2,W1,W3,W0)
### 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,W1,W3,W0])
d[v₀ v₁]
Estimand assumption 1, Unconfoundedness: If U→{v0,v1} and U→y then P(y|v0,v1,W2,W1,W3,W0,U) = P(y|v0,v1,W2,W1,W3,W0)
## Realized estimand
b: y~v0+v1+W2+W1+W3+W0+v0*X0+v0*X1+v1*X0+v1*X1
Target units: ate
## Estimate
Mean value: -18.07821798705483
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,W1,W3,W0])
d[v₀ v₁]
Estimand assumption 1, Unconfoundedness: If U→{v0,v1} and U→y then P(y|v0,v1,W2,W1,W3,W0,U) = P(y|v0,v1,W2,W1,W3,W0)
## Realized estimand
b: y~v0+v1+W2+W1+W3+W0+v0*X0+v0*X1+v1*X0+v1*X1
Target units: ate
## Estimate
Mean value: -18.07821798705483
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.