# Hierarchical statistical diagrams

I would like to create a diagram like the one below in LaTeX (probably with TikZ). This diagram and others can be found on this website.

It looks like it was made with LaTeX, but I am not entirely sure. How can I approach this problem? Does TikZ provide an easy notation or template for creating such diagrams?

• matrix of nodes from tikz? otherwise the positioning library should help.... Apr 24, 2014 at 23:49

Some of those diagrams, don't require the power of TikZ; for exmaple, using a standard array you can produce

\documentclass{article}
\usepackage{graphicx}
\usepackage{amsmath}

\newcommand\Marrowdown{\rotatebox[origin=c]{-90}{$\longrightarrow$}}

\begin{document}

$\setlength\arraycolsep{12pt} \begin{array}{lccc} \text{parameters} & \theta_{1} & \theta_{2} & \theta_{3} \\ & \Marrowdown & \Marrowdown & \Marrowdown \\ \text{observations} & y_{1} & y_{2} & y_{3} \end{array}$

\end{document}


TikZ offers a number of possible alternatives; for example, a matrix of math nodes can be used to produce all those diagrams. A simple example:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}

\begin{tikzpicture}
\matrix[matrix of math nodes,column sep=15pt,row sep=15pt] (mat)
{
\theta_{1} & \theta_{2} & \theta_{3} \\
y_{1} & y_{2} &  y_{3} \\
};
\foreach \Columna in {1,2,3}
\draw[->,>=latex] (mat-1-\Columna) -- (mat-2-\Columna);
\node[anchor=east] at ([xshift=-20pt]mat-1-1) {parameters};
\node[anchor=east] at ([xshift=-20pt]mat-2-1) {observations};

\end{tikzpicture}

\end{document}


Another possibility using chains:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,chains}

\begin{document}

\begin{tikzpicture}
\begin{scope}[
every node/.style={on chain,join},
every join/.style={draw,->}
]
\begin{scope}[start chain=1 going below]
\node {$\theta_{1}$};
\node {$y_{1}$};
\end{scope}
\begin{scope}[xshift=1cm,start chain=2 going below]
\node {$\theta_{2}$};
\node {$y_{2}$};
\end{scope}
\begin{scope}[xshift=2cm,start chain=3 going below]
\node {$\theta_{3}$};
\node {$y_{3}$};
\end{scope}
\end{scope}
\node[anchor=east] at ([xshift=-20pt]1-1) {parameters};
\node[anchor=east] at ([xshift=-20pt]1-2) {observations};
\end{tikzpicture}

\end{document}


For trees, specially if they are complex, I'd suggest you the powerful forest package (it's built upon PGF/TikZ and was designed specifically to build trees):

\documentclass{article}
\usepackage{forest}

\begin{document}

\begin{forest}
for tree={
edge={->,>=latex},
parent anchor=south,
child anchor=north,
content format={\ensuremath{\forestoption{content}}},
}
[{\mu,\sigma^{2}},name=level0
[\theta_{1},name=level1
[y_{1},name=level2]
]
[\theta_{2}
[y_{2}]
]
[\cdots,edge={draw=none}
[\cdots,edge={draw=none}]
]
[\theta_{k}
[y_{k}]
]
]
\foreach \Name/\Label in {level2/parameters,level1/observations,level0/model}
\node[anchor=east] at ([xshift=-30pt]\Name) {\Label};
\end{forest}

\end{document}


As you can see, there are several options, which one to choose depends on the complexity of the diagrams you intend to draw. What is important is to be consistent; I mean, for a single document the ideal would be to choose one tool and stick to it (to guarantee things as the same kind of arrow tips, same distance between nodes, etc.).

Just another way of doing things. Requires the latest PGF release:

\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{graphs,graphdrawing,arrows.meta}
\usegdlibrary{layered}
\begin{document}
\tikz[>={Stealth[]}]
\graph [layered layout, fresh nodes, nodes={anchor=base, text height=0.5em}] {

{ [edge={draw=none}, nodes={text=black!75, font=\sf, anchor=base east}]
model -- parameters -- observations
};

{[math nodes]
"\mu,\sigma^2" -> {
"\theta_1" -> "y_1",
"\theta_2" -> "y_2",
"\ldots" -> "\ldots",
"\theta_k" -> "y_k"};
};
};
\end{document}


A solution that uses pst-tree; it consists in drawing a parallel tree for the text:

\documentclass[12pt, a4paper]{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{MinionPro}
\usepackage[pdf, x11names]{pstricks}%
\usepackage{pst-node, pst-tree}
\def\noedge{\ncline[linestyle = none]}
\renewcommand\psedge{\ncline[arrows =-> ]}
\usepackage{eqparbox}
\begin{document}
\psset{nodesep=3pt, levelsep =1.2cm}

\sffamily
\pstree[thislevelsep = 1.5cm, edge = \noedge]{\TR{\eqmakebox[L][r]{model}}}{%
\pstree{\TR[edge = \noedge]{\eqmakebox[L][r]{parameters}}}{\TR{\eqmakebox[L][r]{observations}}}
}%
$\pstree[thislevelsep = 1.5cm]{\TR{μ,σ^ 2}}{% \pstree{\TR{θ_1}}{\TR{y_1}} \pstree{\TR{θ_2}}{\TR{y_2}} \pstree{\TR[edge = \noedge]{ ... }}{\TR[edge = \noedge]{ ... }} \pstree{\TR *{θ_n}}{\TR{y_n}} }$