使用 DiagrammeR 绘制路径图 (SEM)
Using DiagrammeR for Path Diagrams (SEM)
我正在测试用于结构方程模型可视化的新 DiagrammeR 软件包的功能。
我的目标是,得到这样的情节:
也许我需要找到关于如何指定边和节点方向的资源,因为 - DiagrammeR
- 我现在只能做这样的情节:
devtools::install_github('rich-iannone/DiagrammeR')
library('DiagrammeR')
#----------
test<-grViz("
digraph CFA{
# latent variables
node [shape=circle]
latent_a [group='a'];
latent_b [group='b'];
#regressions
latent_a -> latent_b [label='0.279'];
#measurement model for latent a
subgraph A{
node [shape=box]
ind_1;
ind_2;
ind_3;
latent_a -> ind_1 [label='0.636'];
latent_a -> ind_2 [label='0.825'];
latent_a -> ind_3 [label='0.829'];
}
#measurement model for latent b
subgraph B{
node [shape=box]
ind_4 ;
ind_5 ;
latent_b -> ind_4 [label='1.027'];
latent_b -> ind_5 [label='0.626'];
}
#residuals
node [shape=circle]
e1 ;
e4 ;
e5 ;
e6 ;
e1 -> ind_1;
e4 -> ind_4;
e5 -> ind_5;
e6 -> latent_b;
#residual covariances
ind_1 -> ind_2 [dir=both];
}
")
test
如有任何提示,我将不胜感激。
PS:也许我的 DiagrammeR
-代码看起来很有趣。我试着用同样的方式写它,在 lavaan 中指定了一个模型。也许将来会有一种方法可以在两个包之间进行转换。我认为,这是可能的。
我想出了一些有点手动的东西,但我确信可以通过制作适当的 R 函数来生成 DOT 代码。这是手动解决方案:
devtools::install_github("rich-iannone/DiagrammeR")
library(DiagrammeR)
grViz("
digraph SEM {
graph [layout = neato,
overlap = true,
outputorder = edgesfirst]
node [shape = rectangle]
a [pos = '-4,1!', label = 'e1', shape = circle]
b [pos = '-3,1!', label = 'ind_1']
c [pos = '-3,0!', label = 'ind_2']
d [pos = '-3,-1!', label = 'ind_3']
e [pos = '-1,0!', label = 'latent a', shape = ellipse]
f [pos = '1,0!', label = 'latent b', shape = ellipse]
g [pos = '1,1!', label = 'e6', shape = circle]
h [pos = '3,1!', label = 'ind_4']
i [pos = '3,-1!', label = 'ind_5']
j [pos = '4,1!', label = 'e4', shape = circle]
k [pos = '4,-1!', label = 'e5', shape = circle]
a->b
e->b [label = '0.6']
e->c [label = '0.6']
e->d [label = '0.6']
e->f [label = '0.321', headport = 'w']
g->f [tailport = 's', headport = 'n']
d->c [dir = both]
f->h [label = '0.6', tailport = 'ne', headport = 'w']
f->i [label = '0.6']
j->h
k->i
}
")
我正在测试用于结构方程模型可视化的新 DiagrammeR 软件包的功能。
我的目标是,得到这样的情节:
也许我需要找到关于如何指定边和节点方向的资源,因为 - DiagrammeR
- 我现在只能做这样的情节:
devtools::install_github('rich-iannone/DiagrammeR')
library('DiagrammeR')
#----------
test<-grViz("
digraph CFA{
# latent variables
node [shape=circle]
latent_a [group='a'];
latent_b [group='b'];
#regressions
latent_a -> latent_b [label='0.279'];
#measurement model for latent a
subgraph A{
node [shape=box]
ind_1;
ind_2;
ind_3;
latent_a -> ind_1 [label='0.636'];
latent_a -> ind_2 [label='0.825'];
latent_a -> ind_3 [label='0.829'];
}
#measurement model for latent b
subgraph B{
node [shape=box]
ind_4 ;
ind_5 ;
latent_b -> ind_4 [label='1.027'];
latent_b -> ind_5 [label='0.626'];
}
#residuals
node [shape=circle]
e1 ;
e4 ;
e5 ;
e6 ;
e1 -> ind_1;
e4 -> ind_4;
e5 -> ind_5;
e6 -> latent_b;
#residual covariances
ind_1 -> ind_2 [dir=both];
}
")
test
如有任何提示,我将不胜感激。
PS:也许我的 DiagrammeR
-代码看起来很有趣。我试着用同样的方式写它,在 lavaan 中指定了一个模型。也许将来会有一种方法可以在两个包之间进行转换。我认为,这是可能的。
我想出了一些有点手动的东西,但我确信可以通过制作适当的 R 函数来生成 DOT 代码。这是手动解决方案:
devtools::install_github("rich-iannone/DiagrammeR")
library(DiagrammeR)
grViz("
digraph SEM {
graph [layout = neato,
overlap = true,
outputorder = edgesfirst]
node [shape = rectangle]
a [pos = '-4,1!', label = 'e1', shape = circle]
b [pos = '-3,1!', label = 'ind_1']
c [pos = '-3,0!', label = 'ind_2']
d [pos = '-3,-1!', label = 'ind_3']
e [pos = '-1,0!', label = 'latent a', shape = ellipse]
f [pos = '1,0!', label = 'latent b', shape = ellipse]
g [pos = '1,1!', label = 'e6', shape = circle]
h [pos = '3,1!', label = 'ind_4']
i [pos = '3,-1!', label = 'ind_5']
j [pos = '4,1!', label = 'e4', shape = circle]
k [pos = '4,-1!', label = 'e5', shape = circle]
a->b
e->b [label = '0.6']
e->c [label = '0.6']
e->d [label = '0.6']
e->f [label = '0.321', headport = 'w']
g->f [tailport = 's', headport = 'n']
d->c [dir = both]
f->h [label = '0.6', tailport = 'ne', headport = 'w']
f->i [label = '0.6']
j->h
k->i
}
")