Haghish, E. F. (2014). Applied Statistics Using Stata.

Free Online Stata Tips & Tutorials. Data Management; Stata Graphs and Graphics; Data Analysis; Stata Programming; Advanced Statistics

Fork Star Watch
Updated on 30 March 2017

Dynamic Path Diagrams in Stata

| Installation |     | Examples |    

The diagram package provides the possibility to render and export Graphviz path diagrams within Stata. The package provides the means to produce automated path diagrams for statistical computations. The paper below, shows examples of how the diagram package can be used to produce dynamic analysis path diagrams.

Dynamic Path Diagrams


The diagram package is hosted on GitHub. The command below will install the package and its dependencies. However, exporting a graphical file will require a third-party software, called phantomJS. Read the installation guide on GitHub or the help files for more details

github install haghish/diagram

If you don't have the github command already installed, you can install it by typing the command below. Read more about the github package to see what else can you do to make use of all Stata packages hosted on GitHub.

net install github, from("https://haghish.github.io/github/")


Using the diagram package requires some knowledge of the DOT markup language, used by GraphViz to draw and style path diagrams. On the one hand, explaining this markup language is way beyond the purpose of this document. On the otherhand, you will not be able to use this package if you cannot write a path diagram in the DOT language.

I strongly recommend the DotEditor application, which allows you to draw a DOT graph using the GUI interface and see how the code is being written. Nevertheless, I write a few simple examples below to show you how you can draw a path diagram inside Stata using the diagram package. Note that in all of these examples, I specify the path to the phantomJS application, which is used for exporting the final image from Stata.

Example 1

This is a very simple example which draws a simple path diagram with 2 nodes, a and b.

diagram "digraph G {a -> b;}", magnify(2.5) export(./diagram.png)    ///
digraph G {a -> b;}

Example 2

In this example, the previous example is extended by adding labels to the nodes.

diagram `"digraph G {a -> b;a [label="Raw Data"];b [label="Cleaning data"];}"', magnify(2.5) export(./diagram.png)    ///
          phantomjs("/usr/local/bin/phantomjs") replace
digraph G {a -> b;a [label="Raw Data"];b [label="Cleaning data"];}

Example 3

You can draw edges between many nodes to build a more complex path diagram:

diagram "digraph G {a -> b;a -> c; a->d; b->e; c->e; d->e;}", magnify(2.5) export(./diagram.png)    ///
          phantomjs("/usr/local/bin/phantomjs") replace
digraph G {a -> b;a -> c; a->d;b->e;c->e;d->e;}