使用Graphviz使用R包'sem'绘制路径图 [英] Drawing path diagrams with R package 'sem' using Graphviz

查看:270
本文介绍了使用Graphviz使用R包'sem'绘制路径图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用sem包进行分析.要绘制路径图,我使用sem中的pathDiagram函数来获取代码,然后在Graphviz中使用它来获取关系图.我有两个问题:

I use the sem package to do my analysis. To draw a path diagram I use the pathDiagram function in sem to get the code and then use it in Graphviz to get the diagram. I have two questions:

library(sem)

R.DHP <- readMoments(diag=FALSE, names=c('ROccAsp', 'REdAsp', 'FOccAsp', 
                'FEdAsp', 'RParAsp', 'RIQ', 'RSES', 'FSES', 'FIQ', 'FParAsp'))
    .6247                                                              
    .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')

  1. 如何使用pathDiagram函数直接从R绘制路径图?
  2. 如何将图形包含在SweaveRnw文件中?
  1. How do I draw a path diagram directly from R using the pathDiagram function?
  2. How do I include the graph in an Rnw file for Sweave?

推荐答案

您只需要指定一个文件名(不带扩展名!),请参见file=参数.如文档中所述,它将同时生成.dot和PDF文件(但如果仅需要graphviz输出,则设置为output.type="dot").

You just need to specify a filename (without extension!), see the file= argument. As stated in the documentation, it will generated both a .dot and PDF file (but set output.type="dot" if you only want the graphviz output).

调用上述命令后,我将在Sweave文件中使用一个简单的\includegraphics命令. (如果没有在与主文件.Rnw相同的目录中生成SEM图,则可能需要调整路径以找到图.)

I would use a simple \includegraphics command in the Sweave file, after having called the above command. (You may need to adapt the path to find the figure if you don't generate the SEM diagram in the same directory as your master .Rnw file.)

更新

给出您的评论,是的,似乎在函数调用(pathDiagram)中运行外部程序时出现问题.因此,这是生成路径图并将其包含在Sweave-> TeX文档中的一种不太理想的解决方案.

Given your comment, yes it seems there's a problem running external program from within the function call (pathDiagram). So here is a not very elegant solution to generate the path diagram and include it in your Sweave->TeX document.

这是Sweave文件(sw.rnw):

Here is the Sweave file (sw.rnw):

\documentclass{article}
\usepackage{graphicx}
\begin{document}
<<echo=TRUE>>=
library(sem)
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")
@
<<echo=FALSE>>=
system("dot -Tpdf -o fig1.pdf  sem.dot")
@

And here is the path diagram.

\begin{center}
\includegraphics{fig1}
\end{center}

\end{document}

文件sem.covsem.mod包含在以上示例中手动输入的协方差矩阵和结构模型(纯文本文件中的简单复制/粘贴).我不是很高兴必须使用capture.output(),因为我找不到从块中屏蔽其调用的方法.也许您会找到一种更好的方法来完成此操作(其想法是使用system(),并且可以在块参数中使用echo=FALSE轻松掩盖它).

The files sem.cov and sem.mod contain the covariance matrix and structural model that were both entered manually in the above example (simple copy/paste in plain text file). I'm not very happy to have to use capture.output() because I cannot find a way to mask its call from the chunk. Maybe you'll find a better way to do that (the idea is to use system(), and that can easily be masked with echo=FALSE in the chunk parameters).

我碰巧如下编译了上面的文件:

I happened to compile the above document as follows:

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

这篇关于使用Graphviz使用R包'sem'绘制路径图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆