我使用以下代码使用 tikz 制作了分层模型:
\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=8em, text centered, minimum height=3em]
\tikzstyle{level1} = [cloud, circle, draw, fill=green!10,
text width=7em, 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=5em, text centered, minimum height=2em]
\tikzstyle{arrow} = [thick,->,>=stealth]
\tikzstyle{a} = [rectangle, draw, minimum height=13em, minimum width=30em]
\tikzstyle{b} = [rectangle, draw, minimum height=26em, minimum width=50em]
\tikzstyle{c} = [rectangle, draw, minimum height=47em, minimum width=45em]
\author{Ehsan Masoudi}
\begin{document}
%\begin{tikzpicture}[node distance = 2cm, 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.5cm 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.5cm and -.4cm of alpha] (mu-alpha) {$\mu_\alpha \sim U(.01, 3)$};
\node [level2, above right = 1.5cm and -.4cm of alpha] (sigma-alpha) {$\sigma_\alpha \sim U(.0001, 2)$};
\node [level2, above left = 3cm and 0.5cm of tau] (theta) {$\theta_{(p)} \sim N(\mu_\theta, \sigma_\theta)$};
\node [level2, below = 5.5cm of tau] (chi) {$\chi_{(p)} \sim N(\mu_\chi, \sigma_\chi)$};
\node [level2, above right = 3cm and .4cm of delta] (eta) {$\eta_{(p)} \sim N(\mu_\eta, \sigma_\eta)$};
\node [level2, below = 1cm of delta] (nu) {$\nu_{(pi)}\sim N(\mu_{\nu(i)}, \sigma_{\nu(i)})$};
\node [level3, above left = 1.5cm and -.4cm of theta] (mu-theta) {$\mu_\theta \sim U(.01, 1)$};
\node [level3, above right = 1.5cm and -.4cm of theta] (sigma-theta) {$\sigma_\theta \sim U(.0001, .25)$};
\node [level3, above right = .7cm and 4.2cm of nu] (mu-nu) {$\mu_{\nu(i)} \sim U(-5, 5)$};
\node [level3, right = 4cm of nu] (sigma-nu) {$\sigma_{\nu(i)}\sim U(.0001, 3)$};
\node [level3, above left = 1.5cm and -.4cm of eta] (mu-eta) {$\mu_\eta = 3.5$};
\node [level3, above right = 1.5cm and -.4cm of eta] (sigma-eta) {$\sigma_\eta = 3.5$};
\node [level3, below left = 1.5cm and -.4cm of chi] (mu-chi) {$\mu_\chi = .35$};
\node [level3, below right = 1.5cm and -.4cm of chi] (sigma-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}
现在,我想为每个盘子添加索引 p = 1,...P,i = 1,...I 和 j = 1,...J (图中红色索引是用油漆画出来的!)
任何帮助,将不胜感激。
PS:此外,我如何才能用一种语法将所有节点中的文本加粗?或者用任何语法使它们更明显。例如,将所有先验分布排列在模型旁边的一列中。
答案1
该包tikz-BayesNet
的\plate
最后一个参数是要放置在盘子内的文本。例如(来自 Lee 和 Wagenmakers 的《贝叶斯认知模型》):
\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}
答案2
默认情况下,标签节点的锚点是这样选择的:外部标记节点。
一旦选择了标签锚点,标签节点将放置在label distance
距离主节点(默认为 0pt)的位置。如果此距离为负数,则标签将向主节点中心移动。
这样,所有三个fit
节点都可以声明为
\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) {};
获得