#!/usr/bin/env python # coding: utf-8 # # Getting started with DoWhy: A simple example # This is a quick introduction to the DoWhy causal inference library. # We will load in a sample dataset and estimate the causal effect of a (pre-specified)treatment variable on a (pre-specified) outcome variable. # # First, let us add the required path for Python to find the DoWhy code and load all required packages. # In[1]: import os, sys sys.path.append(os.path.abspath("../../")) # Let's check the python version. # In[2]: print(sys.version) # In[3]: import numpy as np import pandas as pd import dowhy from dowhy.do_why import CausalModel import dowhy.datasets # Now, let us load a dataset. For simplicity, we simulate a dataset with linear relationships between common causes and treatment, and common causes and outcome. # # Beta is the true causal effect. # In[4]: data = dowhy.datasets.linear_dataset(beta=10, num_common_causes=5, num_instruments = 2, num_samples=10000, treatment_is_binary=True) df = data["df"] print(df.head()) print(data["dot_graph"]) print("\n") print(data["gml_graph"]) # Note that we are using a pandas dataframe to load the data. At present, DoWhy only supports pandas dataframe as input. # ## Interface 1 (recommended): Input causal graph # We now input a causal graph in the GML graph format (recommended). You can also use the DOT format. # In[5]: # With graph model=CausalModel( data = df, treatment=data["treatment_name"], outcome=data["outcome_name"], graph=data["gml_graph"] ) # In[6]: model.view_model() # In[7]: from IPython.display import Image, display display(Image(filename="causal_model.png")) # The above causal graph shows the assumptions encoded in the causal model. We can now use this graph to first identify # the causal effect (go from a causal estimand to a probability expression), and then estimate the causal effect. # **DoWhy philosophy: Keep identification and estimation separate** # # Identification can be achieved without access to the data, acccesing only the graph. This results in an expression to be computed. This expression can then be evaluated using the available data in the estimation step. # It is important to understand that these are orthogonal steps. # # * Identification # In[8]: identified_estimand = model.identify_effect() print(identified_estimand) # If you want to disable the warning for ignoring unobserved confounders, you can add a parameter flag ( *proceed\_when\_unidentifiable* ). The same parameter can also be added when instantiating the CausalModel object. # In[9]: identified_estimand = model.identify_effect(proceed_when_unidentifiable=True) print(identified_estimand) # * Estimation # In[10]: causal_estimate = model.estimate_effect(identified_estimand, method_name="backdoor.linear_regression") print(causal_estimate) print("Causal Estimate is " + str(causal_estimate.value)) # ## Interface 2: Specify common causes and instruments # In[11]: # Without graph model= CausalModel( data=df, treatment=data["treatment_name"], outcome=data["outcome_name"], common_causes=data["common_causes_names"]) # In[12]: model.view_model() # # We get the same causal graph. Now identification and estimation is done as before. # In[13]: identified_estimand = model.identify_effect() # * Estimation # In[14]: estimate = model.estimate_effect(identified_estimand, method_name="backdoor.propensity_score_matching") print(estimate) print("Causal Estimate is " + str(estimate.value)) # ## Refuting the estimate # # Let us now look at ways of refuting the estimate obtained. # ### Adding a random common cause variable # In[15]: res_random=model.refute_estimate(identified_estimand, estimate, method_name="random_common_cause") print(res_random) # ### Replacing treatment with a random (placebo) variable # In[16]: res_placebo=model.refute_estimate(identified_estimand, estimate, method_name="placebo_treatment_refuter", placebo_type="permute") print(res_placebo) # ### Removing a random subset of the data # In[17]: res_subset=model.refute_estimate(identified_estimand, estimate, method_name="data_subset_refuter", subset_fraction=0.9) print(res_subset) # As you can see, the linear regression estimator is very sensitive to simple refutations.