Visualising probabilistic graphical models
Also related models, such as Neural nets
March 29, 2018 — February 2, 2023
The diagrams I most often need are directed flow graphs, a formal mathematical cousin of the flowchart, which 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
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 additionally calculates adjustment sets and other useful functions of the graph.
2 TikZ
LaTeX-friendly diagramming tool TikZ has a graphical model library, jluttine/tikz-bayesnet, as “TikZ library for drawing Bayesian networks, graphical models and (directed) factor graphs in LaTeX”.
3 diagrammeR
DiagrammerR is a generic graph visualization app for R which can incidentally do 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 is a generic diagramming tool but has lots of affordances for the specific needs of graphical models.
- desktop version
- integrated into jupyter
- integrated into VS Code
5 Mermaid
Another browser option is mermaid is a flowcharting tool which can be pressed into service for DAGs. Unique Value Proposition: code-driven diagrams with a syntax that aspires to be so basic that it is easier than point and click. Integrates with many markdown editors. Has an online editor, and a CLI.
It also has, if anything, too many VS Code integrations. Here are two.
6 Graphviz
The graphviz family is nearly good, in that it supports graphing various networks, including probabilistic DAGs. Inevitably, none of its fancy algorithms ever lay it out quite like I want. There is a macOS gui and a cross-platform (WX) gui called doteditor.
- numpyro automatically renders graphviz graphical model diagrams via the
render_model
method. - So does pyro
- There is a python graphviz wrapper which has possibly the best documentation for graphviz generally.
- fastdot.core wraps pygraphviz in a slightly nicer API.
- dot2tex is A Graphviz to LaTeX converter which supports mathematical markup in the nodes
- Aaaaaand graphviz renders in jupyter. (see also other jupyter options)
- And it runs in R via diagrammeR
- runs in javascript as viz.js.
- and traditional style using Rgraphviz
For some purposes, I would recommend dagitty over this; the syntax is similar, but dagitty additionally accepts specification by a structural equation model.
7 Daggity
daggity (Textor et al. 2017) is an option for the browser and also r. I’m not 100% sure how these two parts relate to each other. Documentation for the R version was hard to find—see here.
ggdag extends daggity
to the ggplot
ecosystem. The ggdag bias structure vignette shows off the useful explanation diagrams available in ggdag
and is also a good introduction to selection bias and causal DAGs themselves
Shinydag provides a web interface to ggdag. Shinydag is tedious to install natively, especially with the poor documentation. It runs ok in docker.
Now Shinydag is waiting for you at 127.0.0.1:3838/shinyDAG
. It is a bit crashy and clunky. I’m not sure I prefer it to plain ggdag.
8 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.
9 Matplotlib
HOWTO diagramming convnets using matplotlib in python Or: 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).
It’s OK at programmatic DAG rendering; it would be nice if it had some kind of semi-automatic layout algorithm.
10 yEd
yEd is a low-key nerdview 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 talks through 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.
11 Bokeh
12 Netron
visualizes various NNs from various frameworks.
AFAICT the style here is useful for diagnostics but does not seem to match the more manicured style that I would use for a publication. Maybe I should look deeper.
13 flowchart.fun
flowchart.fun (source) is a flowcharting tool which can be pressed into service for DAGs. Documentation and project goals are sparse.
14 TETRAD
TETRAD, the graph inference software, will also graph structural equation models.
15 PlotNeuralNet
Seems to do what it says on the tin, especially if you want to make convnets look pretty: HarisIqbal88/PlotNeuralNet: Latex code for making neural networks diagrams.
16 tikz
Laura Dietz and Jaakko Luttinen made tikz macros for drawing Bayes nets in LaTeX, tikz-bayesnet.