7

I made my Hierarchical model with tikz by following code:

‎‎\documentclass[10pt,a4paper]{article}‎
‎\usepackage[utf8]{inputenc}‎
‎\usepackage{amsmath}‎
‎\usepackage{amsfonts}‎
‎\usepackage{amssymb}‎
‎\usepackage{color}‎
‎\usepackage{graphicx}‎
‎\usepackage{multirow}‎
‎‎‎\usepackage[landscape]{geometry}‎
‎\usepackage{tikz}‎
‎\usetikzlibrary{shapes,‎arrows, fit‎‎‎}‎‎
\usetikzlibrary{positioning}‎

% ‎Define block styles‎
‎\tikzstyle{response} = [cloud‎, ‎circle‎, ‎draw‎, ‎fill=blue!20‎, 
    ‎text width=‎8‎em‎, ‎text centered‎,  ‎minimum height=3em]‎

‎\tikzstyle{level1} = [cloud‎, ‎circle‎, ‎draw‎, ‎fill=green!10‎, 
    ‎text width=‎7‎em‎, ‎text centered‎,  ‎minimum height=2em]‎    

‎\tikzstyle{level2} = [cloud‎, ‎circle‎, ‎draw‎, ‎fill=red!10‎, 
    ‎text width=6em‎, ‎text centered‎,  ‎minimum height=2em]  ‎‎
    ‎
 ‎\tikzstyle{‎level3‎} = [cloud‎, ‎circle‎, ‎draw‎, ‎fill=yellow!10‎, 
   ‎text width=‎5‎‎em‎, ‎text centered‎,  ‎minimum height=2em]  ‎‎‎
    ‎
‎\tikzstyle{arrow} = [thick,->,>=‎stealth]‎‎
\tikzstyle{a} = [rectangle, draw, minimum height=1‎3‎‎‎‎‎‎em, minimum width=‎3‎‎0em]‎‎
\tikzstyle{‎b‎} = [rectangle, draw, minimum height=‎2‎6‎‎‎em, minimum width=‎50‎‎em]‎‎
\tikzstyle{‎c} = [rectangle, draw, minimum height=‎4‎7‎‎‎‎‎em, minimum width=‎45‎‎em]‎‎‎
‎\author{Ehsan Masoudi}‎
‎\begin{document}‎


‎%‎\begin{tikzpicture}[node distance = ‎2‎‎cm‎, ‎‎thick‎, every node/.style={scale=0.‎7‎}, ‎‎auto]‎‎
%align = flush centera‎
\begin{tikzpicture}[thick,scale=0.‎7‎, every node/.style={transform shape}]‎
% ‎Place nodes‎
 % ‎\node [response] (resp) {$y_{(pij)} \sim Wiener(\alpha_{(p)}‎, ‎\beta‎, ‎\tau_{(pij)}‎, ‎\delta_{(pij)})$};‎

 ‎\node [response] (resp) {$y_{(pij)}$};‎
‎%%level1‎    
\node [level1‎, ‎above = ‎1.5‎‎‎cm of  resp] (alpha) {$\alpha_{(p)} \sim N(\mu_{\alpha}‎, ‎\sigma^2_{\alpha})$}‎;‎‎
‎\node [level1‎, ‎left =‎ ‎.7cm‎ of  resp] (tau) {$\tau_{(pij)} \sim N(\theta_{(p)}‎, ‎\chi_{(p)})$};‎  
‎\node [level1‎, ‎right = .7cm of resp] (delta) {$\delta_{(pij)} \sim N(\nu_{(pi)}‎, ‎\eta_{(p)})$};‎

‎%%level2‎ 
‎‎‎‎\node [‎level2‎‎, ‎above left =‎ ‎1.5‎cm and ‎-‎.‎4‎‎cm‎ of alpha] (‎mu‎-‎alpha) ‎{‎‎$‎\mu_‎\‎alpha ‎\sim U(.01, ‎3‎)‎‎$‎}‎;‎
‎\node [level2‎, ‎above right =‎ ‎1.5‎cm and ‎-‎.‎4‎cm of alpha] (‎sig‎ma-alpha)‎ {‎‎$‎\sigma‎_‎\alpha \sim U(.‎0‎0‎0‎1, ‎2‎‎)‎$‎}‎;‎‎
‎
‎\node [level2‎, ‎above ‎left = ‎3‎cm ‎and ‎0.5‎‎cm‎ of ‎tau‎] ‎(‎theta)‎ {‎$‎‎‎\theta_{(p)‎} \sim N(\mu_‎\t‎heta‎, ‎\sigma_\theta)‎$‎‎‎‎‎}‎‎‎;‎‎
‎\node [level2‎, ‎below ‎= ‎5.5‎‎‎cm‎  ‎‎of ‎tau‎] ‎(‎chi)‎ {‎$‎‎‎\chi_{(p)‎} \sim N(\mu_‎\chi‎‎, ‎\sigma_‎\chi)‎‎‎‎$‎}‎‎‎;‎‎
‎‎\node [level2‎, ‎above ‎right = ‎3‎cm ‎and ‎.4cm‎ ‎‎of ‎delta‎‎] ‎(‎eta)‎ {‎$‎\eta‎_{(p)‎} \sim N(\mu_‎\eta‎‎‎, ‎\sigma_‎\eta)‎‎‎‎$‎}‎‎‎;‎‎
‎\node [level2‎, ‎below = ‎1‎‎‎‎‎‎cm ‎‎of ‎delta‎‎] ‎(‎nu‎)‎ {‎$‎\nu‎‎_{(pi)‎}‎‎‎\sim N(\mu_‎{\nu(i)}‎‎, ‎\sigma_‎{\nu(i)})$‎}‎‎‎;‎‎
‎
‎‎‎‎\node [‎level3‎‎‎, ‎above left =‎ ‎1.‎5‎‎cm and ‎-‎.‎4‎cm‎ of ‎theta‎] (‎mu‎-‎theta) ‎{‎‎$‎\mu_‎\theta‎‎ \sim U(.0‎‎1, ‎1‎‎)‎$‎}‎;‎
‎\node [level3‎, ‎above right =‎ ‎1.5‎cm and ‎-‎.‎4‎cm‎ of theta] (‎sig‎ma-‎thet‎a)‎ {‎‎$‎\sigma‎_‎‎\theta \sim U(.0‎0‎0‎‎1, ‎.‎25‎)‎‎$‎}‎;‎‎
‎
‎‎‎‎‎\node [‎level3‎‎‎, ‎above right = ‎.7‎‎‎‎‎cm ‎and ‎4‎.2‎‎‎‎‎‎cm‎ ‎‎‎‎‎of ‎nu‎‎] (‎mu‎-‎nu) ‎{‎‎$‎\mu_{‎\nu(i)‎} ‎‎\sim U(-5, 5)‎$‎}‎;‎
‎\node [level3‎, ‎right =‎ ‎4‎‎cm ‎‎‎ of nu] (‎sig‎ma-nu)‎ {‎‎$‎\sigma‎_‎‎{\nu‎(i)}\sim U(.0001, ‎3‎‎)‎$‎}‎;‎‎
‎
‎‎‎‎‎\node [‎level3‎‎‎, ‎above left =‎ ‎1.5‎cm and ‎-‎.‎4‎cm of ‎eta‎] (‎mu‎-‎eta) ‎{‎‎$‎\mu_‎\‎‎eta = ‎3.5‎‎$‎}‎;‎
‎\node [level3‎, ‎above right =‎ ‎‎1.5‎cm and -.4cm‎ of eta] (‎sig‎ma-eta)‎ {‎‎$‎\sigma‎_‎‎\eta = ‎3.5‎‎$‎}‎;‎‎‎

‎‎‎‎‎\node [‎level3‎‎‎, ‎below‎ left = ‎‎‎1‎.5‎‎‎cm ‎and ‎-‎.‎4‎‎‎‎‎‎cm‎ of ‎chi‎] (‎mu‎-‎chi) ‎{‎‎$‎\mu_‎‎\chi = ‎.35‎$‎}‎;‎
‎\node [level3‎, ‎below right = ‎1.5‎cm ‎and ‎-‎.4‎cm‎ of chi] (‎sig‎ma-chi)‎ {‎‎$‎\sigma‎_‎‎\chi = ‎.125‎‎$‎}‎;‎‎

 ‎%‎‎\node‎ [above right =0.75cm and 0cm of ‎theta‎] {CGPA} edge (student)‎;‎
‎
‎
%level1
\draw [arrow] ‎(‎mu-theta‎) ‎-- ‎(theta)‎;
‎\draw [arrow] ‎(‎sigma‎-theta‎) ‎-- ‎(theta)‎;‎‎
‎
\draw [arrow] ‎(‎mu-alpha‎) ‎-- ‎(alpha)‎;
‎\draw [arrow] ‎(‎sigma‎-alpha‎) ‎-- ‎(alpha)‎;‎‎
‎
‎\draw [‎thick, dashed, ->‎]‎ ‎(‎mu-‎eta‎‎) ‎-- ‎(eta)‎;
‎\draw [‎thick, dashed, ->‎] ‎(‎sigma‎-eta‎) ‎-- ‎(eta)‎;‎‎
‎
‎\draw [arrow] ‎(‎mu-‎nu‎‎‎) ‎-- ‎(‎nu‎)‎;
‎\draw [arrow] ‎(‎sigma‎-nu‎) ‎-- ‎(nu)‎;‎‎
‎
‎\draw [‎thick, dashed, ->‎] ‎(‎mu-‎chi‎‎‎‎) ‎-- ‎(‎chi‎‎)‎;
‎\draw  [‎thick, dashed, ->‎]‎‎(‎sigma‎-chi‎) ‎-- ‎(chi)‎;‎‎‎‎‎‎
‎‎
%level2‎
‎‎\draw [arrow] ‎(‎‎chi‎) ‎-- ‎(‎tau‎)‎;‎‎
‎‎\draw [arrow] ‎(‎‎theta‎‎) ‎-- ‎(‎tau‎)‎;‎‎
‎
‎‎\draw [arrow] ‎(‎‎eta‎‎) ‎-- ‎(‎delta‎)‎;‎‎
‎‎\draw [arrow] ‎(‎nu‎‎‎) ‎-- ‎(‎delta‎)‎;‎‎
‎
%level2‎
‎‎\draw [arrow] ‎(‎‎chi‎) ‎-- ‎(‎tau‎)‎;‎‎
‎‎\draw [arrow] ‎(‎‎theta‎‎) ‎-- ‎(‎tau‎)‎;‎‎
‎
‎‎\draw [arrow] ‎(‎‎eta‎‎) ‎-- ‎(‎delta‎)‎;‎‎
‎‎\draw [arrow] ‎(‎nu‎‎‎) ‎-- ‎(‎delta‎)‎;‎‎‎‎‎‎‎
%%path‎
‎‎
‎‎‎\draw [arrow] ‎(‎‎alpha‎‎) ‎-- ‎(‎resp‎)‎;‎‎
‎‎\draw [‎arrow‎‎‎‎‎‎] ‎(‎tau‎‎‎) ‎-- ‎(‎resp‎‎)‎;‎‎
‎‎\draw [‎arrow‎‎‎] ‎(‎delta‎‎‎) ‎-- ‎(‎resp‎‎)‎;‎‎‎‎
‎
‎\node[‎a‎,fit=(‎resp‎) (tau) (delta)] (container) {‎‎‎‎‎‎}‎;‎‎
‎\node[‎b‎‎,fit=(‎resp‎) (tau) (delta)‎(nu)(mu-nu)(sigma-nu)‎‎] (container) {‎}‎;‎‎
‎\node[‎c‎‎‎,fit=(‎resp‎) (tau) (delta)‎(nu)(‎chi‎)(‎theta‎)(alpha)(eta)‎] (container) {‎}‎;‎‎‎‎
‎
‎\end{tikzpicture}‎
‎\end{document}

Now, I would like to add index p = 1,...P, i = 1,...I and j = 1,...J for each plate (red index in the figure is drawn by paint!)

enter image description here

any help would be appreciated.

P.S.: in addition how can I bold the text in all nodes my one syntax? or any syntax that make them more obvious. For example, arranging all of the prior distributions in a column next to the model.

2
  • 1
    Add font=\bfseries to the options you pass to tikzpicture?
    – cfr
    Sep 5, 2014 at 23:38
  • I think it's not possible to place labels inside nodes but you can add new nodes where you need. \node[above=3mm of container.south] {p index}; could serve you to label third container. Previous syntax is valid if you introduce it after every container if you want to add all labels after all fit nodes, change their names accordingly.
    – Ignasi
    Sep 6, 2014 at 11:48

2 Answers 2

5

The package tikz-BayesNet has \plate which has as its final argument the text to place within the plate. For example (from Lee and Wagenmakers' Bayesian Cognitive Modeling):

\documentclass{article} 
\usepackage{tikz}
\usetikzlibrary{bayesnet}
\usetikzlibrary{arrows}
\usepackage{color}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{subcaption}
\begin{document}
\begin{figure}
  \tikz{
    % nodes
     \node[obs] (x) {$x_i$};%
     \node[latent,above=of x,xshift=-1cm] (mu) {$\mu$}; %
     \node[latent,above=of x] (r) {$r$}; %
     \node[latent,above=of x,xshift=1cm] (sigma) {$\sigma$}; %
    % plate
     \plate [inner sep=.3cm,xshift=.02cm,yshift=.2cm] {plate1} {(x)} {$i$ data}; %
    % edges
     \edge {r,sigma,mu} {x}  }
\end{figure}
\end{document}

enter image description here

1
  • Could you show an example? As it stands now, this is better-suited as a comment.
    – Werner
    Dec 19, 2014 at 15:53
2

By default anchors for label nodes are selected such a way that labels are placed outside of labeled nodes.

Once label anchor is selected, label node is placed at label distance (0pt by default) from main node. If this distance is negative, labels are moved towards main node center.

This way, all three fit nodes can be declared with

\node[a, fit=(resp) (tau) (delta), 
      label={[label distance=-7mm, text=red]235:$j=1,\dots,J$}] (container) {};
\node[b, fit=(resp) (tau) (delta) (nu) (mu-nu) (sigma-nu), 
      label={[label distance=-8mm, text=red]230:$i=1,\dots,I$}] (container) {};
\node[c,fit=(resp) (tau) (delta) (nu) (chi) (theta) (alpha) (eta), 
      label={[label distance=-6mm, text=red]270:$p=1,\dots,P$}] (container) {};

to obtain

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.