py-why / dowhy

DoWhy is a Python library for causal inference that supports explicit modeling and testing of causal assumptions. DoWhy is based on a unified language for causal inference, combining causal graphical models and potential outcomes frameworks.
https://www.pywhy.org/dowhy
MIT License
7.01k stars 922 forks source link

Problem with refute_graph #940

Closed juandavidgutier closed 1 year ago

juandavidgutier commented 1 year ago

I am testing my dataset for conditional independence, but I get the Error message: "AttributeError: 'CausalModel' object has no attribute 'refute_graph'" using DoWhy version 0.6.

I will appreciate a lot your cooperation.

Here is my dataset: dataset_question.csv

And here is my code:

` import numpy as np import pandas as pd import dowhy from dowhy import CausalModel import scipy.stats as stats

load data

dataset = pd.read_csv("D:/dataset_question.csv")

sd units

dataset.soi = stats.zscore(dataset.soi, nan_policy='omit') dataset.Esoi = stats.zscore(dataset.Esoi, nan_policy='omit') dataset.soi = stats.zscore(dataset.soi, nan_policy='omit') dataset.SST3 = stats.zscore(dataset.SST3, nan_policy='omit') dataset.SST4 = stats.zscore(dataset.SST4, nan_policy='omit') dataset.SST34 = stats.zscore(dataset.SST34, nan_policy='omit') dataset.SST12 = stats.zscore(dataset.SST12, nan_policy='omit') dataset.NATL = stats.zscore(dataset.NATL, nan_policy='omit') dataset.SATL = stats.zscore(dataset.SATL, nan_policy='omit') dataset.TROP = stats.zscore(dataset.TROP, nan_policy='omit') dataset.forest = stats.zscore(dataset.forest, nan_policy='omit') dataset.Temp = stats.zscore(dataset.Temp, nan_policy='omit') dataset.Rain = stats.zscore(dataset.Rain, nan_policy='omit') dataset.Qs = stats.zscore(dataset.Qs, nan_policy='omit') dataset.STMP = stats.zscore(dataset.STMP, nan_policy='omit') dataset.Smoi = stats.zscore(dataset.Smoi, nan_policy='omit') dataset.EVI = stats.zscore(dataset.EVI, nan_policy='omit')

DAG

graph = """graph[directed 1 node[id "Treat" label "Treat"] node[id "excess" label "excess"] node[id "soi" label "soi"] node[id "Esoi" label "Esoi"] node[id "SST3" label "SST3"]
node[id "SST4" label "SST4"] node[id "SST34" label "SST34"] node[id "SST12" label "SST12"] node[id "NATL" label "NATL"] node[id "SATL" label "SATL"] node[id "TROP" label "TROP"]
node[id "Temp" label "Temp"] node[id "Rain" label "Rain"] node[id "Qs" label "Qs"] node[id "STMP" label "STMP"] node[id "Smoi" label "Smoi"] node[id "EVI" label "EVI"] node[id "forest" label "forest"] node[id "NBI" label "NBI"]

        edge[source "SST12" target "SST3"]
        edge[source "SST12" target "SST34"]
        edge[source "SST12" target "SST4"]
        edge[source "SST12" target "soi"]
        edge[source "SST12" target "Esoi"]
        edge[source "SST12" target "NATL"]
        edge[source "SST12" target "SATL"]
        edge[source "SST12" target "TROP"]
        edge[source "SST3" target "SST34"]
        edge[source "SST3" target "SST4"]
        edge[source "SST3" target "soi"]
        edge[source "SST3" target "Esoi"]
        edge[source "SST3" target "NATL"]
        edge[source "SST3" target "SATL"]
        edge[source "SST3" target "TROP"]
        edge[source "SST34" target "SST4"]
        edge[source "SST34" target "soi"]
        edge[source "SST34" target "Esoi"]
        edge[source "SST34" target "NATL"]
        edge[source "SST34" target "SATL"]
        edge[source "SST34" target "TROP"]
        edge[source "SST4" target "soi"]
        edge[source "SST4" target "Esoi"]
        edge[source "SST4" target "NATL"]
        edge[source "SST4" target "SATL"]
        edge[source "SST4" target "TROP"]
        edge[source "soi" target "Esoi"]
        edge[source "soi" target "NATL"]
        edge[source "soi" target "SATL"]
        edge[source "soi" target "TROP"]
        edge[source "Esoi" target "NATL"]
        edge[source "Esoi" target "SATL"]
        edge[source "Esoi" target "TROP"]
        edge[source "NATL" target "SATL"]
        edge[source "NATL" target "TROP"]
        edge[source "SATL" target "TROP"]

        edge[source "SST12" target "Treat"]
        edge[source "SST3" target "Treat"]
        edge[source "SST34" target "Treat"]
        edge[source "SST4" target "Treat"]
        edge[source "soi" target "Treat"]
        edge[source "Esoi" target "Treat"]
        edge[source "NATL" target "Treat"]
        edge[source "SATL" target "Treat"]
        edge[source "TROP" target "Treat"]
        edge[source "SST12" target "excess"]
        edge[source "SST3" target "excess"]
        edge[source "SST34" target "excess"]
        edge[source "SST4" target "excess"]
        edge[source "soi" target "excess"]
        edge[source "Esoi" target "excess"]
        edge[source "NATL" target "excess"]
        edge[source "SATL" target "excess"]
        edge[source "TROP" target "excess"]

        edge[source "Temp" target "excess"]
        edge[source "Rain" target "excess"]
        edge[source "Qs" target "excess"]
        edge[source "STMP" target "excess"]
        edge[source "Smoi" target "excess"]
        edge[source "EVI" target "excess"]

        edge[source "forest" target "excess"]
        edge[source "forest" target "Temp"]
        edge[source "forest" target "Rain"]
        edge[source "forest" target "Qs"]
        edge[source "forest" target "STMP"]
        edge[source "forest" target "Smoi"]
        edge[source "forest" target "EVI"]
        edge[source "forest" target "NBI"]

        edge[source "Temp" target "NBI"]
        edge[source "Rain" target "NBI"]
        edge[source "Qs" target "NBI"]
        edge[source "STMP" target "NBI"]
        edge[source "Smoi" target "NBI"]
        edge[source "EVI" target "NBI"]

        edge[source "NBI" target "excess"]

        edge[source "SST12" target "Temp"]
        edge[source "SST3" target "Temp"]
        edge[source "SST34" target "Temp"]
        edge[source "SST4" target "Temp"]
        edge[source "soi" target "Temp"]
        edge[source "Esoi" target "Temp"]
        edge[source "NATL" target "Temp"]
        edge[source "SATL" target "Temp"]
        edge[source "TROP" target "Temp"]

        edge[source "SST12" target "Rain"]
        edge[source "SST3" target "Rain"]
        edge[source "SST34" target "Rain"]
        edge[source "SST4" target "Rain"]
        edge[source "soi" target "Rain"]
        edge[source "Esoi" target "Rain"]
        edge[source "NATL" target "Rain"]
        edge[source "SATL" target "Rain"]
        edge[source "TROP" target "Rain"]

        edge[source "SST12" target "Qs"]
        edge[source "SST3" target "Qs"]
        edge[source "SST34" target "Qs"]
        edge[source "SST4" target "Qs"]
        edge[source "soi" target "Qs"]
        edge[source "Esoi" target "Qs"]
        edge[source "NATL" target "Qs"]
        edge[source "SATL" target "Qs"]
        edge[source "TROP" target "Qs"]

        edge[source "SST12" target "STMP"]
        edge[source "SST3" target "STMP"]
        edge[source "SST34" target "STMP"]
        edge[source "SST4" target "STMP"]
        edge[source "soi" target "STMP"]
        edge[source "Esoi" target "STMP"]
        edge[source "NATL" target "STMP"]
        edge[source "SATL" target "STMP"]
        edge[source "TROP" target "STMP"]

        edge[source "SST12" target "Smoi"]
        edge[source "SST3" target "Smoi"]
        edge[source "SST34" target "Smoi"]
        edge[source "SST4" target "Smoi"]
        edge[source "soi" target "Smoi"]
        edge[source "Esoi" target "Smoi"]
        edge[source "NATL" target "Smoi"]
        edge[source "SATL" target "Smoi"]
        edge[source "TROP" target "Smoi"]

        edge[source "SST12" target "EVI"]
        edge[source "SST3" target "EVI"]
        edge[source "SST34" target "EVI"]
        edge[source "SST4" target "EVI"]
        edge[source "soi" target "EVI"]
        edge[source "Esoi" target "EVI"]
        edge[source "NATL" target "EVI"]
        edge[source "SATL" target "EVI"]
        edge[source "TROP" target "EVI"]

        edge[source "Treat" target "excess"]]"""

model

model = CausalModel( data=dataset, treatment=["Treat"], outcome=["excess"], graph=graph, )

model.view_model()

test conditional independences (HERE IS THE ERROR)

refuter_object = model.refute_graph(k=17, independence_test='partial_correlation', independence_constraints=None)

print(refuter_object)

`

amnesiacandres commented 1 year ago

@juandavidgutier , seems the refute_graph method was added in v0.8 and is not available at all in v0.6. https://github.com/py-why/dowhy/blob/3767f3146bcb862ad0627730abfe17c5668f8ccd/dowhy/causal_model.py#L468

juandavidgutier commented 1 year ago

@amnesiacandres, Thanks a lot for your answer.

github-actions[bot] commented 1 year ago

This issue is stale because it has been open for 14 days with no activity.

github-actions[bot] commented 1 year ago

This issue was closed because it has been inactive for 7 days since being marked as stale.