Make a Graphical Figure of your SEM model in OpenMx

[This article was first published on Industrial Code Workshop, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.


In this post, I made an SEM model and showed the results in a table.
It’s a great feature of SEM that you can sketch your ideas about how the world works, and being able to get such a sketch back out of OpenMx is very helpful.
Importantly, a figure can help readers understand what you’ve done, and it is a great help to verify for yourself that you’ve drawn the model you think you have drawn.
So… how do you get presentation-quality figures out of OpenMx?
I’ll assume you’ve got a model already run, and it’s called model
In out-of-the-box OpenMx, you can say
    omxGraphviz(model, dotFilename="cool.dot")

Boom!

As they say on 2-minute R: “Boom! You just made a figure!”
Sure: but where is it? Well, its a text file called cool.dot sitting in the directory getwd() points to.
To turn it into a graphic, we have to open it in an application that understands how to read .dot or “graphviz” syntax.
First, download an application that can read .dot. On windows, that includes Vizio. On mac, it includesOmnigraffle.
An application I like, especially early on in developing a model, is GraphViz. Believe it or not, this is yet another amazing product of Bell Laboratories (as if S (and therefore R), CUnixCosmic Background radiation, and the transistor to name just a few were not enough) !
That done, this code should pop up the file in Graphviz for you (you might need to make Graphviz the default application for .dot (esp. if you have word on your system))
    system(paste("open ", getwd(), "/cool.dot", sep=""));
The builtin function doesn’t open the file automatically, and, importantly, doesn’t include path values or allow you to standardize them.
I wrote this function which does these things.
It also defaults to creating a file based on the model@name
 umxGraph_RAM(model) # default call opens standardized model in editor, with 2 decimal places of accuracy.
 # some extra parameters
 umxGraph_RAM(model, std = T, dotFilename = "name",  pathLabels = "both", precision = 3)
 # set the dotFilename to NA to get the syntax back
 umxGraph_RAM(model, std = T, dotFilename = NA,  pathLabels = "both", precision = 3)
You can grab it here

To leave a comment for the author, please follow the link and comment on their blog: Industrial Code Workshop.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)