Best practices for drawing path diagrams?

3,047 views
Skip to first unread message

Bertolt Meyer

unread,
Sep 13, 2012, 9:30:43 AM9/13/12
to lav...@googlegroups.com
Dear fellow lavaan users,

I was wondering how you go about drawing path diagrams based on the parameter estimates that lavaan delivers. Is there a better option than drawing it by hand, e.g., by involving tikz somehow? Any advice would be greatly appreciated.

Thank you,
Bertolt

yrosseel

unread,
Sep 13, 2012, 10:50:15 AM9/13/12
to lav...@googlegroups.com
The qgraph package produces some nice diagrams (using a network
approach). And it understands lavaan objects.

For publication, I use Latex+Tikz. Manually.

Yves.

Bertolt Meyer

unread,
Sep 13, 2012, 12:47:06 PM9/13/12
to lav...@googlegroups.com
Thanks Yves,

I gave qgraph a try, but it only worked on very simple models and threw errors once I ran it with a real model with four latent variables and a few error covariances (Error in if (shape[E$to[i]] != "square") { : Missing value where TRUE/FALSE is required). So I'll stick with manual tikz plots for the time being.

Bertolt

yrosseel

unread,
Sep 13, 2012, 5:55:12 PM9/13/12
to lav...@googlegroups.com
On 09/13/2012 06:47 PM, Bertolt Meyer wrote:
> Thanks Yves,
>
> I gave qgraph a try, but it only worked on very simple models and threw
> errors once I ran it with a real model with four latent variables and a
> few error covariances (Error in if (shape[E$to[i]] != "square") {
> : Missing value where TRUE/FALSE is required).

Please send this non-working model to the author of qgraph. I'm sure he
will fix it.

Yves.

Alexander Beaujean

unread,
Sep 15, 2012, 11:47:08 AM9/15/12
to lav...@googlegroups.com
I use the dot tool via Graphviz: http://www.graphviz.org There is a little learning curve, but once you get the hang of it, the syntax is very parsimonious so it takes minimal time to create new models.Here are some examples: https://blogs.baylor.edu/alex_beaujean/graphviz/

Best,

Alex

yrosseel

unread,
Sep 15, 2012, 4:04:33 PM9/15/12
to lav...@googlegroups.com
On 09/15/2012 05:47 PM, Alexander Beaujean wrote:
> I use the dot tool via Graphviz: http://www.graphviz.org There is a
> little learning curve, but once you get the hang of it, the syntax is
> very parsimonious so it takes minimal time to create new models.Here are
> some examples: https://blogs.baylor.edu/alex_beaujean/graphviz/

Nice examples. Both the sem and OpenMx packages are able to export a
model using the 'dot' syntax. Lavaan has no such export function yet.
Any volunteers?

Yves.

Rene Mayer

unread,
Sep 16, 2012, 4:12:28 AM9/16/12
to lav...@googlegroups.com
An automatic way to draw CFA's from lavaan with R comes with the
psych-package by Revelle.
The following is from his lesson 'Psychology 454: Latent Variable
Modeling', see slide 28 from
http://personality-project.org/revelle/syllabi/454/wk6.lavaan.pdf.

library(psych)
library(lavaan)
b2 <- Bechtoldt.2[c(3:8,15:17),c(3:8,15:17)]
Thurstone.mod <- ' F1 =~ Sentences + Vocabulary + Completion
F2 =~ First_Letters + Four_letter_words + Suffixes
F3 =~ Letter_Series + Pedigrees + Letter_Grouping'

t.cfa.2 <- cfa(Thurstone.mod,sample.cov=b2,sample.nobs=213,std.lv=TRUE)
summary(t.cfa.2)


lavaan.diagram <-
function(fit,model="cfa",...) {
if(model=="cfa") {
fx=fit@Model@GLIST$lambda
colnames(fx) <- fit@Model@dimNames$lambda[[2]]
Phi <- fit@Model@GLIST$psi
Rx <- fit@Model@GLIST$theta
v.labels <- fit@Model@dimNames$lambda[[1]]

structure.diagram(fx=fx,Phi=Phi,Rx=Rx,labels=v.labels,...)
} else
{structure.diagram(fx=fit@Model@GLIST$lambda,Phi=fit@Model@GLIST$beta,
Rx=fit@Model@GLIST$theta,...) }
}
lavaan.diagram(t.cfa.2)

Alternativly if one wishes to plot with R, one needs to call RGraphviz
or qplot and build up all from the skretch.

regards,
Ren�

Yves Rosseel

unread,
Oct 11, 2012, 2:56:57 AM10/11/12
to lav...@googlegroups.com
Sacha Epskamp (the author of the qgraph package) is working on a new package (drawSEM) which produces very nice diagrams. Not on CRAN yet, but you can get it from github:

https://github.com/SachaEpskamp/drawSEM

René Mayer

unread,
Oct 11, 2012, 4:13:00 AM10/11/12
to lav...@googlegroups.com
thanks for pointing out, Yves! drawSEM::SEMpaths looks nice and saves a lot of time (compared with drawing all by hand qgraph/tikz). all the best, Rene
--
You received this message because you are subscribed to the Google Groups "lavaan" group.
To post to this group, send email to lav...@googlegroups.com.
Visit this group at http://groups.google.com/group/lavaan?hl=en.
 
 

Jong-Hwa

unread,
Feb 1, 2013, 9:28:15 AM2/1/13
to lav...@googlegroups.com
Dear Yves,

I am preparing an introductory materials for SEM with lavaan and R. With my limited knowledge on Graphviz, I have been failed to draw path diagrams, which are used in your writing, "lavaan: an R package for structural equation modeling and more." I would like to put the diagrams of HolzingerSwineford1939, PoliticalDemocracy1965 on the latex document. I think that you also used Graphviz with tikz for diagrams in the document. May I expect some tips for drawing them with graphviz?

best regards,

Jong-Hwa

yrosseel

unread,
Feb 1, 2013, 10:27:26 AM2/1/13
to lav...@googlegroups.com
On 02/01/2013 03:28 PM, Jong-Hwa wrote:
> Dear Yves,
>
> I am preparing an introductory materials for SEM with lavaan and R. With
> my limited knowledge on Graphviz, I have been failed to draw path
> diagrams, which are used in your writing, "lavaan: an R package for
> structural equation modeling and more
> <http://users.ugent.be/%7Eyrosseel/lavaan/lavaanIntroduction.pdf>." I
> would like to put the diagrams of HolzingerSwineford1939,
> PoliticalDemocracy1965 on the latex document. I think that you also used
> Graphviz with tikz for diagrams in the document. May I expect some tips
> for drawing them with graphviz?

See

http://users.ugent.be/~yrosseel/lavaan/

for some latex files (including tikz diagrams).

Yves.


SHIN Jong-Hwa

unread,
Feb 2, 2013, 1:42:02 AM2/2/13
to lav...@googlegroups.com
2013년 02월 02일 00:27, yrosseel 쓴 글:
Thank you very much~. The latex files are very helpful.

Jong-Hwa

Alexander Beaujean

unread,
Feb 3, 2013, 12:55:45 PM2/3/13
to lav...@googlegroups.com
If you are using LaTeX already, you might consider tikz for your path diagrams, e.g., 

\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'},
]

\node [manifest] (SI) at (0,0) {Similarities};
\node [manifest] (VO) [below=of SI]  {Vocabulary};
\node [manifest] (CO) [below=of VO]  {Comprehension};
\node [manifest] (IN) [below=of CO]  {Information};
\node [manifest] (WR) [below=of IN]  {Word Reasoning};
\node [manifest] (BD) [below=of WR]  {Block Design};
\node [manifest] (PS) [below=of BD]  {Picture Concepts};
\node [manifest] (MR) [below=of PS]  {Matrix Reasoning};
\node [manifest] (PC) [below=of MR]  {Picture Completion};
\node [manifest] (DS) [below=of PC]  {Digit Span};
\node [manifest] (LN) [below=of DS]  {Letter-Number};
\node [manifest] (AR) [below=of LN]  {Arithmetic};
\node [manifest] (CD) [below=of AR]  {Coding};
\node [manifest] (SS) [below=of CD]  {Symbol Search};
\node [manifest] (CA) [below=of SS]  {Cancellation};

\node [latent] (g)  [left=2.5cm of MR]  {\emph{g}};
\node [latent] (Gc) [right=4.5cm of CO] {Verbal\\ Comprehension};
\node [latent] (Gv) [right=4.5cm of BD] {Visual\\ Spatial};
\node [latent] (Gf) [right=4.5cm of PC] {Fluid\\ Reasoning};
\node [latent] (Gsm) [right=4.5cm of LN] {Working\\ Memory};
\node [latent] (Gs) [right=4.5cm of SS] {Processing\\ Speed};

\foreach \all in {SI, VO, CO, IN, WR, BD, PS, MR, PC, DS, LN, AR, CD, SS, CA}
{
\draw [paths] (g.east) to node  { }   (\all.west);
}

\foreach \vc in {SI, VO, CO, IN, WR, PC}
\draw [paths] (Gc.west) to node  {}   (\vc.east);

\foreach \vs in {BD, MR, PC, SS}
\draw [paths] (Gv.west) to node  {}   (\vs.east);

\foreach \fr in {PS, MR, AR}
\draw [paths] (Gf.west) to node  {}   (\fr.east);

\foreach \wm in {DS, LN}
\draw [paths] (Gsm.west) to node   { }   (\wm.east);

\foreach \ps in {CD, SS, CA}
\draw [paths] (Gs.west) to node  {}   (\ps.east);

\end{tikzpicture}
\end{document}


Best,

Alex

Andreas Brandmaier

unread,
Feb 8, 2013, 8:57:04 AM2/8/13
to lav...@googlegroups.com
Onyx is a graphical interface that allows to draw models and export them (among other formats) to both lavaan code and LaTeX/tikz code. I attach a screenshot of the political democracy demo in Onyx. The software can be freely downloaded here: http://onyx.brandmaier.de/.
Unfortunately, lavaan models cannot be imported, yet.

best regards,
 Andreas
onyx-lavaan-screenshot1.png
Reply all
Reply to author
Forward
0 new messages