Visualising probabilistic graphical models

Also related models, such as Neural nets

2018-03-29 — 2024-10-02

Wherein a catalogue of tools for rendering probabilistic graphical models is presented, and export to SVG/PDF, plate‑notation and inline mathematical‑markup handling are described, with Graphviz, TikZ, Mermaid and dagitty listed.

algebra
computers are awful
dataviz
faster pussycat
graphical models
machine learning
networks
photon choreography
probability
statistics
Figure 1: A confounder

The diagrams I most often need are directed flow graphs — a formal mathematical cousin of the flowchart — that can represent graphical models and neural nets. That is, my needs are nearly flowchart-like enough that I can sketch them with a flowchart tool if need be; but they are closely integrated with the equations of a particular statistical model, so I would like to incorporate them into the same system to avoid tedious and error-prone manual sync if possible. Further, there are a couple of things I would like handled which flowchart programs are bad at, such as plate-notation and handling inline mathematical markup, and representing network topologies.

As always, I would like to export the resulting diagrams to a modern compatible vector format which means SVG, PDF. As a fallback, I would accept other formats that can be converted to the above, such as Adobe Illustrator, EPS, or xfig.

1 dagR

dagR does R

After initialising a new DAG using a command line, the researcher can evaluate what associations are introduced by adjusting for covariables. Potentially biasing paths from exposure to outcome can be identified (see eFig. 1, demonstrating harmful adjustment using an example DAG from Fleischer and Diez Roux). Functions to conveniently add or remove nodes and arcs are included, as is a function checking introduced associations and biasing paths for all possible adjustment sets […]. The graphics capabilities of R allow fairly straightforward programming of basic DAG drawing routines, while also supporting the interactive repositioning of nodes and arcs.

It also calculates adjustment sets and other useful graph functions.

2 TikZ

LaTeX-friendly diagramming tool TikZ has a graphical model library, jluttine/tikz-bayesnet, called “TikZ library for drawing Bayesian networks, graphical models and (directed) factor graphs in LaTeX”.

3 diagrammeR

Figure 2: DiagrammeR graph

DiagrammerR is a generic graph visualization app for R that can also create graphical models.

With the DiagrammeR package you can create, modify, analyze, and visualize network graph diagrams. The output can be incorporated into RMarkdown documents, integrated with Shiny web apps, converted to other graph formats, or exported as image files.

4 diagrams.net

diagrams.net (formerly draw.io) is a browser-based diagram editor. It’s a generic diagramming tool but offers lots of features for the specific needs of graphical models.

5 Mermaid

Another browser option is mermaid, a flowcharting tool that can be pressed into service for DAGs. Unique value proposition: code-driven diagrams with a syntax that aims to be so basic it’s easier than point-and-click. It integrates with many markdown editors. It has an online editor and a CLI.

It also has, if anything, too many VS Code integrations. Here are two.

6 penrose

Seems to handle DAGs somewhat.

See penrose.

7 Graphviz

The graphviz family is pretty good — it supports graphing various networks, including probabilistic DAGs. Inevitably, none of its fancy algorithms ever lay out graphs quite like I want.

Figure 3: graphviz model with the fastdot theme

For some purposes I recommend dagitty instead; the syntax is similar, and dagitty also accepts specification by a structural equation model.

8 Dagitty

dagitty (Textor et al. 2017) is an option for the browser and also for R. I’m not entirely sure how the two parts relate. Documentation for the R version was hard to find—see here.

ggdag extends daggity to the ggplot ecosystem. The ggdag bias structure vignette shows useful explanation diagrams available in ggdag and is a good introduction to selection bias and causal DAGs themselves

Shinydag provides a web interface to ggdag. Shinydag is tedious to install natively because the documentation is poor. It runs okay in docker.

docker run -d -p 3838:3838 --name shinydag gerkelab/shinydag:latest

Now Shinydag is waiting for us at 127.0.0.1:3838/shinyDAG. It’s a bit crashy and clunky. I’m not sure I prefer it to plain ggdag.

9 Cytoscape

Cytoscape

Cytoscape is an open source software platform for visualising complex networks and integrating these with any type of attribute data. A lot of Apps are available for various kinds of problem domains, including bioinformatics, social network analysis, and semantic web.

10 Matplotlib

How to diagram convnets using Matplotlib in Python: diagramming convnets using matplotlib in python Or try: daft-pgm

Daft is a Python package that uses matplotlib to render pixel-perfect probabilistic graphical models for publication in a journal or on the internet. With a short Python script and an intuitive model-building syntax you can design directed (Bayesian Networks, directed acyclic graphs) and undirected (Markov random fields) models and save them in any formats that matplotlib supports (including PDF, PNG, EPS and SVG).

We find it OK at programmatic DAG rendering; it would be nice if it had a semi-automatic layout algorithm.

11 yEd

yEd is a low-key, nerdy diagrammer.

yEd supports a wide variety of diagram types. In addition to the illustrated types, (BPMN Diagrams, Flowcharts, Family Trees, Semantic Networks, Social Networks, UML Class Diagrams) yEd also supports organization charts, mind maps, swimlane diagrams, Entity Relationship diagrams, and many more.

Found via A blog by Jonas Kristoffer Lindeløv, which walks through the pluses and minuses:

yEd is purely graphical editing which is fast to work with and great for tweaking small details. A very handy yEd feature is its intelligent snapping to alignments and equal distances when positioning objects. Actually, I don’t understand why yEd almost never makes it to the “top 10 diagram/flowchart programs” lists.

A few things I learned: To make subscripts, you have to use HTML code.… it is not possible to do double-subscripts. Also, the double-edges node is made completely manually by placing an empty ellipse above another. […] A final limitation is that arrowhead sizes cannot be changed. You can, however, zoom. Therefore, your very first decision has to be the arrowhead size. Zoom so that it is appropriate and make your graphical model.

12 Bokeh

13 Netron

Visualizes various neural networks from several frameworks.

As far as I can tell, the style is useful for diagnostics but doesn’t match the more polished look I would use for a publication. I should probably look deeper.

14 flowchart.fun

flowchart.fun (source) is a flowcharting tool that we can use for DAGs. Documentation and project goals are sparse.

15 TETRAD

TETRAD, the graph inference software, can also visualize structural equation models.

16 PlotNeuralNet

It does what it says on the tin, especially if we want to make convnets look pretty: HarisIqbal88/PlotNeuralNet: Latex code for making neural networks diagrams.

17 tikz

Laura Dietz and Jaakko Luttinen created tikz macros for drawing Bayes nets in LaTeX: tikz-bayesnet.

18 Incoming

19 References

Breitling. 2010. dagR: A Suite of R Functions for Directed Acyclic Graphs.” Epidemiology.
Creed, and Gerke. 2018. Gerkelab/Shinydag: Initial Release.”
Greenland, Pearl, and Robins. 1999. Causal Diagrams for Epidemiologic Research.” Epidemiology.
Kottwitz. 2023. LaTeX Graphics with TikZ: A practitioner’s guide to drawing 2D and 3D images, diagrams, charts, and plots.
Textor, Hardt, and Knüppel. 2011. DAGitty: A Graphical Tool for Analyzing Causal Diagrams.” Epidemiology.
Textor, van der Zander, Gilthorpe, et al. 2017. Robust Causal Inference Using Directed Acyclic Graphs: The R Package ‘Dagitty’.” International Journal of Epidemiology.