Skip to main content

Graph Classes

Source: vignettes/articles/graph-classes.qmd

This guide explains how to build a causal graph and what each graph class means. For a full introduction to these graphs and the theory behind them, see, for instance, Pearl (2009) or Peters et al. (2017); Richardson and Spirtes (2002) cover ancestral graphs (AGs and MAGs) specifically, and Zhang (2008) covers PAGs.

Constructing graphs

cgraph() builds a graph from edges written with infix operators, and its class argument ("DAG" by default) sets the graph class:

dag <- cgraph(A %-->% X + Y, X %-->% M %-->% Y)
admg <- cgraph(X %-->% Y, X %<->% Y, class = "ADMG")

Each operator is an edge with a mark at either end: a tail (-), an arrowhead (< or >) or a circle (o).

Operator Edge
%-->% directed, src --> dst
%<--% directed, dst --> src
%---% undirected
%<->% bidirected
%o->% partially directed, src o-> dst
%<-o% partially directed, dst o-> src
%o--% partially undirected, src o-- dst
%--o% partially undirected, dst o-- src
%o-o% partial (circles at both ends)

+ fans an edge out to (or in from) several nodes at once, and chaining operators connects consecutive groups of nodes, so A %-->% B %-->% C gives the two edges A --> B and B --> C, while A %-->% B + C gives A --> B and A --> C. A bare node name declares an isolated node:

nodes(cgraph(A %-->% B, F))
#> [1] "A" "B" "F"

To build a graph from a string made at run time, pass the same edges as a string through spec, with the operators written without %:

spec <- paste("A -->", paste0("X", 1:3, collapse = " + "))
edges(cgraph(spec = spec))
#>   from to type
#> 1    A X1  -->
#> 2    A X2  -->
#> 3    A X3  -->

Graph classes

Each class is checked on construction, so a graph of a class is always valid for it. Building a DAG with a cycle, or with a bidirected edge, is an error:

cgraph(A %-->% B %-->% C %-->% A)
#> Error:
#> ! Invalid DAG:
#>   - directed cycles are not allowed in DAG
cgraph(A %<->% B)
#> Error:
#> ! Invalid DAG:
#>   - invalid edge type for graph class DAG

reclass() changes the class of an existing graph, again checking the result, and the predicates (is_dag(), is_cpdag(), is_mag(), …) test whether a graph satisfies a class’s constraints, independent of its own graph class.

Directed acyclic graphs

A DAG (directed acyclic graph) is the standard causal graph. All edges are directed (-->), and the graph contains no directed cycles. An edge X --> Y means X is a direct cause of Y.

Partially directed acyclic graphs

The PDAG family ("PDAG", "CPDAG", "MPDAG") has directed (-->) and undirected (---) edges, and no directed cycles. An undirected edge is one whose orientation is not specified.

  • A PDAG is the general case.
  • A CPDAG represents a whole Markov equivalence class of DAGs: an edge stays undirected exactly when its orientation varies across the class. It is what constraint-based causal discovery algorithms such as PC estimate from observational data, and dag_to_cpdag() computes it from a DAG.
  • An MPDAG is a CPDAG with background knowledge added, which may orient edges the equivalence class alone leaves undetermined.
plot(dag_to_cpdag(dag))

See Equivalence Classes for worked examples of all three.

Acyclic directed mixed graphs

An ADMG (acyclic directed mixed graph) has directed (-->) and bidirected (<->) edges. Directed edges are causal relations, while a bidirected edge is unobserved confounding between its endpoints. An ADMG is, for example, what latent_project() gives when it projects latent variables out of a DAG:

full <- cgraph(U %-->% X + Y, X %-->% M %-->% Y)
plot(latent_project(full, "U"))

Ancestral graphs

Ancestral graphs ("AG" and its maximal subclass "MAG") have directed (-->), bidirected (<->) and undirected (---) edges. In an ancestral graph, an arrowhead at a node means that node is not an ancestor of the other endpoint. A bidirected edge thus means neither endpoint is an ancestor of the other; in causal applications such edges commonly represent unobserved confounding.

Undirected edges mean something different here than in a PDAG: rather than an unknown orientation, they represent selection bias, associations arising from conditioning on variables that are common effects. A MAG is the ancestral graph of a DAG with latent and selection variables marginalized and conditioned out, and is maximal: every pair of non-adjacent nodes can be separated.

Partial ancestral graphs

A PAG plays the role for MAGs that a CPDAG plays for DAGs: it represents a Markov equivalence class of MAGs (Zhang 2008), and is what the FCI algorithm estimates. PAG edges can have three endpoint marks: a tail, an arrowhead or a circle, and a circle means that mark is not the same in every MAG in the class. mag_to_pag() computes the PAG of a MAG:

mag <- cgraph(A %-->% B, C %-->% B, B %-->% D, class = "MAG")
plot(mag_to_pag(mag))

See PAG Causal Effects for estimating causal effects from a PAG.

Undirected graphs

A UG ("UG") has only undirected (---) edges. Undirected graphs are not common in causal inference, but are available for those who need them; for example, moralize() returns one.

Unknown graphs

"UNKNOWN" imposes no structural constraints. It accepts every edge type, including self-loops and parallel edges, for graphs that do not fit any other class:

edges(cgraph(A %-->% B, A %<->% B, B %-->% B, class = "UNKNOWN"))
#>   from to type
#> 1    A  B  -->
#> 2    A  B  <->
#> 3    B  B  -->

Background knowledge

Sometimes you know some causal relations in advance: A must cause B, or C does not cause D. apply_background_knowledge() adds such knowledge to a PDAG, required edges as %-->% (or %<--%) and forbidden ones as %!-->% (or %!<--%), and propagates the orientations it implies, giving an MPDAG:

cpdag <- cgraph(A %---% B %---% C, class = "CPDAG")
plot(apply_background_knowledge(cpdag, A %-->% B))

dag_to_mpdag() does the same starting from a DAG, using its CPDAG.

References

Pearl, Judea. 2009. Causality: Models, Reasoning, and Inference. 2nd ed. Cambridge University Press.
Peters, Jonas Martin, Dominik Janzing, and Bernhard Schölkopf. 2017. Elements of Causal Inference: Foundations and Learning Algorithms: Foundations and Learning Algorithms. Adaptive Computation and Machine Learning Series. MIT Press.
Richardson, Thomas, and Peter Spirtes. 2002. “Ancestral Graph Markov Models.” Annals of Statistics 30 (4): 962–1030.
Zhang, Jiji. 2008. “On the Completeness of Orientation Rules for Causal Discovery in the Presence of Latent Confounders and Selection Bias.” Artificial Intelligence 172 (16-17): 1873–96.