Drawing path diagrams with R package 'sem' using Graphviz

I am using a package sem

to do my analysis. To draw a path diagram, I use the function pathDiagram

in sem

to get the code and then use it in Graphviz

to get the diagram. I have two questions:


R.DHP <- readMoments(diag=FALSE, names=c('ROccAsp', 'REdAsp', 'FOccAsp', 
                'FEdAsp', 'RParAsp', 'RIQ', 'RSES', 'FSES', 'FIQ', 'FParAsp'))
    .3269  .3669                                                        
    .4216  .3275  .6404                                      
    .2137  .2742  .1124  .0839                                
    .4105  .4043  .2903  .2598  .1839                          
    .3240  .4047  .3054  .2786  .0489  .2220                    
    .2930  .2407  .4105  .3607  .0186  .1861  .2707              
    .2995  .2863  .5191  .5007  .0782  .3355  .2302  .2950        
    .0760  .0702  .2784  .1988  .1147  .1021  .0931 -.0438  .2087  

model.dhp <- specifyModel()
    RParAsp  -> RGenAsp, gam11,  NA
    RIQ      -> RGenAsp, gam12,  NA
    RSES     -> RGenAsp, gam13,  NA
    FSES     -> RGenAsp, gam14,  NA
    RSES     -> FGenAsp, gam23,  NA
    FSES     -> FGenAsp, gam24,  NA
    FIQ      -> FGenAsp, gam25,  NA
    FParAsp  -> FGenAsp, gam26,  NA
    FGenAsp  -> RGenAsp, beta12, NA
    RGenAsp  -> FGenAsp, beta21, NA
    RGenAsp  -> ROccAsp,  NA,       1
    RGenAsp  -> REdAsp,  lam21,  NA
    FGenAsp  -> FOccAsp,  NA,       1
    FGenAsp  -> FEdAsp,  lam42,  NA
    RGenAsp <-> RGenAsp, ps11,   NA
    FGenAsp <-> FGenAsp, ps22,   NA
    RGenAsp <-> FGenAsp, ps12,   NA
    ROccAsp <-> ROccAsp, theta1, NA
    REdAsp  <-> REdAsp,  theta2, NA
    FOccAsp <-> FOccAsp, theta3, NA
    FEdAsp  <-> FEdAsp,  theta4, NA

sem.dhp <- sem(model.dhp, R.DHP, 329,
    fixed.x=c('RParAsp', 'RIQ', 'RSES', 'FSES', 'FIQ', 'FParAsp'))

pathDiagram(sem.dhp, min.rank='RIQ, RSES, RParAsp, FParAsp, FSES, FIQ', 
    max.rank='ROccAsp, REdAsp, FEdAsp, FOccAsp')


  • How to draw a path diagram directly from R

    using a function pathDiagram

  • How do I include a graph in a file Rnw

    for Sweave


source to share

1 answer

You just need to give the filename (no extension!), See the argument file=

. As the documentation says, it will create both a file .dot

and a PDF (but install output.type="dot"

if you only want graphviz output).

I would use a simple command \includegraphics

in the Sweave file after calling the above command. (You may need to adapt the shape search path unless you generate the SEM diagram in the same directory as your main file .Rnw



Given your comment, yes, it looks like there is a problem with starting an external program from a function call ( pathDiagram

). So, this is not a very elegant solution to generate a path diagram and include it in your Sweave-> TeX document.

Here is the Sweave ( sw.rnw

) file :

R.DHP <- readMoments("sem.cov", diag=FALSE, 
                     names=c('ROccAsp', 'REdAsp', 'FOccAsp', 
                       'FEdAsp', 'RParAsp', 'RIQ', 'RSES',  
                       'FSES', 'FIQ', 'FParAsp'))
model.dhp <- specifyModel(file="sem.mod")
sem.dhp <- sem(model.dhp, R.DHP, 329,
               fixed.x=c('RParAsp', 'RIQ', 'RSES', 'FSES', 'FIQ', 'FParAsp'))            
capture.output(pathDiagram(sem.dhp, min.rank='RIQ, RSES, RParAsp, FParAsp, FSES, FIQ', 
            max.rank='ROccAsp, REdAsp, FEdAsp, FOccAsp'), file="sem.dot")
system("dot -Tpdf -o fig1.pdf  sem.dot")

And here is the path diagram.




The files sem.cov

and sem.mod

provides the covariance matrix and the structural model, both of which were manually entered in the above example (simple copy / paste in a text file). I am not very happy to use capture.output()

because I cannot find a way to mask its call from a chunk. Maybe you can find a better way to do this (the idea is to use system()

it and it can be easily masked with echo=FALSE

chunk in the parameters).

I managed to compile the above document like this:

$ R CMD Sweave sw.rnw
$ R CMD texi2pdf sw.tex


enter image description here



All Articles