Showing posts with label tikz. Show all posts
Showing posts with label tikz. Show all posts
Thursday, December 19, 2013
Tikz: simple decision tree (or CART tree) example
I created the above diagram of a decision tree using the tikz package for LaTeX. It took me a while to find a simple, clear way to produce a decision tree. This is the code I used:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\newdimen\nodeDist
\nodeDist=35mm
\begin{document}
\begin{tikzpicture}[
node/.style={%
draw,
rectangle,
},
]
\node [node] (A) {IDS $>$ 13.5?};
\path (A) ++(-135:\nodeDist) node [node] (B) {exit 1};
\path (A) ++(-45:\nodeDist) node [node] (C) {LCIanx $>$ 0.3604?};
\path (C) ++(-135:\nodeDist) node [node] (D) {exit 2};
\path (C) ++(-45:\nodeDist) node [node] (E) {exit 3};
\draw (A) -- (B) node [left,pos=0.25] {no}(A);
\draw (A) -- (C) node [right,pos=0.25] {yes}(A);
\draw (C) -- (D) node [left,pos=0.25] {no}(A);
\draw (C) -- (E) node [right,pos=0.25] {yes}(A);
\end{tikzpicture}
\end{document}
Friday, September 27, 2013
Adjusting size of nodes and text labels in tikz (LaTeX)
The following picture is created with tikz, using the same node sizes and text label sizes for every node:
\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning}
\begin{document}
\begin{tikzpicture}[auto,node distance=.5cm,
latent/.style={circle,draw,very thick,inner sep=0pt,minimum size=30mm,align=center},
manifest/.style={rectangle,draw,very thick,inner sep=0pt,minimum width=45mm,minimum height=10mm},
paths/.style={->, ultra thick, >=stealth'},
]
% Define observed variables
\node [manifest] (things) at (0,0) {things};
% Define latent variables
\node [latent] (all) [left=3.5cm of things] {All};
\node [latent] (random) [above right=3.5cm of things] {random};
% Draw paths from latent to observed variables
\draw [paths] (all) to node { } (things);
\draw [paths] (random) to node { } (things);
\end{tikzpicture}
\end{document}
However, I didn't like the node sizes, so I changed them using the 'scale' argument within the 'node' command:
\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning}
\begin{document}
\begin{tikzpicture}[auto,node distance=.5cm,
latent/.style={circle,draw,very thick,inner sep=0pt,minimum size=30mm,align=center},
manifest/.style={rectangle,draw,very thick,inner sep=0pt,minimum width=45mm,minimum height=10mm},
paths/.style={->, ultra thick, >=stealth'},
]
% Define observed variables
\node [manifest, scale=.75] (things) at (0,0) {things};
% Define latent variables
\node [latent] (all) [left=3.5cm of things] {All};
\node [latent, scale=.5] (random) [above right=3.5cm of things] {random};
% Draw paths from latent to observed variables
\draw [paths] (all) to node { } (things);
\draw [paths] (random) to node { } (things);
\end{tikzpicture}
\end{document}
The scale command changes the size of the node, as well as the size of the text label. So, you may want to change the size of the text labels only. You can do this by using one of the following LaTeX text size commands:
\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning}
\begin{document}
\begin{tikzpicture}[auto,node distance=.5cm,
latent/.style={circle,draw,very thick,inner sep=0pt,minimum size=30mm,align=center},
manifest/.style={rectangle,draw,very thick,inner sep=0pt,minimum width=45mm,minimum height=10mm},
paths/.style={->, ultra thick, >=stealth'},
]
% Define observed variables
\node [manifest, scale=.75] (things) at (0,0) {\Large things};
% Define latent variables
\node [latent] (all) [left=3.5cm of things] {\large All};
\node [latent, scale=.5] (random) [above right=3.5cm of things] {\huge random};
% Draw paths from latent to observed variables
\draw [paths] (all) to node { } (things);
\draw [paths] (random) to node { } (things);
\end{tikzpicture}
\end{document}
Note that you can always change font size locally in LaTeX documents by using one of the following commands:
\tiny
\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning}
\begin{document}
\begin{tikzpicture}[auto,node distance=.5cm,
latent/.style={circle,draw,very thick,inner sep=0pt,minimum size=30mm,align=center},
manifest/.style={rectangle,draw,very thick,inner sep=0pt,minimum width=45mm,minimum height=10mm},
paths/.style={->, ultra thick, >=stealth'},
]
% Define observed variables
\node [manifest] (things) at (0,0) {things};
% Define latent variables
\node [latent] (all) [left=3.5cm of things] {All};
\node [latent] (random) [above right=3.5cm of things] {random};
% Draw paths from latent to observed variables
\draw [paths] (all) to node { } (things);
\draw [paths] (random) to node { } (things);
\end{tikzpicture}
\end{document}
However, I didn't like the node sizes, so I changed them using the 'scale' argument within the 'node' command:
\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning}
\begin{document}
\begin{tikzpicture}[auto,node distance=.5cm,
latent/.style={circle,draw,very thick,inner sep=0pt,minimum size=30mm,align=center},
manifest/.style={rectangle,draw,very thick,inner sep=0pt,minimum width=45mm,minimum height=10mm},
paths/.style={->, ultra thick, >=stealth'},
]
% Define observed variables
\node [manifest, scale=.75] (things) at (0,0) {things};
% Define latent variables
\node [latent] (all) [left=3.5cm of things] {All};
\node [latent, scale=.5] (random) [above right=3.5cm of things] {random};
% Draw paths from latent to observed variables
\draw [paths] (all) to node { } (things);
\draw [paths] (random) to node { } (things);
\end{tikzpicture}
\end{document}
The scale command changes the size of the node, as well as the size of the text label. So, you may want to change the size of the text labels only. You can do this by using one of the following LaTeX text size commands:
\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning}
\begin{document}
\begin{tikzpicture}[auto,node distance=.5cm,
latent/.style={circle,draw,very thick,inner sep=0pt,minimum size=30mm,align=center},
manifest/.style={rectangle,draw,very thick,inner sep=0pt,minimum width=45mm,minimum height=10mm},
paths/.style={->, ultra thick, >=stealth'},
]
% Define observed variables
\node [manifest, scale=.75] (things) at (0,0) {\Large things};
% Define latent variables
\node [latent] (all) [left=3.5cm of things] {\large All};
\node [latent, scale=.5] (random) [above right=3.5cm of things] {\huge random};
% Draw paths from latent to observed variables
\draw [paths] (all) to node { } (things);
\draw [paths] (random) to node { } (things);
\end{tikzpicture}
\end{document}
Note that you can always change font size locally in LaTeX documents by using one of the following commands:
\tiny
\scriptsize
\footnotesize
\small
\normalsize
\large
\Large
\LARGE
\huge
\Huge
Friday, September 20, 2013
I think tikz iz fantaztic! (software for drawing SEM models)
I have been trying to produce my own diagrams for structural equation models and path diagrams. Today, I produced the following diagram using the tikz package in LaTeX:
Here is the code for creating the diagram (please note: it works using 'pdf texify', not using 'texify', not sure about the rest):
\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning}
\newcommand{\at}{\makeatletter @\makeatother}
\begin{document}
\begin{tikzpicture}[auto,node distance=.5cm,
latent/.style={circle,draw,very thick,inner sep=0pt,minimum size=30mm,align=center},
manifest/.style={rectangle,draw,very thick,inner sep=0pt,minimum width=45mm,minimum height=10mm},
paths/.style={->, ultra thick, >=stealth'},
twopaths/.style={<->, ultra thick, >=stealth'}
]
% Define observed variables
\node [manifest] (BSS01T1) at (0,0) {BSS01 \at T1};
\node [manifest] (BSS02T1) [below=of BSS01T1] {BSS02 \at T1};
\node [manifest] (BSS03T1) [below=of BSS02T1] {BSS03 \at T1};
\node [manifest] (BSS17T1) [below=2cm of BSS03T1] {BSS16 \at T1};
\node [manifest] (BSS18T1) [below=of BSS17T1] {BSS17 \at T1};
\node [manifest] (BSS19T1) [below=of BSS18T1] {BSS19 \at T1};
\node [manifest] (BSS01T2) [below=2cm of BSS19T1] {BSS01 \at T2};
\node [manifest] (BSS02T2) [below=of BSS01T2] {BSS02 \at T2};
\node [manifest] (BSS03T2) [below=of BSS02T2] {BSS03 \at T2};
\node [manifest] (BSS17T2) [below=2cm of BSS03T2] {BSS17 \at T2};
\node [manifest] (BSS18T2) [below=of BSS17T2] {BSS18 \at T2};
\node [manifest] (BSS19T2) [below=of BSS18T2] {BSS19 \at T2};
% Draw dashed vertical lines to show that items in middle of questionnaire are not shown
\draw[dashed, ultra thick] (BSS03T1.south) -- (BSS17T1.north);
\draw[dashed, ultra thick] (BSS03T2.south) -- (BSS17T2.north);
% Define latent variables
\node [latent] (SIT1) [left=3.5cm of BSS17T1] {Suicidal \\ Ideation \at T1};
\node [latent] (SIT2) [left=3.5cm of BSS03T2] {Suicidal \\ Ideation \at T2};
% Draw paths form latent to observed variables
\foreach \all in {BSS01T1, BSS02T1, BSS03T1, BSS17T1, BSS18T1, BSS19T1}{
\draw [paths] (SIT1.east) to node { } (\all.west);
}
\foreach \all in {BSS01T2, BSS02T2, BSS03T2, BSS17T2, BSS18T2, BSS19T2}{
\draw [paths] (SIT2.east) to node { } (\all.west);
}
% Draw lines to indicate (residual) covariances
\draw [twopaths] (BSS01T1.east) to [bend left=90] (BSS01T2.east);
\draw [twopaths] (BSS02T1.east) to [bend left=90] (BSS02T2.east);
\draw [twopaths] (BSS03T1.east) to [bend left=90] (BSS03T2.east);
\draw [twopaths] (BSS17T1.east) to [bend left=90] (BSS17T2.east);
\draw [twopaths] (BSS18T1.east) to [bend left=90] (BSS18T2.east);
\draw [twopaths] (BSS19T1.east) to [bend left=90] (BSS19T2.east);
\draw [twopaths] (SIT1.west) to [bend right=90] (SIT2);
\end{tikzpicture}
\end{document}
Here is the code for creating the diagram (please note: it works using 'pdf texify', not using 'texify', not sure about the rest):
\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning}
\newcommand{\at}{\makeatletter @\makeatother}
\begin{document}
\begin{tikzpicture}[auto,node distance=.5cm,
latent/.style={circle,draw,very thick,inner sep=0pt,minimum size=30mm,align=center},
manifest/.style={rectangle,draw,very thick,inner sep=0pt,minimum width=45mm,minimum height=10mm},
paths/.style={->, ultra thick, >=stealth'},
twopaths/.style={<->, ultra thick, >=stealth'}
]
% Define observed variables
\node [manifest] (BSS01T1) at (0,0) {BSS01 \at T1};
\node [manifest] (BSS02T1) [below=of BSS01T1] {BSS02 \at T1};
\node [manifest] (BSS03T1) [below=of BSS02T1] {BSS03 \at T1};
\node [manifest] (BSS17T1) [below=2cm of BSS03T1] {BSS16 \at T1};
\node [manifest] (BSS18T1) [below=of BSS17T1] {BSS17 \at T1};
\node [manifest] (BSS19T1) [below=of BSS18T1] {BSS19 \at T1};
\node [manifest] (BSS01T2) [below=2cm of BSS19T1] {BSS01 \at T2};
\node [manifest] (BSS02T2) [below=of BSS01T2] {BSS02 \at T2};
\node [manifest] (BSS03T2) [below=of BSS02T2] {BSS03 \at T2};
\node [manifest] (BSS17T2) [below=2cm of BSS03T2] {BSS17 \at T2};
\node [manifest] (BSS18T2) [below=of BSS17T2] {BSS18 \at T2};
\node [manifest] (BSS19T2) [below=of BSS18T2] {BSS19 \at T2};
% Draw dashed vertical lines to show that items in middle of questionnaire are not shown
\draw[dashed, ultra thick] (BSS03T1.south) -- (BSS17T1.north);
\draw[dashed, ultra thick] (BSS03T2.south) -- (BSS17T2.north);
% Define latent variables
\node [latent] (SIT1) [left=3.5cm of BSS17T1] {Suicidal \\ Ideation \at T1};
\node [latent] (SIT2) [left=3.5cm of BSS03T2] {Suicidal \\ Ideation \at T2};
% Draw paths form latent to observed variables
\foreach \all in {BSS01T1, BSS02T1, BSS03T1, BSS17T1, BSS18T1, BSS19T1}{
\draw [paths] (SIT1.east) to node { } (\all.west);
}
\foreach \all in {BSS01T2, BSS02T2, BSS03T2, BSS17T2, BSS18T2, BSS19T2}{
\draw [paths] (SIT2.east) to node { } (\all.west);
}
% Draw lines to indicate (residual) covariances
\draw [twopaths] (BSS01T1.east) to [bend left=90] (BSS01T2.east);
\draw [twopaths] (BSS02T1.east) to [bend left=90] (BSS02T2.east);
\draw [twopaths] (BSS03T1.east) to [bend left=90] (BSS03T2.east);
\draw [twopaths] (BSS17T1.east) to [bend left=90] (BSS17T2.east);
\draw [twopaths] (BSS18T1.east) to [bend left=90] (BSS18T2.east);
\draw [twopaths] (BSS19T1.east) to [bend left=90] (BSS19T2.east);
\draw [twopaths] (SIT1.west) to [bend right=90] (SIT2);
\end{tikzpicture}
\end{document}
Subscribe to:
Posts (Atom)




