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.407332 | -0.056173 | 0.524682 | 0.735251 | 0 | 0 | 4.864141 | 5.273716 | 79.835212 |
| 1 | -0.581832 | -1.617002 | 0.804786 | -0.065007 | 0 | 1 | 5.247939 | 5.010679 | -61.788236 |
| 2 | -0.958478 | -0.847614 | 2.261130 | 0.798190 | 1 | 3 | 20.445784 | 24.276125 | -1361.010758 |
| 3 | -1.818551 | -0.238508 | -1.883856 | 0.881209 | 1 | 3 | 6.979215 | 2.579859 | 73.519319 |
| 4 | -0.695539 | -1.463729 | 0.008912 | 0.891629 | 3 | 2 | 18.415539 | 22.080640 | -1976.234657 |
[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|W3,W1,W0,W2])
d[v₀ v₁]
Estimand assumption 1, Unconfoundedness: If U→{v0,v1} and U→y then P(y|v0,v1,W3,W1,W0,W2,U) = P(y|v0,v1,W3,W1,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|W3,W1,W0,W2])
d[v₀ v₁]
Estimand assumption 1, Unconfoundedness: If U→{v0,v1} and U→y then P(y|v0,v1,W3,W1,W0,W2,U) = P(y|v0,v1,W3,W1,W0,W2)
## Realized estimand
b: y~v0+v1+W3+W1+W0+W2+v0*X0+v0*X1+v1*X0+v1*X1
Target units: ate
## Estimate
Mean value: -44.172959204905396
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|W3,W1,W0,W2])
d[v₀ v₁]
Estimand assumption 1, Unconfoundedness: If U→{v0,v1} and U→y then P(y|v0,v1,W3,W1,W0,W2,U) = P(y|v0,v1,W3,W1,W0,W2)
## Realized estimand
b: y~v0+v1+W3+W1+W0+W2+v0*X0+v0*X1+v1*X0+v1*X1
Target units:
## Estimate
Mean value: -44.172959204905396
### Conditional Estimates
__categorical__X0 __categorical__X1
(-4.6450000000000005, -1.569] (-4.336, -1.407] -156.138500
(-1.407, -0.825] -93.005413
(-0.825, -0.331] -55.673786
(-0.331, 0.258] -19.729158
(0.258, 3.309] 43.037638
(-1.569, -0.978] (-4.336, -1.407] -144.685377
(-1.407, -0.825] -86.368464
(-0.825, -0.331] -48.578420
(-0.331, 0.258] -11.070802
(0.258, 3.309] 48.415747
(-0.978, -0.468] (-4.336, -1.407] -143.716956
(-1.407, -0.825] -80.413600
(-0.825, -0.331] -43.731527
(-0.331, 0.258] -7.003184
(0.258, 3.309] 53.984016
(-0.468, 0.11] (-4.336, -1.407] -136.701641
(-1.407, -0.825] -75.036007
(-0.825, -0.331] -39.548946
(-0.331, 0.258] -1.861913
(0.258, 3.309] 53.733867
(0.11, 3.119] (-4.336, -1.407] -129.153329
(-1.407, -0.825] -67.579358
(-0.825, -0.331] -31.259031
(-0.331, 0.258] 3.842395
(0.258, 3.309] 63.560552
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.