Skip to content
Framework

Publishing

dagitty()

DAGitty model text for a networkx graph.

::

import networkx as nx

Usage

dagitty(
    graph: Any,
    *,
    pos: Mapping[Any, Any] | None = None,
    y: str | None = None,
) -> str

Arguments

Name Type Default Description
graph required Any

A networkx.DiGraph, or a subclass such as pgmpy's DAG.

pos Mapping[Any, Any] | None None

Positions by node, such as {"age": (0, 0), "smoking": (1, 1)} or a networkx layout. They win over pos node attributes.

y str | None None

Which way y points in the positions: "up", as networkx and matplotlib draw, or "down", as DAGitty reads. Default: "up".

Value

The DAGitty model text.

Details

g = nx.DiGraph() g.add_node("smoking", role="exposure", label="Smoking status") g.add_node("cancer", role="outcome") g.add_edges_from([("age", "smoking"), ("age", "cancer"), ("smoking", "cancer")]) print(fw.dagitty(g))

The text opens with dag {, lists every node with its roles and position, then every edge. R's dagitty and ggdag and dagitty.net read it. A name that isn't a plain identifier, such as parental SES, is double-quoted.

The graph's node and edge attributes say what DAGitty needs:

  • role: "exposure", "outcome", "latent", "adjusted", or "selected", or a list of them. A pgmpy DAG's latents are latent too.
  • pos: an (x, y) position, as nx.draw(G, pos) takes it: y points up. DAGitty's y points down, so y is negated in the text, and the drawing on Framework Cloud matches nx.draw(). y="down" writes positions as they are.
  • label: the name to show. dagitty() leaves labels out; publish_dag() sends them.
  • bidirected=True on an edge: a <-> b, a latent common cause. The edge's direction doesn't matter, and it isn't part of the cycle check.

Raises

  • FrameworkError: The graph is undirected or has a directed cycle, a node points to itself, or a role or position isn't valid.

Search guides and reference for R, Python, Stata, the CLI, and Framework Cloud.