<jupyter_start><jupyter_text>Starting with dowhy for Causality inference analysis<jupyter_code>import sys
import os
sys.path.append(os.path.abspath(os.path.join('..')))
import pandas as pd
import re
# from wordcloud import STOPWORDS,WordCloud
import matplotlib.pyplot as plt

CSV_PATH = "../data/Feature_selection_with_correlation.csv"
def read_proccessed_data(csv_path):
    try:    
        df = pd.read_csv(csv_path)
        print("file read as csv")
        return df
    except FileNotFoundError:
        print("file not found")
df = read_proccessed_data(CSV_PATH)
df.head()
from dowhy import CausalModel
import dowhy.datasets as ds
d = ds.linear_dataset(beta=5,
    num_common_causes=5,
    num_instruments = 0,
    num_samples=10000,
    treatment_is_binary=True)
d["dot_graph"] = 'digraph { U[label="Unobserved Confounders"];U->Diagnosis;area_mean->Diagnosis;concavity_mean->Diagnosis;area_se->Diagnosis;symmetry_worst->Diagnosis;concavity_worst->Diagnosis;symmetry_se->Diagnosis;concavity_se->Diagnosis;smoothness_se->Diagnosis;fractal_dimension_mean->Diagnosis;smoothness_mean->Diagnosis;symmetry_se->Diagnosis;fractal_dimension_se->Diagnosis;smoothness_worst->Diagnosis;fractal_dimension_worst->Diagnosis;}'
model = CausalModel(
    data = df,
    treatment = ["Diagnosis"],
    outcome = "Diagnosis",
    graph = d["dot_graph"]
)
model.view_model(layout = "dot_graph")
from IPython.display import Image, display
display(Image(filename = "causal_model.png"))
identified_estimand = model.identify_effect()
print(identified_estimand)
df["Diagnosis"] = ''
estimate = model.estimate_effect(identified_estimand,
                                 method_name="backdoor.propensity_score_matching")
d["dot_graph"] = 'digraph { U[label="Unobserved Confounders"];U->Diagnosis;area_mean->Diagnosis;concavity_mean->Diagnosis;area_se->Diagnosis;symmetry_worst->Diagnosis;concavity_worst->Diagnosis;}'
model = CausalModel(
    data = df,
    treatment = ["Diagnosis"],
    outcome = "Diagnosis",
    graph = d["dot_graph"]
)
model.view_model(layout = "dot_graph")
from IPython.display import Image, display
display(Image(filename = "causal_model.png"))
dir(model)
identified_estimand = model.identify_effect()
print(identified_estimand)
estimate = model.estimate_effect(identified_estimand,
                                 method_name="backdoor.propensity_score_matching")
identified_estimand = model.identify_effect()
print(identified_estimand)
estimate = model.estimate_effect(identified_estimand,method_name = "backdoor.linear_regression")
d["treatment_name"],
d["outcome_name"],
d["gml_graph"]
ds.linear_dataset(beta=5,
    num_common_causes=5,
    num_instruments = 0,
    num_samples=10000,
    treatment_is_binary=True)
d["common_causes_names"] = ['area_mean', 'concavity_mean', 'area_se', 'concavity_worst',
       'symmetry_worst']
data = dowhy.datasets.linear_dataset(
    beta=10,
    num_common_causes=5,
    num_instruments=2,
    num_samples=10000,
    treatment_is_binary=True)
# I. Create a causal model from the data and given graph.
model = CausalModel(
    data=data["df"],
    treatment=data["treatment_name"],
    outcome=data["outcome_name"],
    graph=data["gml_graph"])

# II. Identify causal effect and return target estimands
identified_estimand = model.identify_effect()

# III. Estimate the target estimand using a statistical method.
estimate = model.estimate_effect(identified_estimand,
                                 method_name="backdoor.propensity_score_matching")

# IV. Refute the obtained estimate using multiple robustness checks.
refute_results = model.refute_estimate(identified_estimand, estimate,
                                       method_name="random_common_cause")<jupyter_output>WARN: Do you want to continue by ignoring any unobserved confounders? (use proceed_when_unidentifiable=True to disable this prompt) [y/n] y
{'fraction_non_alphanumeric': 0.10840879689521346, 'fraction_numerical': 0.00646830530401035, 'mean_word_length': 5.165869218500798, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '121915', 'license': 'MIT', 'n_tokens_mistral': 1318, 'n_tokens_neox': 1241, 'n_words': 219, 'path': 'notebooks/causalInference.ipynb', 'repo_name': 'Bina-man/Breast_Cancer_Causality_Inference', 'size': '3865'}