【问题标题】:Loop or Function in R that for Multiple Analyses with Similar VariablesR中的循环或函数,用于具有相似变量的多个分析
【发布时间】:2020-03-28 21:18:52
【问题描述】:

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

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 错误:数据集中缺少观察到的变量:tst3 tst1 tst2

很明显,R 没有认识到它应该在“tst”所在的位置插入“x”或“y”。有没有人有关于如何让它运行的建议?谢谢。

【问题讨论】:

    标签: r lapply r function loops r-lavaan


    【解决方案1】:

    您的问题是您实际上并没有用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 的列表。要将其“移动”到全局环境中,您还可以使用 list2env(output, globalenv())

    【讨论】:

    • 这很棒,还修复了很多其他问题。唯一剩下的问题:即使在全局赋值 (
    • 你可以用 assign 来做这件事,但更 Rtistic 的做法是在命名列表中返回 tst,或者在运行后命名 lapply 的结果setNames.
    • 不能说我是 Rtistic——我对此很陌生——但我想出了如何使用 assign 来做到这一点。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-17
    • 2019-03-15
    • 2023-03-26
    • 2015-02-18
    • 2021-04-25
    • 2018-07-09
    相关资源
    最近更新 更多