R/extractParameters.R

Defines functions extractModelParameters extractParameters_1file extractClassEqualityTests extractESEMStandardized extractParameters_1section extractParameters_1chunk

Documented in extractModelParameters extractParameters_1chunk extractParameters_1file extractParameters_1section

#' Extract Parameters for One Chunk
#'
#' Helper function for extractModelParameters. Used to parse each subsection of
#' output within a given file and given results section (e.g., stdyx section) There
#' will be many chunks if latent classes, multiple groups, multilevel features are used.
#'
#' @param filename name of Mplus output file being processed
#' @param thisChunk character vector of output chunk from which to extract parameter values
#' @param columnNames character vector of expected column names for output chunk
#' @return A data frame (or matrix?)
#' @keywords internal
extractParameters_1chunk <- function(filename, thisChunk, columnNames, sectionName) {
  if (isEmpty(thisChunk)) stop("Missing chunk to parse.\n  ", filename)
  if (isEmpty(columnNames)) stop("Missing column names for chunk.\n  ", filename)
  if (missing(sectionName)) sectionName <- "" #right now, just use sectionName for R-SQUARE section, where there are no subheaders per se
  
  # R-SQUARE tables contain observed and latent rows without parameter subheaders.
  if (sectionName == "r2") {
    values <- vapply(strsplit(trimws(thisChunk), "\\s+"), `[`, character(1L), 2L)
    is_estimate <- !is.na(suppressWarnings(mplus_as.numeric(values))) |
      grepl("^(?:Undefined|NA|\\*{2,})$", values, ignore.case = TRUE, perl = TRUE)
    thisChunk <- thisChunk[is_estimate]
    if (length(thisChunk) == 0L) return(NULL)
    convertMatches <- data.frame(element=0L, keyword="R-SQUARE", varname=NA_character_,
                                 operator=NA_character_, endline=length(thisChunk))
  } else if (sectionName %in% c("probability.scale", "ci.probability.scale")) {
    #Probability scale output tends to fall in one uniform chunk. If it has header (e.g., Latent Class 1), these are handled upstream before parsing into chunks processed here.
    convertMatches <- data.frame(element=1, keyword="Probability.Scale", varname=NA_character_, operator=NA_character_, endline=length(thisChunk))
  } else {
    # okay to match beginning and end of line because strip.white used in scan
    # Mar2025: switch to optimized friendlyGregexpr for speed
    convertMatches <- friendlyGregexpr("^\\s*((Means|Thresholds|Intercepts|Variances|Item Difficulties|Item Locations|Item Categories|Item Guessing|Upper Asymptote|Residual Variances|Base Hazard Parameters|New/Additional Parameters|Scales|Dispersion|Steps)|([\\w_\\d+\\.#\\&\\^]+\\s+(BY|WITH|ON|\\|))|([\\w_\\d+\\.#\\&\\^]+\\s*\\|\\s*[\\w_\\d+\\.#\\&\\^]+\\s*(BY|WITH|ON)))\\s*$", thisChunk, perl=TRUE)
    
    # sometimes chunks have no parameters because they are empty. e.g., stdyx for ex7.30
    # in this case, return null
    if (is.null(convertMatches)) return(NULL)
    # if (nrow(convertMatches)==0L) return(NULL)
    
    # Mar2025: for speed, switch to base R regex and lapply, avoiding repeated calls to data.frame
    #develop a data.frame that divides into keyword matches versus variable matches
    results <- lapply(seq_len(nrow(convertMatches)), function(i) {
      row <- convertMatches[i, ]
      match <- row$tag
      
      # no variable names or operators for elements such as means
      if (match %in% c("Means", "Thresholds", "Intercepts", "Variances", "Residual Variances", "Base Hazard Parameters", 
                       "New/Additional Parameters", "Scales", "Item Difficulties", "Item Locations", "Item Categories", 
                       "Item Guessing", "Upper Asymptote", "Dispersion", "Steps")) {
        return(c(keyword = make.names(match), slope = NA_character_, varname = NA_character_, operator = NA_character_))
      } 
      
      # for regmatches, the first element is always the entire match, and subsequent elements are capturing groups
      # paramHeader operator, like POS_AFFECT BY
      param_match <- regmatches(match, regexec("^\\s*([\\w_\\d+\\.#\\&\\^]+)\\s+(BY|WITH|ON|\\|)\\s*$", match, perl=TRUE))[[1]]
      if (length(param_match) > 1L) {
        return(c(keyword = NA_character_, slope = NA_character_, varname = param_match[2], operator = param_match[3]))
      }
      
      rslope_match <- regmatches(match, regexec("^\\s*([\\w_\\d+\\.#\\&\\^]+)\\s*\\|\\s*([\\w_\\d+\\.#\\&\\^]+)\\s*(BY|WITH|ON)\\s*$", match, perl=TRUE))[[1]]
      if (length(rslope_match) > 1L) {
        return(c(keyword = NA_character_, slope = rslope_match[2], varname = rslope_match[3], operator = rslope_match[4]))
      }
      
      stop("failure to match keyword: ", match)
    })
    
    char_mat <- do.call(rbind, results)
    convertMatches <- data.frame(element=convertMatches$element, char_mat) # convert to data.frame and restore element
  }

  #convertMatches will now contain a data.frame marking the section headers for the chunk
  #example:
  #		element keyword varname operator endline
  #		        7    <NA>      FW       BY      12
  #		       13    <NA>      FW       ON      16
  
  combo_list <- lapply(seq_len(nrow(convertMatches)), function(i) {
    #define the end line for this match as the start of next match - 1
    if (i < nrow(convertMatches)) convertMatches[i,"endline"] <- convertMatches[i+1,"element"]-1L
    else convertMatches[i,"endline"] <- length(thisChunk) # or if last chunk in the section, just define as length

    #need +1 to eliminate header row from params
    paramsToParse <- thisChunk[(convertMatches[i, "element"]+1L):convertMatches[i, "endline"]]

    #Very rarely, a section header is printed, but no parameters follow. In such cases, skip to the next match
    #example:
    #  I1       |
    #
    if (all(paramsToParse=="")) return(NULL) # next
    
    #should result in a short list of params to parse (that belong to a given header i)
    #Example:
    #"U1                 0.557      0.036     15.470      0.000"
    #"U2                 0.638      0.038     16.751      0.000"
    #"U3                 0.660      0.038     17.473      0.000"
    #"U4                 0.656      0.037     17.585      0.000"

    if ((!is.na(convertMatches[i,"keyword"]) && (convertMatches[i,"keyword"] %in% c("Item.Categories", "Probability.Scale")))) { 
      #handle item categories output from IRT (e.g. ex5.5pcm.out), where the output section looks like this:
      # Item Categories
      #  U1
      #    Category 1         0.000      0.000      0.000      1.000
      #    Category 2        -0.247      0.045     -5.534      0.000
      #    Category 3         0.699      0.052     13.325      0.000
      #    Category 4        -0.743      0.057    -12.938      0.000
      #    Category 5         0.291      0.052      5.551      0.000
      paramsToParse <- parseCatOutput(paramsToParse) #reformat into U1.Cat.1 etc.
    }
  
    #define the var title outside of the chunk processing because it will apply to all rows
    if (is.na(convertMatches[i,]$keyword)) {
      slo <- convertMatches[i,"slope"] #support Mplus v8 output style S1 | Y1 ON Y1&1 (e.g., ex9.32.out) 
      prefix <- ifelse(is.null(slo) || is.na(slo), "", paste(slo, "|", sep=""))
      varTitle <- paste(prefix, convertMatches[i,"varname"], ".", convertMatches[i,"operator"], sep="")
    } else { varTitle <- as.character(convertMatches[i,"keyword"]) }
   
    splitParams <- strsplit(paramsToParse, "\\s+", perl=TRUE)

    # Handle undefined parameter values (I've only seen these in R-SQUARE second column so far)    
    if (columnNames[2L] == "est") {
      splitParams <- lapply(splitParams, function(x) {
        if (length(x) >= 2L && grepl("undefined", x[2L], ignore.case = TRUE)) { 
          x <- c(x[1L], rep("NA", length(columnNames) - 1)) #Fill in NAs for undefined params
        }
        return(x)
      })
    }
    
    #for the Significance column in 7-column Mplus output, it may be missing for a chunk (all n.s./not tested), or for a given row.
    #Handle this condition here by adding FALSE for missing 7th column and converting * to TRUE.
    if (length(columnNames) == 7L && columnNames[7L] == "sig") {
      splitParams <- lapply(splitParams, function(col) {
        lcol <- length(col)
        if (lcol == 6L) { col[7L] <- "FALSE"
        } else if (lcol == 7L && col[7L] == "*") { col[7L] <- "TRUE"
        } else if (lcol != 0) { warning("Unknown columns found for 7-column BAYES format") }
        return(col)
        
      })
    }
    
    #handle case of missing scale factors for non-categorical variables and residual variance in R-SQUARE output 
    if (length(columnNames) == 6L && (columnNames[6L] == "scale_f" || columnNames[6L] == "resid_var")) {
      splitParams <- lapply(splitParams, function(col) {
        lcol <- length(col)
        if (lcol == 5L) { col[6L] <- "NA" } #NA-fill variables without a scale factor or residual variance
        return(col)
      })
    }
    
    #similar problem for 3-column R-SQUARE output with missing residual variances or scale factors
    if (length(columnNames) == 3L && columnNames[3L] %in% c("resid_var", "scale_f")) {
      splitParams <- lapply(splitParams, function(col) {
        lcol <- length(col)
        if (lcol == 2L) { col[3L] <- "NA" } #NA-fill variables without a residual variance
        return(col)
      })
    }
    
    #rbind the split list as a data.frame
    parsedParams <- data.frame(do.call("rbind", splitParams), stringsAsFactors=FALSE)

    #use the column names detected in extractParameters_1section
    names(parsedParams) <- columnNames

    # Preserve column 1 (param) as character so variable/factor names like "F" or "T"
    # are never coerced to logical FALSE/TRUE. Convert value columns cleanly and map
    # Mplus Fortran overflow strings (*********) to NA_real_.
    for (col_idx in seq_len(ncol(parsedParams))[-1L]) {
      col_vals <- parsedParams[[col_idx]]
      col_vals[grepl("^\\*{2,}$", col_vals, perl = TRUE)] <- NA_character_
      if (identical(columnNames[col_idx], "sig")) {
        parsedParams[[col_idx]] <- as.logical(col_vals)
      } else {
        parsedParams[[col_idx]] <- suppressWarnings(mplus_as.numeric(col_vals))
      }
    }

    #add the paramHeader to the data.frame
    parsedParams$paramHeader <- varTitle

    #put the paramHeader at the front of the data.frame columns
    parsedParams <- parsedParams[,c("paramHeader", columnNames)]
    
    return(parsedParams)
  })
  
  #combined the chunks into one data.frame
  comboFrame <- do.call(rbind, combo_list)

  if (sectionName == "r2") { comboFrame$paramHeader <- NULL } #not relevant for this section
  
  #under the new strsplit strategy, just return the dataframe
  return(comboFrame)

}

#' Extract Parameters for One Section
#'
#' To do: add details
#'
#' @param filename name of Mplus output file being processed
#' @param modelSection name of model output section being processed
#' @param sectionName name of output type to search for (e.g., "model_results") 
#' @return A list of parameters
#' @keywords internal
#' @examples
#' \dontrun{
#'   #a few examples of files to parse
#'   #mg + lc. Results in latent class pattern, not really different from
#'   #         regular latent class matching. See Example 7.21
#'   #mg + twolevel. Group is top, bw/wi is 2nd. See Example 9.11
#'   #lc + twolevel. Bw/wi is top, lc is 2nd. See Example 10.1.
#'   #               But categorical latent variables is even higher
#'   #test cases for more complex output: 7.21, 9.7, 9.11, 10.1
#' }
extractParameters_1section <- function(filename, modelSection, sectionName) {
  #extract model parameters for a given model results section. A section contains complete output for all parameters of a given type
  #(unstandardized, ci, stdyx, stdy, or std) for a single file.
  #section name is used to name the list element of the returned list

  #first trim all leading and trailing spaces (new under strip.white=FALSE)
  modelSection <- gsub("(^\\s+|\\s+$)", "", modelSection, perl=TRUE)

  #detectColumnNames sub-divides (perhaps unnecessarily) the matches based on the putative section type of the output
  #current distinctions include modification indices, confidence intervals, and model results.
  if (sectionName %in% c("ci.unstandardized", "ci.stdyx.standardized", "ci.stdy.standardized", "ci.std.standardized", "ci.odds", "ci.probability.scale")) {
    sectionType <- "confidence_intervals"
  } else if (sectionName == "irt.parameterization" || sectionName == "probability.scale") {
    #the IRT section follows from the MODEL RESULTS section, and column headers are not reprinted.
    #Same applies for RESULTS IN PROBABILITY SCALE section
    
    #Update 2020: at least for GPCM, v8.4 now reprints the header "Estimate", but does not provide se, est_se, or pval.
    #Thus, first try to detect column names, but revert to default if this fails
    columnNames <- suppressWarnings(detectColumnNames(filename, modelSection, "model_results"))
    
    #If we can't detect column names (because they're not reprinted from model results, assume a 5-column header -- heuristic
    if (is.null(columnNames)) columnNames <- c("param", "est", "se", "est_se", "pval") #default if detection fails
  } else if (sectionName == "odds") {
    columnNames <- suppressWarnings(detectColumnNames(filename, modelSection, "model_results"))
    
    #If we can't detect column names (because they're not printed), assume that it's just param and est. Happens in Mplus v8, for example.
    if (is.null(columnNames)) columnNames <- c("param", "est") #default if detection fails
  } else { 
    sectionType <- "model_results" 
  }

  if (!exists("columnNames")) { columnNames <- detectColumnNames(filename, modelSection, sectionType) }

  #return nothing if unable to detect column names (this will then get filtered out in the extractParameters_1file process
  if (is.null(columnNames)) {
    x <- data.frame() #empty
    class(x) <- c("mplus.params", "data.frame")
    attr(x, "filename") <- filename
    return(x)
  }
  #Detect model section dividers
  #These include: 1) multiple groups: Group XYZ
  #  2) latent classes: Latent Class XYZ
  #  3) two-level structure: Between Level, Within Level
  #  4) categorical latent variables: Categorical Latent Variables
  #  5) class proportions (only known class output?)

  rbind_params <- function(df1, df2) {
    if (is.null(df1) || nrow(df1) == 0L) return(df2)
    if (is.null(df2) || nrow(df2) == 0L) return(df1)
    missing_in_1 <- setdiff(names(df2), names(df1))
    missing_in_2 <- setdiff(names(df1), names(df2))
    for (nm in missing_in_1) df1[[nm]] <- NA
    for (nm in missing_in_2) df2[[nm]] <- NA
    df2 <- df2[, names(df1), drop = FALSE]
    rbind(df1, df2)
  }

  allSectionParameters <- c() #will hold extracted params for all sections
  betweenWithinMatches <- grep("^\\s*(Between (?:\\s*\\w+\\s+)*Level|Within (?:\\s*\\w+\\s+)*Level|Within-Level (Standardized Estimates|R-Square) Averaged (Over|Across) Clusters)\\s*$", modelSection, ignore.case=TRUE, perl=TRUE)
  #latent class patterns can be "1 1 3 1" or something like "C#1" (see ex8.15.out for a complicated example)
  latentClassMatches <- grep("^\\s*(Latent )*Class (Pattern )*([\\d\\w#_]+\\s*)+(\\(\\s*\\d+\\s*\\))*$", modelSection, ignore.case=TRUE, perl=TRUE)
  multipleGroupMatches <- grep("^\\s*Group \\w+(?:\\s+\\(\\d+\\))*\\s*$", modelSection, ignore.case=TRUE, perl=TRUE) #support Mplus v8 syntax Group G1 (0) with parentheses of numeric value
  catLatentMatches <- grep("^\\s*Categorical Latent Variables\\s*$", modelSection, ignore.case=TRUE)
  classPropMatches <- grep("^\\s*Class Proportions\\s*$", modelSection, ignore.case=TRUE)
  overallPartMatches <- grep("^\\s*(Results|Parameters) in the Overall Part of the Model.*$", modelSection, ignore.case=TRUE, perl=TRUE)
  classSpecificMatches <- grep("^\\s*(Results|Parameters) for Class-specific Model Parts(?:\\s+of\\s+[\\w_\\.]+)?\\s*$", modelSection, ignore.case=TRUE, perl=TRUE)
  referenceClassMatches <- grep("^\\s*Parameterization using Reference Class [0-9]+", modelSection, ignore.case=TRUE)

  topLevelMatches <- sort(c(betweenWithinMatches, latentClassMatches, multipleGroupMatches, catLatentMatches, classPropMatches, overallPartMatches, classSpecificMatches, referenceClassMatches))
  
  if (length(topLevelMatches) > 0) {

    lcNum <- NULL
    bwWi <- NULL
    groupName <- NULL
    rcNum <- NULL

    matchIndex <- 1
    for (match in topLevelMatches) {
      if (grepl("\\s*Within-Level Standardized Estimates Averaged Over Clusters\\s*", modelSection[match], perl=TRUE)) {
        bwWi <- "Within.Std.Averaged.Over.Clusters"
      } else if (match %in% betweenWithinMatches) { 
        bwWi <- sub("\\s+Level\\s*$", "", modelSection[match], perl=TRUE)
      } else if (match %in% overallPartMatches) {
        lcNum <- "Overall"
      } else if (match %in% latentClassMatches) {
        if ((pos <- regexpr("Pattern", modelSection[match], ignore.case=TRUE)) > 0) {
          #need to pull out and concatenate all numerical values following pattern
          postPattern <- trimSpace(substr(modelSection[match], pos + attr(pos, "match.length"), nchar(modelSection[match])))
          #replace any spaces with periods to create usable unique lc levels
          lcNum <- gsub("\\s+", "\\.", postPattern, perl=TRUE)
        }
        else lcNum <- sub("^\\s*(?:Latent )*Class\\s+([\\w#]+)\\s*(\\(\\s*\\d+\\s*\\))*$", "\\1", modelSection[match], perl=TRUE)
      } else if (match %in% multipleGroupMatches) groupName <- sub("^\\s*Group (\\w+)(?:\\s+\\(\\d+\\))*\\s*$", "\\1", modelSection[match], perl=TRUE)
      else if (match %in% catLatentMatches) {
        #the categorical latent variables section is truly "top level"
        #that is, it starts over in terms of bw/wi and latent classes
        #multiple groups with cat latent variables is handled by knownclass and results in a latent class
        #pattern, so don't have to worry about nullifying groupName
        lcNum <- "Categorical.Latent.Variables"
        bwWi <- NULL
      }
      else if (match %in% classPropMatches) {
        #the class proportions section is truly "top level"
        #that is, it starts over in terms of bw/wi and latent classes
        #multiple groups with cat latent variables is handled by knownclass and results in a latent class
        #pattern, so don't have to worry about nullifying groupName
        lcNum <- "Class.Proportions"
        bwWi <- NULL

        #N.B.: 15Mar2012. the parse chunk routine can't handle the class proportions output right now
        #because there is no nesting. Need to come back and fix.
        #for now, this output is just ignored.
      } else if (match %in% referenceClassMatches) {
        lcNum <- NULL
        bwWi <- NULL
        rcNum <- sub("^\\s*Parameterization using Reference Class ([0-9]+)\\s*$", "\\1", modelSection[match], perl=TRUE)
      }

      #if the subsequent top level match is more than 2 lines away, assume that there is a
      #chunk to be parsed. If it's <= 2, then assume that these are just blank lines
      chunkToParse <- FALSE
      if (matchIndex < length(topLevelMatches) &&
          (topLevelMatches[matchIndex + 1] - topLevelMatches[matchIndex]) > 2) {

        #extract all text between this match and the next one (add one to omit this header row,
        #subtract one to exclude the subsequent header row)
        thisChunk <- modelSection[(match+1):(topLevelMatches[matchIndex+1]-1)]
        chunkToParse <- TRUE
      } else if (matchIndex == length(topLevelMatches) && match+1 <= length(modelSection)) {
        #also assume that the text following the last topLevelMatch is also to be parsed
        #second clause ensures that there is some chunk below the final header.
        #this handles issues where a blank section terminates the results section, such as multilevel w/ no between
        thisChunk <- modelSection[(match+1):length(modelSection)]
        chunkToParse <- TRUE
      }
      
      if (chunkToParse == TRUE && !all(thisChunk=="")) { #omit completely blank chunks (v8 ex9.32.out)
        # In R-SQUARE output for multilevel models, we violate the usual convention of column headers that
        # are superordinate to the Within/Between distinction. This leads the chunk to contain the labels.
        # Here's an example:
        #
        # R-SQUARE
        #
        # Within Level
        #
        # Observed                                        Two-Tailed   Rate of
        # Variable        Estimate       S.E.  Est./S.E.    P-Value    Missing
        #
        # ASRREA             0.266      0.013     20.079      0.000      0.026
  
        #Thus, we need to delete these lines prior to parsing. Implement attr tagging in detectColumnNames
        if (sectionName == "r2") {
          #need to delete header lines from each chunk using the header_lines attribute
          header_lines <- modelSection[attr(columnNames, "header_lines")]
          thisChunk <- thisChunk[!thisChunk %in% header_lines]
        }
  
  
        #parse the chunk into a data.frame      
        parsedChunk <- extractParameters_1chunk(filename, thisChunk, columnNames, sectionName)
        
        if (sectionName == "ci.unstandardized" && identical(lcNum, "Categorical.Latent.Variables") &&
            is.null(bwWi) && length(betweenWithinMatches) > 0L) {
          bwWi <- "Within"
        }
  
        #only append if there are some rows
        if (!is.null(parsedChunk) && nrow(parsedChunk) > 0) {
          parsedChunk$LatentClass <- lcNum
          parsedChunk$BetweenWithin <- bwWi
          parsedChunk$Group <- groupName
          parsedChunk$ReferenceClass <- rcNum
          allSectionParameters <- rbind_params(allSectionParameters, parsedChunk)
        }
      }

      matchIndex <- matchIndex + 1
    }

  } else {
    allSectionParameters <- extractParameters_1chunk(filename, modelSection, columnNames, sectionName) #just one model section
  }

  #if any std variable is one of the returned columns, we are dealing with an old-style combined results section (i.e.,
  #standardized results are not divided into their own sections, as with newer output).
  #newer output would just have the params, est, etc.
  #for consistency with newer output, we need to parse these into individual list elements and remove from unstandardized output.
  #this is a tricky maneuver in some ways because the function may return a data.frame or a list... will have to be handled by the caller
  oldStyleColumns <- c("stdyx", "stdy", "std")
  listParameters <- list()

  if (any(oldStyleColumns %in% names(allSectionParameters))) {

    #for each standardized column present, reprocess into its own df, append to list, and remove from the df
    for (colName in oldStyleColumns[oldStyleColumns %in% names(allSectionParameters)]) {
      listParameters[[paste0(colName, ".standardized")]] <- data.frame(paramHeader=allSectionParameters$paramHeader,
          param=allSectionParameters$param, est=allSectionParameters[,colName], stringsAsFactors=FALSE)

      #also include latent class, multiple groups and bw/wi in the output
      if ("LatentClass" %in% names(allSectionParameters)) listParameters[[paste0(colName, ".standardized")]]$LatentClass <- allSectionParameters$LatentClass
      if ("Group" %in% names(allSectionParameters)) listParameters[[paste0(colName, ".standardized")]]$Group <- allSectionParameters$Group
      if ("BetweenWithin" %in% names(allSectionParameters)) listParameters[[paste0(colName, ".standardized")]]$BetweenWithin <- allSectionParameters$BetweenWithin

      allSectionParameters[[colName]] <- NULL #remove from unstandardized output

    }
    listParameters[[sectionName]] <- allSectionParameters #now that standardized removed, add remainder to the list under appropriate name
  }
  else {
    #if output only contains results of one section type (stdyx, unstandardized, etc.),
    #then return a list with a single element, which will be appended to other elements by the extractParameters_1file function
    #copy data.frame into the appropriate list element to be returned.
    listParameters[[sectionName]] <- allSectionParameters

  }

  #tag as mplusParams class
  listParameters <- lapply(listParameters, function(x) {
        class(x) <- c("mplus.params", "data.frame")
        attr(x, "filename") <- filename
        return(x)
      })
  return(listParameters)
}

# Extract Mplus 9.1 side-by-side STANDARDIZED SOLUTION for ESEM, SEFA, and DSEFA
extractESEMStandardized <- function(outfiletext, filename) {
  stdSol <- getSection("^STANDARDIZED SOLUTION$", outfiletext)
  if (is.null(stdSol) || length(stdSol) == 0L) return(NULL)

  bifactor_pat <- "^\\s*BI-FACTOR MODEL(?:\\s+FOR\\s+([\\w_.]+))?\\s*$"
  loadings_pat <- "^\\s*(?:[A-Za-z\\-]+\\s+)?ROTATED LOADINGS(?:\\s*\\([^)]*\\))?(?:\\s+FOR\\s+([\\w_.]+))?\\s*$"
  corrs_pat <- "^\\s*(?:[A-Za-z\\-]+\\s+)?FACTOR CORRELATIONS(?:\\s*\\([^)]*\\))?(?:\\s+FOR\\s+([\\w_.]+))?\\s*$"
  simp_pat <- "^\\s*[A-Za-z\\-]+\\s+SIMPLICITY\\s+([0-9.\\-*]+)\\s*$"

  # Segment stdSol into rotation units (each unit ends after its FACTOR CORRELATIONS block)
  sub_idx <- grep(paste(bifactor_pat, loadings_pat, corrs_pat, sep = "|"), stdSol, perl = TRUE)
  if (length(sub_idx) == 0L) return(NULL)

  unit_starts <- integer(0L)
  unit_groups <- character(0L)
  seen_corrs <- TRUE

  for (idx in sub_idx) {
    line <- stdSol[idx]
    is_bi <- grepl(bifactor_pat, line, perl = TRUE)
    is_load <- grepl(loadings_pat, line, perl = TRUE)
    is_corr <- grepl(corrs_pat, line, perl = TRUE)

    grp <- NA_character_
    for (pat in c(bifactor_pat, loadings_pat, corrs_pat)) {
      m <- extract_pcre_groups(line, pat)[[1L]]
      if (length(m) == 2L && nzchar(m[2L])) {
        grp <- m[2L]
        break
      }
    }

    if (is_bi || (is_load && seen_corrs) ||
        (length(unit_groups) > 0L && !identical(grp, tail(unit_groups, 1L)) && !is.na(grp))) {
      unit_starts <- c(unit_starts, idx)
      unit_groups <- c(unit_groups, grp)
      seen_corrs <- FALSE
    }
    if (is_corr) seen_corrs <- TRUE
  }

  parse_unit <- function(unit_lines) {
    simp_idx <- grep(simp_pat, unit_lines, perl = TRUE)
    simp_vals <- if (length(simp_idx) > 0L) {
      vapply(
        extract_pcre_groups(unit_lines[simp_idx], simp_pat),
        function(x) suppressWarnings(mplus_as.numeric(x[2L])),
        numeric(1L)
      )
    } else {
      numeric(0L)
    }
    clean_lines <- if (length(simp_idx) > 0L) unit_lines[-simp_idx] else unit_lines

    bi_est <- matrixExtract(clean_lines, "BI-FACTOR MODEL(?:\\s+FOR\\s+[\\w_.]+)?", filename, expect_sig = TRUE)
    bi_sig <- attr(bi_est, "sig")
    if (!is.null(bi_est)) attr(bi_est, "sig") <- NULL

    load_est <- matrixExtract(clean_lines, "(?:[A-Za-z\\-]+\\s+)?ROTATED LOADINGS.*", filename, expect_sig = TRUE)
    load_sig <- attr(load_est, "sig")
    if (!is.null(load_est)) attr(load_est, "sig") <- NULL

    corr_est <- matrixExtract(clean_lines, "(?:[A-Za-z\\-]+\\s+)?FACTOR CORRELATIONS.*", filename, expect_sig = TRUE)
    corr_sig <- attr(corr_est, "sig")
    if (!is.null(corr_est)) attr(corr_est, "sig") <- NULL

    res <- list(
      loadings = list(estimates = load_est, sig = load_sig),
      corrs = list(estimates = corr_est, sig = corr_sig)
    )
    if (!is.null(bi_est)) {
      bi_simp <- if (length(simp_vals) >= 2L) simp_vals[1L] else NA_real_
      rot_simp <- if (length(simp_vals) >= 2L) simp_vals[2L] else if (length(simp_vals) == 1L) simp_vals[1L] else NA_real_
      res$bifactor <- list(estimates = bi_est, sig = bi_sig, simplicity = bi_simp)
      if (!is.na(rot_simp)) res$simplicity <- rot_simp
    } else if (length(simp_vals) >= 1L) {
      res$simplicity <- simp_vals[1L]
    }
    res
  }

  units <- vector("list", length(unit_starts))
  unit_labels <- character(length(unit_starts))
  for (u in seq_along(unit_starts)) {
    start_l <- unit_starts[u]
    end_l <- if (u < length(unit_starts)) unit_starts[u + 1L] - 1L else length(stdSol)
    units[[u]] <- parse_unit(stdSol[start_l:end_l])
    if (!is.na(unit_groups[u])) {
      unit_labels[u] <- unit_groups[u]
    } else if (!is.null(units[[u]]$loadings$estimates)) {
      unit_labels[u] <- paste(colnames(units[[u]]$loadings$estimates), collapse = "_")
    } else {
      unit_labels[u] <- paste0("set", u)
    }
  }

  if (length(units) == 1L && is.na(unit_groups[1L])) {
    return(units[[1L]])
  }

  # Handle multi-group or multiple EFA factor sets
  if (any(!is.na(unit_groups))) {
    unique_grps <- unique(unit_groups)
    if (length(unique_grps) == length(units)) {
      names(units) <- unique_grps
      return(units)
    }
    out <- list()
    for (g in unique_grps) {
      idx_g <- which(unit_groups == g)
      if (length(idx_g) == 1L) {
        out[[g]] <- units[[idx_g]]
      } else {
        sub_list <- units[idx_g]
        names(sub_list) <- vapply(sub_list, function(x) paste(colnames(x$loadings$estimates), collapse = "_"), character(1L))
        out[[g]] <- sub_list
      }
    }
    return(out)
  }

  names(units) <- make.unique(unit_labels, sep = "_")
  units
}

# Extract Mplus 9.0+ EQUALITY TESTS OF ... ACROSS THE LATENT CLASSES
extractClassEqualityTests <- function(outfiletext, filename) {
  hdr_pat <- "^\\s*EQUALITY TESTS OF (MEANS|INTERCEPTS|THRESHOLDS|PROBABILITIES|INTERCEPTS/PROBABILITIES|MEANS/PROBABILITIES) ACROSS THE LATENT CLASSES\\s*$"
  hdr_idx <- grep(hdr_pat, outfiletext, perl = TRUE)
  if (length(hdr_idx) == 0L) return(NULL)

  param_type <- tolower(sub(hdr_pat, "\\1", outfiletext[hdr_idx[1L]], perl = TRUE))
  sec <- getSection(paste0("^\\s*", trimws(outfiletext[hdr_idx[1L]]), "\\s*$"), outfiletext)
  if (is.null(sec) || length(sec) == 0L) return(NULL)

  rows <- list()
  current_var <- NA_character_
  for (line in sec) {
    var_m <- extract_pcre_groups(line, "^([A-Za-z0-9_.$#]+)\\s*$")[[1L]]
    if (length(var_m) == 2L) {
      current_var <- var_m[2L]
      next
    }
    ov_m <- extract_pcre_groups(
      line,
      "^\\s*Overall test\\s+([0-9.\\-*]+)\\s+([0-9.\\-*]+)\\s+(\\d+)\\s*$"
    )[[1L]]
    if (length(ov_m) == 4L && !is.na(current_var)) {
      rows[[length(rows) + 1L]] <- data.frame(
        param = current_var,
        test_type = "overall",
        classA = NA_character_,
        classB = NA_character_,
        chisq = suppressWarnings(mplus_as.numeric(ov_m[2L])),
        df = as.integer(ov_m[4L]),
        pval = suppressWarnings(mplus_as.numeric(ov_m[3L])),
        stringsAsFactors = FALSE
      )
      next
    }
    pw_m <- extract_pcre_groups(
      line,
      "^\\s*Class\\s+(\\w+)\\s+vs\\.\\s+(\\w+)\\s+([0-9.\\-*]+)\\s+([0-9.\\-*]+)\\s+(\\d+)\\s*$"
    )[[1L]]
    if (length(pw_m) == 6L && !is.na(current_var)) {
      rows[[length(rows) + 1L]] <- data.frame(
        param = current_var,
        test_type = "pairwise",
        classA = pw_m[2L],
        classB = pw_m[3L],
        chisq = suppressWarnings(mplus_as.numeric(pw_m[4L])),
        df = as.integer(pw_m[6L]),
        pval = suppressWarnings(mplus_as.numeric(pw_m[5L])),
        stringsAsFactors = FALSE
      )
    }
  }

  if (length(rows) == 0L) return(NULL)
  out <- do.call(rbind, rows)
  rownames(out) <- NULL
  class(out) <- c("mplus.params", "data.frame")
  attr(out, "filename") <- filename
  attr(out, "parameter_type") <- param_type
  out
}

#' Extract Parameters for One File
#'
#' To do: add details
#'
#' @param outfiletext character vector of Mplus output file being processed
#' @param filename name of Mplus output file being processed
#' @param resultType (deprecated)
#' @param efa indicates whether the output is from an EFA model (requires some additional processing)
#' @return A list of parameters
#' @keywords internal
extractParameters_1file <- function(outfiletext, filename, resultType, efa = FALSE) {

  # if (length(grep("TYPE\\s+(IS|=|ARE)\\s+((MIXTURE|TWOLEVEL)\\s+)+EFA\\s+\\d+", outfiletext, ignore.case=TRUE, perl=TRUE)) > 0) {
  #   warning(paste0("EFA, MIXTURE EFA, and TWOLEVEL EFA files are not currently supported by extractModelParameters.\n  Skipping outfile: ", filename))
  #   return(NULL) #skip file
  # }
  
  allSections <- list() #holds parameters for all identified sections
  if (length(multisectionMatches <- grep("^\\s*MODEL RESULTS FOR .*", outfiletext, perl=TRUE, value=TRUE)) > 0L) {
    sectionNames <- make.names(sub("^\\s*MODEL RESULTS FOR\\s+(?:THE)*\\s*([\\w\\.]+)", "\\1", multisectionMatches, perl=TRUE))
    #mercifully, for now, these invariance testing outputs only appear to generate unstandardized parameters (otherwise we'd need to wrap this whole function and pass in a suffix parameter)
    unstandardizedList <- list()
    for (s in 1:length(sectionNames)) {
      unstandardizedSection <- getSection(multisectionMatches[s], outfiletext)
      if (!is.null(unstandardizedSection)) {
        unstandardizedList[[ sectionNames[s] ]] <- extractParameters_1section(filename, unstandardizedSection, "unstandardized")[[1]]
      }
    }
    allSections$unstandardized <- unstandardizedList
  } else {
    unstandardizedSection <- getSection("^MODEL RESULTS$", outfiletext)
    
    if (!is.null(unstandardizedSection)) {
      allSections <- appendListElements(allSections, extractParameters_1section(filename, unstandardizedSection, "unstandardized"))
    }
    
    alt.unstandardized <- getSection("^ALTERNATIVE PARAMETERIZATIONS FOR THE CATEGORICAL LATENT VARIABLE REGRESSION$", outfiletext)
    
    if (!is.null(alt.unstandardized)) {
      header.idx <- c(grep("Parameterization using Reference Class (\\d+)", alt.unstandardized), length(alt.unstandardized))
      
      alt.params <- llply(1:(length(header.idx)-1), function(i) {
        start.line <- header.idx[[i]]
        end.line <- header.idx[[i+1]]-1
        
        alt.section <- c(alt.unstandardized[1:4], alt.unstandardized[start.line:end.line])
        
        res <- extractParameters_1section(filename, alt.section, "alt")
        
        res[[1]]
        
      })
      
      names(alt.params) <- paste0("ref.cat.", 1:(length(header.idx)-1))
      
      allSections$unstandardized.alt <- alt.params
    }
  }
  
  standardizedSection <- getSection("^STANDARDIZED MODEL RESULTS$", outfiletext)

  if (!is.null(standardizedSection)) {
    # check to see if standardized results are divided by standardization type (new format)

    # probably somewhat kludgy to use the blanklines code here, but it gets the job done
    # ultimately probably better to search for the three sections, split them, etc.
    # gregexpr("STD[YX]*Standardization", capsLine, perl=TRUE)

    stdYXSection <- getSection_Blanklines("^STDYX Standardization$", standardizedSection)
    if (!is.null(stdYXSection)) {
      allSections <- appendListElements(allSections, extractParameters_1section(filename, stdYXSection, "stdyx.standardized"))
    }

    stdYSection <- getSection_Blanklines("^STDY Standardization$", standardizedSection)
    if (!is.null(stdYSection)) {
      allSections <- appendListElements(allSections, extractParameters_1section(filename, stdYSection, "stdy.standardized"))
    }

    stdSection <- getSection_Blanklines("^STD Standardization$", standardizedSection)
    if (!is.null(stdSection)) {
      allSections <- appendListElements(allSections, extractParameters_1section(filename, stdSection, "std.standardized"))
    }
    
    #if all individual standardized sections are absent, but the standardized section is present, must be old-style
    #combined standardized section (affects WLS and MUML, too). Extract and process old section.
    if (all(is.null(stdYXSection), is.null(stdYSection), is.null(stdSection))) {
      # this section name should never survive the call
      allSections <- appendListElements(allSections, extractParameters_1section(filename, standardizedSection, "standardized"))
    }

  }

  # Extract standalone R-SQUARE even when STANDARDIZED MODEL RESULTS is absent (e.g., WLSMV / categorical models)
  r2Section <- getSection("^R-SQUARE$", outfiletext)
  if (!is.null(r2Section)) {
    allSections <- appendListElements(allSections, extractParameters_1section(filename, r2Section, "r2"))
  }

  #Mplus v8 within-level standardized model results by cluster (ex9.32.out)
  wiclusterSections <- grep("WITHIN-LEVEL STANDARDIZED MODEL RESULTS FOR CLUSTER \\d+", outfiletext, perl=TRUE)
  if (length(wiclusterSections) > 0L) {
    wiclusterSection <- lapply(wiclusterSections, function(w) {
          cs <- getSection(trimSpace(outfiletext[w]), outfiletext)
          clist <- list()
          
          stdYXSection <- getSection_Blanklines("^STDYX Standardization$", cs)
          if (!is.null(stdYXSection)) { clist <- appendListElements(clist, extractParameters_1section(filename, stdYXSection, "stdyx.standardized")) }
          
          stdYSection <- getSection_Blanklines("^STDY Standardization$", cs)
          if (!is.null(stdYSection)) { clist <- appendListElements(clist, extractParameters_1section(filename, stdYSection, "stdy.standardized")) }
          
          stdSection <- getSection_Blanklines("^STD Standardization$", cs)
          if (!is.null(stdSection)) { clist <- appendListElements(clist, extractParameters_1section(filename, stdSection, "std.standardized")) }
          
          r2Section <- getSection_Blanklines("^WITHIN-LEVEL R-SQUARE FOR CLUSTER \\d+$", cs)
          if (!is.null(r2Section)) { clist <- appendListElements(clist, extractParameters_1section(filename, r2Section, "r2")) }
          
          #add cluster number to each list
          clist <- lapply(clist, function(el) { 
                el$cluster <- as.numeric(sub("\\s*WITHIN-LEVEL STANDARDIZED MODEL RESULTS FOR CLUSTER (\\d+)\\s*", "\\1", outfiletext[w], perl=TRUE))
                return(el)
              })
          
          return(clist)          
        })
    
    #glue together each subelement into its own aggregated list
    wiclusterResults <- list()
    wiclusterResults$stdyx.standardized <- do.call(rbind, lapply(wiclusterSection, "[[", "stdyx.standardized"))
    wiclusterResults$stdy.standardized <- do.call(rbind, lapply(wiclusterSection, "[[", "stdy.standardized"))
    wiclusterResults$std.standardized <- do.call(rbind, lapply(wiclusterSection, "[[", "std.standardized"))
    wiclusterResults$r2 <- do.call(rbind, lapply(wiclusterSection, "[[", "r2"))
    
    allSections <- appendListElements(allSections, list(wilevel.standardized=wiclusterResults)) 
  }
  
  #two-parameter IRT output
  irtSection <- getSection("^IRT PARAMETERIZATION( IN TWO-PARAMETER (PROBIT|LOGISTIC) METRIC)*$", outfiletext)
  if (!is.null(irtSection)) {
    hasprobitlogit <- FALSE
    if (grepl("where the (probit|logit)", irtSection[1L], ignore.case=TRUE, perl=TRUE)) {
      #parse what the logit or probit is
      hasprobitlogit <- TRUE
      probitLogit <- tolower(sub("^\\s*where the (probit|logit) is.*$", "\\1", irtSection[1L], ignore.case=TRUE, perl=TRUE))
      def <- tolower(sub("^\\s*where the (?:probit|logit) is\\s+(.*)$", "\\1", irtSection[1L], ignore.case=TRUE, perl=TRUE))
      irtSection <- irtSection[2:length(irtSection)] #drop line "WHERE THE LOGIT IS 1.7*DISCRIMINATION*(THETA - DIFFICULTY)"
    }
    irtParsed <- extractParameters_1section(filename, irtSection, "irt.parameterization")
    if (hasprobitlogit) { attr(irtParsed[["irt.parameterization"]], probitLogit) <- def } #add probit/logit definition as attribute
    allSections <- appendListElements(allSections, irtParsed)
  }

  probSection <- getSection("^RESULTS IN PROBABILITY SCALE$", outfiletext)
  if (!is.null(probSection)) {
    probParsed <- extractParameters_1section(filename, probSection, "probability.scale")
    #slight modification of typical output for $parameters to put category in separate column
    probParsed[[1]] <- format_probability_parameters(probParsed[[1]])
    allSections <- appendListElements(allSections, probParsed)
  }
  
  align_rbind_sections <- function(tables) {
    tables <- Filter(function(x) !is.null(x) && nrow(x) > 0L, tables)
    if (length(tables) == 0L) return(NULL)
    if (length(tables) == 1L) return(tables[[1L]])
    all_cols <- unique(unlist(lapply(tables, names)))
    aligned <- lapply(tables, function(df) {
      for (nm in setdiff(all_cols, names(df))) df[[nm]] <- NA
      df[, all_cols, drop = FALSE]
    })
    out <- do.call(rbind, aligned)
    rownames(out) <- NULL
    class(out) <- c("mplus.params", "data.frame")
    attr(out, "filename") <- filename
    out
  }

  # odds ratios (Mplus 9.1 may emit separate FOR OBSERVED VARIABLES and FOR LATENT CLASS VARIABLES sections)
  oddsHeaders <- grep("^\\s*LOGISTIC REGRESSION ODDS RATIO RESULTS.*$", outfiletext, perl=TRUE, value=TRUE)
  if (length(oddsHeaders) > 0L) {
    oddsTables <- lapply(oddsHeaders, function(hdr) {
      sec <- getSection(paste0("^\\s*", trimws(hdr), "\\s*$"), outfiletext)
      if (is.null(sec)) return(NULL)
      tbl <- extractParameters_1section(filename, sec, "odds")[["odds"]]
      if (!is.null(tbl) && nrow(tbl) > 0L && length(oddsHeaders) > 1L &&
          grepl("FOR LATENT CLASS VARIABLES", hdr, fixed = TRUE) &&
          !"LatentClass" %in% names(tbl)) {
        tbl$LatentClass <- "Categorical.Latent.Variables"
      }
      tbl
    })
    combinedOdds <- align_rbind_sections(oddsTables)
    if (!is.null(combinedOdds)) allSections$odds <- combinedOdds
  }

  #confidence intervals for usual output, credibility intervals for bayesian output
  ciSection <- getSection("^(CONFIDENCE INTERVALS OF MODEL RESULTS|CREDIBILITY INTERVALS OF MODEL RESULTS)$", outfiletext)
  if (!is.null(ciSection)) {
    # capture the header line to be reused by the odds section, if relevant (where the header is not reprinted)
    hline <- detectColumnNames(filename, ciSection, "confidence_intervals")
    allSections <- appendListElements(allSections, extractParameters_1section(filename, ciSection, "ci.unstandardized"))
  }

  ciStdSection <- getSection("^(CONFIDENCE INTERVALS OF STANDARDIZED MODEL RESULTS|CREDIBILITY INTERVALS OF STANDARDIZED MODEL RESULTS)$", outfiletext)
  if (!is.null(ciStdSection)) {
    stdsections <- c("STDYX Standardization", "STDY Standardization", "STD Standardization")
    stdyx.section <- getSection("STDYX Standardization", ciStdSection, headers=stdsections)
    if (!is.null(stdyx.section)) { allSections <- appendListElements(allSections, extractParameters_1section(filename, stdyx.section, "ci.stdyx.standardized")) }

    stdy.section <- getSection("STDY Standardization", ciStdSection, headers=stdsections)
    if (!is.null(stdy.section)) allSections <- appendListElements(allSections, extractParameters_1section(filename, stdy.section, "ci.stdy.standardized"))

    std.section <- getSection("STD Standardization", ciStdSection, headers=stdsections)
    if (!is.null(std.section)) allSections <- appendListElements(allSections, extractParameters_1section(filename, std.section, "ci.std.standardized"))
  }
  
  # when the confidence intervals for logistic regression odds are present, the header line is not reprinted and must be copied from ciSection
  ciOddsHeaders <- grep("^\\s*CONFIDENCE INTERVALS FOR THE LOGISTIC REGRESSION ODDS RATIO RESULTS.*$", outfiletext, perl=TRUE, value=TRUE)
  if (length(ciOddsHeaders) > 0L && !is.null(ciSection)) {
    ciOddsTables <- lapply(ciOddsHeaders, function(hdr) {
      sec <- getSection(paste0("^\\s*", trimws(hdr), "\\s*$"), outfiletext)
      if (is.null(sec)) return(NULL)
      sec <- c(ciSection[attr(hline, "header_lines")], sec)
      tbl <- extractParameters_1section(filename, sec, "ci.odds")[["ci.odds"]]
      if (!is.null(tbl) && nrow(tbl) > 0L && length(ciOddsHeaders) > 1L &&
          grepl("FOR LATENT CLASS VARIABLES", hdr, fixed = TRUE) &&
          !"LatentClass" %in% names(tbl)) {
        tbl$LatentClass <- "Categorical.Latent.Variables"
      }
      tbl
    })
    combinedCiOdds <- align_rbind_sections(ciOddsTables)
    if (!is.null(combinedCiOdds)) allSections$ci.odds <- combinedCiOdds
  }

  # Mplus 9.0 / 9.1 sections: ESEM / SEFA / DSEFA STANDARDIZED SOLUTION,
  # latent-class indicator equality tests, and LTA transition odds
  esemStd <- extractESEMStandardized(outfiletext, filename)
  if (!is.null(esemStd)) allSections$esem.standardized <- esemStd

  classEq <- extractClassEqualityTests(outfiletext, filename)
  if (!is.null(classEq)) allSections$class.equality <- classEq

  transOdds <- extractTransitionOdds(outfiletext)
  if (!is.null(transOdds)) allSections$transition.odds <- transOdds

  allSections <- appendListElements(allSections, extractAdditionalParameters(outfiletext, filename))

  #extract EFA parameters if this is an EFA output
  if (efa) {
    
    allSections <- appendListElements(
      allSections,
      extractEFAparameters(outfiletext, filename)
    )
  }

  # cleaner equivalent of above
  listOrder <- c("unstandardized", "unstandardized.alt", "r2", "ci.unstandardized",
      "irt.parameterization", "probability.scale", "ci.probability.scale",
      "indicator.profiles", "indicator.odds", "ci.indicator.odds", "brant", "logrank",
      "class.equality", "transition.odds",
      "odds", "ci.odds",
      "stdyx.standardized", "ci.stdyx.standardized",
      "stdy.standardized", "ci.stdy.standardized",
      "std.standardized", "ci.std.standardized",
      "wilevel.standardized", "esem.standardized", "efa")
  listOrder <- listOrder[listOrder %in% names(allSections)]


  #only re-order if out of order
  if(!identical(names(allSections), listOrder)) allSections <- allSections[listOrder]

  #this needs to be here not to conflict with the drop to 1 element logic above.
  #if resultType passed (deprecated), only return the appropriate element
  #this is inefficient because all sections will be parsed, but it's deprecated, so no worries.
  if (!missing(resultType)) {
    warning(paste("resultType is deprecated and will be removed in a future version.\n  ",
            "extractModelParameters now returns a list containing unstandardized and standardized parameters, where available.\n  ",
            "For now, resultType is respected, so a data.frame will be returned."))

    oldNewTranslation <- switch(EXPR = resultType,
        raw   = "unstandardized",
        stdyx = "stdyx.standardized",
        stdy  = "stdy.standardized",
        std   = "std.standardized")

    allSections <- allSections[[oldNewTranslation]]
  }

  return(allSections)
}

#' (DEPRECATED) Extract model parameters from MODEL RESULTS section.
#'
#' Extracts the model parameters from the MODEL RESULTS section of one or more Mplus output files.
#' If a particular output file has more than one results section (unstandardized, stdyx, stdy, and/or std),
#' a list will be returned. If the \code{target} is a directory, all .out files therein will be parsed
#' and a single list will be returned, where the list elements are named by the output file name.
#' Returned parameters often include the parameter estimate, std. err, param/s.e., and two-tailed p-value.
#'
#' @param target the directory containing Mplus output files (.out) to parse OR the single output file to
#'   be parsed. May be a full path, relative path, or a filename within the working directory.
#'   Defaults to the current working directory. Example: \dQuote{C:/Users/Michael/Mplus Runs}
#' @param recursive optional. If \code{TRUE}, parse all models nested in subdirectories
#'   within \code{target}. Defaults to \code{FALSE}.
#' @param filefilter a Perl regular expression (PCRE-compatible) specifying particular output
#'   files to be parsed within \code{directory}. See \code{regex} or \url{https://www.pcre.org/pcre.txt}
#'   for details about regular expression syntax.
#' @param dropDimensions Relevant only for multi-file parsing. If \code{TRUE}, then if only one output
#'   section (usually unstandardized) is present for all files in the parsed list, then eliminate
#'   the second-level list (which contains elements for each output section). The result is
#'   that the elements of the returned list are \code{data.frame} objects with the relevant parameters.
#' @param resultType N.B.: this parameter is deprecated and will be removed in a future version. The
#'   new default is to extract all results that are present and return a list (see below for details).
#'   \code{resultType} specified the results section to extract. If \code{raw}, the unstandardized
#'   estimates will be returned. \dQuote{stdyx}, \dQuote{stdy}, and \dQuote{std}
#'   are the other options, which extract different standardized solutions.
#'   See the Mplus User's Guide for additional details about the differences in these standardizations.
#'
#' @return If \code{target} is a single file, a list containing unstandardized and standardized results will be
#' returned. If all standardized solutions are available, the list element will be named: \code{unstandardized},
#' \code{stdyx.standardized}, \code{stdy.standardized}, and \code{std.standardized}. If confidence intervals
#' are output using OUTPUT:CINTERVAL, then a list element named \code{ci.unstandardized} will be included.
#' Each of these list elements is a \code{data.frame} containing relevant model parameters.
#'
#' If \code{target} is a directory, a list will be returned, where each element contains the results for
#' a single file, and the top-level elements are named after the corresponding output file name. Each
#' element within this list is itself a list, with elements as in the single file case above.
#'
#' The core \code{data.frame} for each MODEL RESULTS section typically has the following structure:
#'
#' * `paramHeader`: The header that begins a given parameter set. Example: "FACTOR1 BY"
#' * `param`: The particular parameter being measured (within \code{paramHeader}). Example: "ITEM1"
#' * `est`: Parameter estimate value.
#' * `se`: Standard error of the estimate
#' * `est_se`: Quotient of \code{est/se}, representing z-test/t-test in large samples
#' * `pval`: Two-tailed p-value for the \code{est_se} quotient.
#'
#' In the case of output from Bayesian estimation (ESTIMATOR=BAYES), the \code{data.frame} will contain
#' a different set of variables, including some of the above, as well as
#' * `posterior_sd`: Posterior standard deviation of the estimate.
#' * `lower_2.5ci`: Lower 2.5 percentile of the estimate.
#' * `upper_2.5ci`: Upper 2.5 percentile (aka 97.5 percentile) of the estimate.
#'
#' Also note that the \code{pval} column for Bayesian output represents a one-tailed estimate.
#'
#' In the case of output from a Monte Carlo study (MONTECARLO: and MODEL POPULATION:), the \code{data.frame} will contain
#' a different set of variables, including some of the above, as well as
#' * `population`: Population parameter value.
#' * `average`: Average parameter estimate across replications.
#' * `population_sd`: Standard deviation of parameter value in population across replications.
#' * `average_se`: Average standard error of estimated parameter value across replications.
#' * `mse`: Mean squared error.
#' * `cover_95`: Proportion of replications whose 95\% confidence interval for the parameter includes the population value.
#' * `pct_sig_coef`: Proportion of replications for which the two-tailed significance test of the parameter is significant (p < .05).
#'
#' In the case of confidence interval output (OUTPUT:CINTERVAL), the list element \code{ci.unstandardized} will contain
#' a different set of variables, including some of the above, as well as
#' * `low.5`: Lower 0.5\% CI estimate.
#' * `low2.5`: Lower 2.5\% CI estimate.
#' * `low5`: Lower 5\% CI estimate.
#' * `est`: Parameter estimate value.
#' * `up5`: Upper 5\% (i.e., 95\%) CI estimate.
#' * `up2.5`: Upper 2.5\% (i.e., 97.5\%) CI estimate.
#' * `up.5`: Upper 0.5\% (i.e., 99.5\%) CI estimate.
#'
#' If the model contains multiple latent classes, an additional variable, \code{LatentClass},
#' will be included, specifying the latent class number. Also, the Categorical Latent Variables section
#' will be included as \code{LatentClass} "Categorical.Latent.Variables."
#'
#' If the model contains multiple groups, \code{Group} will be included.
#'
#' If the model contains two-level output (between/within), \code{BetweenWithin} will be included.
#'
#'
#' @author Michael Hallquist
#' @seealso \code{\link{extractModelSummaries}}
#' @keywords internal
#' @examples
#' \dontrun{
#' ex3.14 <- extractModelParameters(
#' 	"C:/Program Files/Mplus/Mplus Examples/User's Guide Examples/ex3.14.out")
#' }
extractModelParameters <- function(target=getwd(), recursive=FALSE, filefilter, dropDimensions=FALSE, resultType) {
  #message("This function is deprecated and will be removed from future versions of MplusAutomation. Please use readModels() instead.")
  message("extractModelParameters has been deprecated. Please use readModels(\"nameofMplusoutfile.out\", what=\"parameters\")$parameters to replicate the old functionality.")
  
  #function tree (top to bottom):
  #extractModelParameters: loop over one or more output files
  #extractParameters_1file: extract model parameters for all sections (unstandardized, stdyx, stdy, std in a single file
  #extractParameters_1section: extract model parameters for a given section.
  #extractParameters_1chunk: extract model parameters for a given chunk (e.g., Latent class 2, Between Level) within a given section.

}

Try the MplusAutomation package in your browser

Any scripts or data that you put into this service are public.

MplusAutomation documentation built on Nov. 18, 2026, 1:06 a.m.