我发现了一个非常棒的post,关于使用DiagrammeR转换lavaan输出。但是,这篇文章中使用的DiagrammeR版本是0.6版本,在尝试复制版本0.8(http://rich-iannone.github.io/DiagrammeR/docs.html)的代码时遇到了一些麻烦。
随着一些非常小的变化,代码就像这样
先用lavaan填写SEM
library("stringr")
library("lavaan")
library("DiagrammeR")
library("dplyr")
library("semPlot")
model
# latent variables
ind60 =~ x1 + x2 + x3
dem60 =~ y1 + y2 + y3 + y4
dem65 =~ y5 + y6 + y7 + y8
# regressions
dem60 ~ ind60
dem65 ~ ind60 + dem60
# residual covariances
y1 ~~ y5
y2 ~~ y4 + y6
y3 ~~ y7
y4 ~~ y8
y6 ~~ y8
'
fit
semPaths(fit, intercept = FALSE, whatLabel = "est",
residuals = FALSE, exoCov = FALSE)现在这是post中提出的代码
paths %
parameterestimates %>%
select(lhs, op, rhs, est)
# Latent variables are left-hand side of "=~" lines
latent %
filter(op == "=~") %>%
select(nodes = lhs) %>%
distinct %>%
mutate(shape = "circle")
# Manifest variables are not latent variables
`%not_in%`
manifest %
filter(op != "~1", lhs %not_in% latent$nodes) %>%
select(nodes = lhs) %>%
distinct %>%
mutate(shape = "square")
# Nodes are prepared
node_set
# Edges will be labeled by the parameter estimates
all_paths %
filter(op != "~1") %>%
mutate(label = round(est, 2)) %>%
select(-est)
# Factor loadings are the paths in the "=~" lines
loadings %
filter(op == "=~") %>%
mutate(edge_from = lhs, edge_to = rhs, style = "dashed") %>%
select(edge_from, edge_to, style, label)
regressions %
filter(op == "~") %>%
rename(edge_to = lhs, edge_from = rhs) %>%
mutate(style = "solid") %>%
select(edge_from, edge_to, style, label)
edge_set
错误来到这里
# Combine edges and nodes
my_graph
nodes = node_set,
edges_df = edge_set,
graph_attrs = c("ranksep = 1"))
# We can plot the graph directly
graphviz_render(my_graph)在DiagrammeR的文档中,据说“重命名了graphviz_graph和graphviz_render到create_graph和render_graph”。
但是,进行此更改不起作用
my_graph
nodes = node_set,
edges_df = edge_set,
graph_attrs = c("ranksep = 1"))任何想法为什么?
其他问题。在这篇文章中,这段代码似乎没有绘制残差协方差。任何想法如何绘制DiagrammeR的残差共变量?
谢谢