【问题标题】:How to Add Title To SEM Plot in R如何在 R 中为 SEM 图添加标题
【发布时间】:2021-10-17 03:35:55
【问题描述】:

这就是我的情节:

semPaths(twofac7items_b,"std","est", rotation = 2,
         style = "lisrel",nCharNodes = 0,nodeNames=nodeNames,layout = "tree2",
         filetype = "pdf", width = 8, height = 6, filename = "Two Factor",
         residScale = 20)

看起来像这样:

我想要的是这个:

这是 dput(head)。我所拥有的一切都被重新标记为焦虑和抑郁,所以不确定这会有多大用处:

structure(list(q01 = c(2, 1, 2, 3, 2, 2), q02 = c(1, 1, 3, 1, 
1, 1), q03 = c(4, 4, 2, 1, 3, 3), q04 = c(2, 3, 2, 4, 2, 2), 
    q05 = c(2, 2, 4, 3, 2, 4), q06 = c(2, 2, 1, 3, 3, 4), q07 = c(3, 
    2, 2, 4, 3, 4), q08 = c(1, 2, 2, 2, 2, 2), q09 = c(1, 5, 
    2, 2, 4, 4), q10 = c(2, 2, 2, 4, 2, 3), q11 = c(1, 2, 3, 
    2, 2, 2), q12 = c(2, 3, 3, 2, 3, 4), q13 = c(2, 1, 2, 2, 
    3, 3), q14 = c(2, 3, 4, 3, 2, 3), q15 = c(2, 4, 2, 3, 2, 
    5), q16 = c(3, 3, 3, 3, 2, 2), q17 = c(1, 2, 2, 2, 2, 3), 
    q18 = c(2, 2, 3, 4, 3, 5), q19 = c(3, 3, 1, 2, 3, 1), q20 = c(2, 
    4, 4, 4, 4, 5), q21 = c(2, 4, 3, 4, 2, 3), q22 = c(2, 4, 
    2, 4, 4, 1), q23 = c(5, 2, 2, 3, 4, 4)), variable.labels = c(q01 = "Statistics makes me cry", 
q02 = "My friends will think I'm stupid for not being able to cope with SPSS", 
q03 = "Standard deviations excite me", q04 = "I dream that Pearson is attacking me with correlation coefficients", 
q05 = "I don't understand statistics", q06 = "I have little experience of computers", 
q07 = "All computers hate me", q08 = "I have never been good at mathematics", 
q09 = "My friends are better at statistics than me", q10 = "Computers are useful only for playing games ", 
q11 = "I did badly at mathematics at school", q12 = "People try to tell you that SPSS makes statistics easier to understand but it doesn't", 
q13 = "I worry that I will cause irreparable damage because of my incompetenece with computers", 
q14 = "Computers have minds of their own and deliberately go wrong whenever I use them", 
q15 = "Computers are out to get me", q16 = "I weep openly at the mention of central tendency", 
q17 = "I slip into a coma whenever I see an equation", q18 = "SPSS always crashes when I try to use it", 
q19 = "Everybody looks at me when I use SPSS", q20 = "I can't sleep for thoughts of eigen vectors", 
q21 = "I wake up under my duvet thinking that I am trapped under a normal distribtion", 
q22 = "My friends are better at SPSS than I am", q23 = "If I'm good at statistics my friends will think I'm a nerd"
), codepage = 65001L, row.names = c(NA, 6L), class = "data.frame")

这是必要的 SEM 模型:

m4b <- 'Depression =~ q01+ q03 + q04 + q05 + q08
        Anxiety =~ q06 + q07' 
twofac7items_b <- cfa(m4b, data=dat,std.lv=TRUE) 
summary(twofac7items_b,fit.measures=TRUE,standardized=TRUE)

【问题讨论】:

  • 你试过title(main = ...)吗?
  • @Teufelkoenig;如果您不直接保存到外部文件,rawr 的建议将起作用。所以改为使用pdf(...); semPaths(...); title("www"); dev.off(),其中... 是相关参数。
  • 您上面评论中的代码不起作用,因为您没有打开pdf设备并在关闭它后添加了标题。这些方面的东西应该可以工作:pdf("output.pdf") ; semPaths(twofac7items_b,"std","est", rotation = 2, style = "lisrel",nCharNodes = 0,nodeNames=nodeNames,layout = "tree2", residScale = 20); title("SEM DEPRESSION") ; dev.off()
  • 这个小例子能否正确生成pdf:pdf("test.pdf"); semPaths(lm(mpg ~ wt, mtcars), intAtSide=TRUE); dev.off()
  • 根据您的描述,我不太确定您在看什么。我看到一个包含三个节点的图表,分别是 mpg、wt 和截距。也许您的 pdf 查看器有什么不好的地方。您可以尝试运行我上一条评论中的代码,但使用png;那是用png("test.png") 代替pdf

标签: r sem semplot


【解决方案1】:

背景

最后,当我更加精通 R 时,我把它搁置了一段时间。我没有试图让事情变得过于复杂,而是决定制作一个非常简单的 SEM 路径图,然后应用前面 cmets 中所说的内容来解决问题。

解决方案

因此,我一直遇到的主要问题是获得要映射的标题。出于某种原因,我无法理解是什么导致了这个问题......直到我弄清楚了打印出情节的操作顺序。所以这基本上就是我所做的。首先,我使用了一个运行良好的数据框架,并根据旧的 lavaan 手册编写了一个模型:

# Write model:
model <- '
ind60 =~ x1 + x2 + x3
dem60 =~ y1 + y2 + y3 + y4
dem65 =~ y5 + y6 + y7 + y8

dem60 ~ ind60
dem65 ~ ind60 + dem60

y1 ~~ y5
y2 ~~ y4 + y6
y3 ~~ y7
y4 ~~ y8
y6 ~~ y8
'
# Fit model:
fit <- sem(model,
           data = PoliticalDemocracy)
# Summarize model:
summary(fit,
        standardized = T)

semPaths(fit,
         "std",
         "est",
         style = "lisrel",
         theme = "colorblind",
         rotation = 2,
         legend.cex = .5,
         layout = "tree2")

现在这是主要部分...首先,我刚刚打印了从那里开始的情节:

那时我在 R 绘图查看器中运行绘图后,将标题命令 直接

# Manually add title to generated plot
title("Democracy")

从那里,我刚刚用这个下拉列表打印了情节:

我对 R 很陌生,以至于我一直试图先输出文件来搞砸这个。通过更改脚本中的顺序,我得到了我的情节的 pdf 文件!我很高兴我终于能够完成这项工作。感谢 Rawr 和 User20650 尝试与我合作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-12
    • 1970-01-01
    • 2018-08-10
    • 2013-02-19
    • 1970-01-01
    • 2014-10-04
    相关资源
    最近更新 更多