【发布时间】:2020-03-28 21:18:52
【问题描述】:
r 相对较新,我正试图弄清楚如何对两组不同的变量进行相同的分析。这不是我的数据,但这是我的代码现在的样子:
library(lavaan)
names(PoliticalDemocracy)
listoftype <- c("x", "y")
anexample <- function(tst) {
model <- '
tst3 ~
+ tst1
+ tst2
'
runmodel <- lavaan::sem(model,
data = PoliticalDemocracy,
missing = "default",
estimator = "ML",
se = "default",
)
lavaan::summary(runmodel, fit.measures=FALSE)
modelsummary <-lavaan::summary(runmodel, fit.measures=FALSE)
write(paste(utils::capture.output(summary(runmodel)),
collapse = "\n"), file = paste0("output_",tst.txt))
tst <-as.data.frame(modelsummary)
}
lapply(listoftype, FUN = anexample)
您可能会弄清楚我想要做什么,但基本想法是我尝试使用 x3、x2 和 x1 运行第一个分析,然后使用 y3、y2 和 y1 运行第二个分析.我试图在有“tst”的地方插入“x”或“y”。我也尝试过使用循环(这是我将在其他统计包中使用的),但这也不起作用,我的理解是lapply 会更好地用于此目的,如果我能弄明白的话.
到目前为止,我收到的错误消息是:
lavaan 错误:数据集中缺少观察到的变量:tst3 tst1 tst2
很明显,R 没有认识到它应该在“tst”所在的位置插入“x”或“y”。有没有人有关于如何让它运行的建议?谢谢。
【问题讨论】:
标签: r lapply r function loops r-lavaan