Getting Started
We will show the basic usage of this package via directed acyclic graphs (DAGs).
Constructing a graph
Let's build a DAG with a confounder A that affects both X and Y, and a mediator M between X and Y:
using CausalStructures
using CairoMakie
using Sugiyama
dag = DAG("A --> X + Y, X --> M --> Y")DAG with 4 nodes and 4 edges:
nodes: A, M, X, Y
edges:
A --> X, A --> Y, X --> M, M --> Y
All graphs you construct are validated on construction to be a valid graph according to your graph class. For a DAG this means all edges are directed edges (-->), and there are no cycles. Let's try to create a DAG with a cycle:
julia> invalid_dag = DAG("A --> B --> C --> A")
ERROR: ArgumentError: Invalid DAG:
- directed cycles are not allowed in DAGSince, a picture is worth a thousand words[1] let's plot our DAG:
plot(dag)For more plotting details and customization options, see the Plotting guide.
Testing conditional independence
A central question in causal inference is whether two variables are conditionally independent. For DAGs, this is determined using d-separation.
Are X and Y independent?
d_separated(dag, :X, :Y)falseNo, there is a directed path X --> M --> Y.
If we condition on the mediator M, does that make them independent?
d_separated(dag, :X, :Y, :M)falseStill no, the backdoor path via A: X <-- A --> Y remains open.
What if we condition on both A and M?
d_separated(dag, :X, :Y, [:A, :M])trueYes, now all paths are blocked.
Finding adjustment sets
Now suppose we want to estimate the causal effect of X on Y. The backdoor path X <-- A --> Y introduces confounding bias, so we need to block it by conditioning on a valid adjustment set.
Let's find one automatically:
adjustment_set(dag, :X, :Y)1-element Vector{Symbol}:
:AWe can also verify that a specific set is valid:
is_valid_adjustment(dag, :X, :Y, :A)trueAnd enumerate all minimal valid adjustment sets:
all_adjustment_sets(dag, :X, :Y)1-element Vector{Vector{Symbol}}:
[:A]In this case, {A} is the only minimal set that blocks the confounding.
Next steps
This small quick guide barely scratched the surface of what you can do with this package:
- For more on working with DAGs in the context of causal identification, see Causal Identification.
- To learn more about the different graph classes, see Equivalence Classes or the Graph & Edge Types reference.
- If you're already familiar with causal graphs, you might instead be interested in Plotting or Benchmarks.
- 1Especially with causal graphs