R/mplusSaveDataReadeR.R

Defines functions mplusSaveDataReadeR

Documented in mplusSaveDataReadeR

#' MPlus Save Data Reader
#'
#' @param modelFactorsList List (elements = models) of vectors, each containing the factors for each model
#'                 (use addModelContent function; see documentation using ?TabulationAutomation::addModelContent)
#' @param MPlusOutputDirectory Where are the MPlus syntax template and Mplus .inp and .out files?
#'
#' @return
#' @export
mplusSaveDataReadeR <- function(modelFactorsList, MPlusOutputDirectory = getwd()) {

  require(rlist, quietly = TRUE); require(MplusAutomationFork); require(rhdf5)

  mplusOutList <- MPlusAutomationFork::readModels(MPlusOutputDirectory) # read models in subdirectory (sourceFolder)

  mplusOutList <- readModels(MPlusOutputDirectory) # read models in subdirectory (sourceFolder)

  names(mplusOutList) <- gsub(names(mplusOutList), pattern = '.out', replacement = '') # remove .out so that named after models

  factorDataList <- vector(mode = 'list') # empty list

  for (m in 1:length(mplusOutList)) { # read all model factor score data and store in df named after model and cfa
    ## store model data in generic "data"
    eval(parse(text = paste0('mplus <- mplusOutList[[\'', names(mplusOutList)[m], '\']][[\'savedata\']]')))
    if (length(mplus) > 1) { # if savedata output was not empty
      preds <- modelFactorsList[[names(mplusOutList)[[m]]]] # factor/latent variables
      data <- mplus[, colnames(mplus) %in% preds] # store just scores for factors/latent variables
      factorDataList <- rlist::list.append(factorDataList, data)
    }
  }

}
enaY15/TabulationAutomation documentation built on March 18, 2020, 8:35 p.m.