R 中用于使用相似变量进行多项分析的循环或函数

Loop or Function in R that for Multiple Analyses with Similar Variables

相对较新,我正在尝试弄清楚如何运行 对两组不同的变量进行相同的分析。这不是我的数据,但这是我的代码现在的样子:

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"。我也试过用循环来做(这是我在其他统计包中使用的方法)但这也不起作用,我的理解是 如果我能想到的话会更好地用于此目的出来了。

到目前为止,我收到的错误消息是:

lavaan ERROR: missing observed variables in dataset: tst3 tst1 tst2

很明显,R 没有意识到它应该在 "tst" 所在的位置插入 "x" 或 "y"。有人对如何将其发送到 运行 有什么建议吗?谢谢

你的问题是你实际上没有用 xy 代替 tst。参见例如

library(stringr)

anexample <- function(tst) {
  model <- 'tst3 ~ + tst1 + tst2'
  model <- str_replace_all(model, 'tst', tst)
  return(model)
  }

lapply(listoftype, FUN = anexample)
[[1]]
[1] "x3 ~ + x1 + x2"

[[2]]
[1] "y3 ~ + y1 + y2"

因此,以下应该有效:

anexample <- function(tst) {
  model <- 'tst3 ~ + tst1 + tst2'
  model <- str_replace_all(model, 'tst', tst)

  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)

要获得具有适当名称的结果,请查看以下示例:

anexample <- function(tst) {
  model <- str_replace_all('tst3 ~ + tst1 + tst2', 'tst', tst)
  runmodel <- lavaan::sem(model, 
                          data = PoliticalDemocracy,
                          missing = "default",
                          estimator = "ML",
                          se = "default",
  )
  modelsummary <-lavaan::summary(runmodel, fit.measures=FALSE)
  return(as.data.frame(modelsummary))
}

output <- setNames(lapply(listoftype, anexample), listoftype)
str(output)
List of 2
 $ x:'data.frame':  6 obs. of  8 variables:
  ..$ PE.lhs   : chr [1:6] "x3" "x3" "x3" "x1" ...
  ..$ PE.op    : chr [1:6] "~" "~" "~~" "~~" ...
... 
 $ y:'data.frame':  6 obs. of  8 variables:
  ..$ PE.lhs   : chr [1:6] "y3" "y3" "y3" "y1" ...
  ..$ PE.op    : chr [1:6] "~" "~" "~~" "~~" ...
...

这为您提供了一个包含 xy 的列表。要将'move'这个放到全局环境中,可以另外使用list2env(output, globalenv()).