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}

1 comment:

  1. Great post. Thank you so much for sharing. I need to learn tikz.

    ReplyDelete