When you estimate a causal effect, the number you get depends heavily on your assumptions. But the real question is: What happens if those assumptions are slightly wrong?
Refutation methods in DoWhy help you answer this question by stress-testing your result. We’ll use the Lalonde dataset, a standard real-world dataset for evaluating causal inference methods.
Step 1: Loading the Dataset
import pandas as pd from dowhy import CausalModel import dowhy.datasets # Load Lalonde dataset from DoWhy data = dowhy.datasets.lalonde_dataset() # Extract the dataframe df = data["df"] # Preview the data print(df.head())
What this does:
- lalonde_dataset() returns a dictionary that includes the dataset and metadata
- df contains the actual data we’ll use
- Columns include:
- treatment: whether a person received job training
- re78: earnings after training, which is our outcome
- Other columns: age, education, past income, and other confounders
Step 2: Defining the Causal Model
# Define the causal model
model = CausalModel(
data=df,
treatment="treatment", # variable we intervene on
outcome="re78", # variable we measure
common_causes=[ # variables affecting both treatment and outcome
"age", "educ", "black", "hisp",
"married", "nodegr", "re74", "re75"
]
)
What this does:
You explicitly tell DoWhy:
- what the treatment is
- what the outcome is
- which variables may confound the relationship
This is the assumption layer, and everything depends on it.
Step 3: Identifying and Estimating the Effect
# Step 1: Identify the causal effect
identified_estimand = model.identify_effect()
# Step 2: Estimate the effect using linear regression (backdoor adjustment)
estimate = model.estimate_effect(
identified_estimand,
method_name="backdoor.linear_regression"
)
# Print the estimated causal effect
print(estimate.value)
What this does:
- identify_effect() checks whether the causal effect can be computed given your assumptions
- estimate_effect() calculates the effect using regression while controlling for confounders
- estimate.value is the final causal estimate
If this returns something like 1500, it means the training program increased earnings by about $1,500 on average.
Step 4: Refuting the Estimate
Now let’s test how stable that number is.
Method 1: Random Common Cause
# Add a random variable as a fake confounder
refutation = model.refute_estimate(
identified_estimand,
estimate,
method_name="random_common_cause"
)
print(refutation)
What this does:
- Injects a random variable into the dataset
- Recomputes the estimate
- Checks whether the result changes significantly
If your estimate shifts a lot, your model may be sensitive to missing variables.
Method 2: Placebo Treatment Test
# Replace the real treatment with random noise
refutation_placebo = model.refute_estimate(
identified_estimand,
estimate,
method_name="placebo_treatment_refuter"
)
print(refutation_placebo)
What this does:
- Replaces the actual treatment with random values
- Re-estimates the effect
If you still get a strong effect here, it’s a red flag. Your model may be picking up spurious patterns.
Interpreting the Results
You’re not trying to pass a test. You’re checking whether your conclusion breaks easily.
- Large changes → your result is fragile
- Small changes → your result is more stable
That’s all refutation is doing. It gives you a way to question your own results before someone else does.
