Beispiel #1
0
def dowhy_quick_backdoor_estimator(dataframe,
                                   outcome,
                                   treatment,
                                   cofounders_list,
                                   method_name,
                                   populaton_of_interest='ate',
                                   view_model=False):
    """
    Make a quick statistical assessment for the mean of 2 different samples (hypothesis test based)
    :param dataframe: original dataframe in a subject level
    :param group_col: the name of the group column
    :param category_col: the name of the category_col column
    :returns group_share_per_category_df: df containing the % share each category has by group
    """
    causal_model = CausalModel(data=dataframe,
                               treatment=treatment,
                               outcome=outcome,
                               common_causes=cofounders_list)
    if view_model:
        causal_model.view_model(layout="dot")
    identified_estimand = causal_model.identify_effect(
        proceed_when_unidentifiable=True)
    causal_estimate = causal_model.estimate_effect(
        identified_estimand,
        method_name=method_name,
        target_units=
        populaton_of_interest  #, confidence_intervals=True # not in this release
    )
    return causal_estimate.value
Beispiel #2
0
def att_causal_estimator(df,
                         outcome,
                         treatment,
                         cofounders_list,
                         method_name,
                         view_model=False):
    causal_model = CausalModel(data=df,
                               treatment=treatment,
                               outcome=outcome,
                               common_causes=cofounders_list)
    if view_model:
        causal_model.view_model(layout="dot")
    identified_estimand = causal_model.identify_effect(
        proceed_when_unidentifiable=True)
    causal_estimate = causal_model.estimate_effect(
        identified_estimand,
        method_name=method_name,
        target_units='att',  #, confidence_intervals=True
    )
    return (causal_estimate.value)
Beispiel #3
0
from datasets import *
from dowhy import CausalModel

credit_data = get_credit()

model = CausalModel(
    data=credit_data["df"],
    treatment=["YearsEmployed"],
    outcome=["Approved"],
    graph=credit_data["dot_graph"],
)

from sklearn.linear_model import LogisticRegressionCV

# Saves the model as "causal_model.png"
model.view_model(layout="dot")
identified_estimand_binary = model.identify_effect(
    proceed_when_unidentifiable=True)
# estimate = model.estimate_effect(identified_estimand, method_name="backdoor.econml.drlearner.LinearDRLearner")

orthoforest_estimate = model.estimate_effect(
    identified_estimand_binary,
    method_name="backdoor.econml.ortho_forest.ContinuousTreatmentOrthoForest",
    target_units=lambda df: df["Male"] == 1,
    confidence_intervals=False,
    method_params={
        "init_params": {
            'n_trees':
            2,  # not ideal, just as an example to speed up computation
        },
        "fit_params": {}
data = data.astype({"treatment": 'bool'}, copy=False)
print(data.head())

# Create a causal model from the data and given common causes.
xs = ""
for i in range(1, 26):
    xs += ("x" + str(i) + "+")

model = CausalModel(
    data=data,
    treatment='treatment',
    outcome='y_factual',
    common_causes=xs.split('+'),
)
#save the model as a png
model.view_model()
display(Image(filename="causal_model.png"))

#Identify the causal effect
identified_estimand = model.identify_effect()
print(identified_estimand)

# Estimate the causal effect and compare it with Average Treatment Effect
estimate = model.estimate_effect(identified_estimand,
                                 method_name="backdoor.linear_regression",
                                 test_significance=True)

print(estimate)

print("Causal Estimate is " + str(estimate.value))
data_1 = data[data["treatment"] == 1]