R/mplus.extract.R

Defines functions mplus.extract

Documented in mplus.extract

#' Extract Results Saved in a Mplus HDF5 file
#'
#' This function extracts various results saved in the Hierarchical Data Format
#' version 5 (HDF5) file with the h5 file extension created by Mplus when
#' requesting \code{H5RESULTS} in the \code{SAVEDATA} section, e.g.,
#' \code{SAVEDATA: H5RESULTS = results.h5;}. By default, the function does not
#' print any results on the console, i.e., \code{result = "none"}. Note that
#' Mplus version 8.11 or higher is required for saving result in a HDF file.
#'
#' @param x       a character string indicating the name of the Mplus H5 file
#'                (HDF5 format) with or without the file extension \code{.h5},
#'                e.g., \code{"Mplus_Results.h5"} or \code{"Mplus_Results"}.
#' @param print   a character vector indicating which Mplus output section to show
#'                on the R console, i.e., \code{"none"} (default) for no input or
#'                result sections, \code{"all"} for input section including
#'                warning messages and result sections, \code{"input"} for input
#'                section including warning messages, and "result" for result
#'                sections.
#' @param result  a character vector specifying the Mplus result sections to be
#'                shown on the console, \code{"all"} for all available result
#'                sections. See 'Details' for result sections that can be requested.
#' @param exclude a character string indicating result sections to be excluded
#'                when \code{result} is not \code{"none"}.
#' @param digits  an integer value indicating the number of decimal places to be
#'                used for displaying results. Note that by default, the function
#'                does not round any results.
#' @param write   a character string naming a text file with file extension
#'                \code{".txt"} (e.g., \code{"Output.txt"}) for writing the
#'                output into a text file.
#' @param append  logical: if \code{TRUE} (default), output will be appended
#'                to an existing text file with extension \code{.txt} specified
#'                in \code{write}, if \code{FALSE} existing text file will be
#'                overwritten.
#' @param check   logical: if \code{TRUE} (default), argument specification is checked.
#'
#' @details
#' \describe{
#' \item{\strong{Result Sections}}{Following result sections can be selected by
#' using the \code{result} argument or excluded by using the \code{exclude}
#' argument:
#'    \itemize{
#'       \item{\code{"summary.analysis"}} for the \code{SUMMARY OF ANALYSIS} section.
#'       \item{\code{"fit"}} for the \code{Model Fit Information} section.
#'       \item{\code{"fit.config"}} for the \code{Model Fit Information (configural)} section.
#'       \item{\code{"fit.metric"}} for the \code{Model Fit Information (metric)} section.
#'       \item{\code{"fit.scalar"}} for the \code{Model Fit Information (scalar)} section.
#'       \item{\code{"mod.result"}} for the \code{Model Results} section.
#'       \item{\code{"mod.result.config"}} for the \code{Model Results (configural)} section.
#'       \item{\code{"mod.result.metric"}} for the \code{Model Results (metric)} section.
#'       \item{\code{"mod.result.scalar"}} for the \code{Model Results (scalar)} section.
#'       \item{\code{"conf.mod.result"}} for the \code{Confidence Intervals of Model Results} section.
#'       \item{\code{"conf.mod.result.config"}} for the \code{Confidence Intervals of Model Results (configural)} section.
#'       \item{\code{"conf.mod.result.metric"}} for the \code{Confidence Intervals of Model Results (metric)} section.
#'       \item{\code{"conf.mod.result.scalar"}} for the \code{Confidence Intervals of Model Results (scalar)} section.
#'       \item{\code{"cred.mod.result"}} for the \code{Credibility Intervals of Model Results} section.
#'       \item{\code{"cred.mod.result.config"}} for the \code{Credibility Intervals of Model Results (configural)} section.
#'       \item{\code{"cred.mod.result.metric"}} for the \code{Credibility Intervals of Model Results (metric)} section.
#'       \item{\code{"cred.mod.result.scalar"}} for the \code{Credibility Intervals of Model Results (scalar)} section.
#'       \item{\code{"std.mod.result"}} for the \code{Standardized Model Results} section.
#'       \item{\code{"conf.std.mod.result"}} for the \code{Confidence Intervals of Standardized Model Results} section.
#'       \item{\code{"cred.std.mod.result"}} for the \code{Credibility Intervals of Standardized Model Results} section.
#'       \item{\code{"rsquare"}} for the \code{R-Square} section.
#'       \item{\code{"rsquare.config"}} for the \code{R-Square (configural)} section.
#'       \item{\code{"rsquare.metric"}} for the \code{R-Square (metric)} section.
#'       \item{\code{"rsquare.scalar"}} for the \code{R-Square (scalar)} section.
#'       \item{\code{"irt.param"}} for the \code{IRT Parameterization} section.
#'       \item{\code{"irt.param.config"}} for the \code{IRT Parameterization (configural)} section.
#'       \item{\code{"irt.param.metric"}} for the \code{IRT Parameterization (metric)} section.
#'       \item{\code{"irt.param.scalar"}} for the \code{IRT Parameterization (scalar)} section.
#'       \item{\code{"modind"}} for the \code{Model Modification Indices} section.
#'       \item{\code{"prob.scale"}} for the \code{Results in Probability Scale} section.
#'       \item{\code{"prob.scale.config"}} for the \code{Results in Probability Scale (configural)} section.
#'       \item{\code{"prob.scale.metric"}} for the \code{Results in Probability Scale (metric)} section.
#'       \item{\code{"prob.scale.scalar"}} for the \code{Results in Probability Scale (scalar)} section.
#'       \item{\code{"odds.ratio"}} for the \code{Logistic Regression Odds Ratio Results} section.
#'       \item{\code{"conf.prob.scale"}} for the \code{Confidence Intervals in Probability Scale} section.
#'       \item{\code{"conf.prob.scale.config"}} for the \code{Confidence Intervals in Probability Scale (configural)} section.
#'       \item{\code{"conf.prob.scale.metric"}} for the \code{Confidence Intervals in Probability Scale (metric)} section.
#'       \item{\code{"conf.prob.scale.scalar"}} for the \code{Confidence Intervals in Probability Scale (scalar)} section.
#'       \item{\code{"cred.prob.scale"}} for the \code{Credibility Intervals in Probability Scale} section.
#'       \item{\code{"cred.prob.scale.config"}} for the \code{Credibility Intervals in Probability Scale (configural)} section.
#'       \item{\code{"cred.prob.scale.metric"}} for the \code{Credibility Intervals in Probability Scale (metric)} section.
#'       \item{\code{"cred.prob.scale.scalar"}} for the \code{Credibility Intervals in Probability Scale (scalar)} section.
#'       \item{\code{"ind.odds.ratio"}} for the \code{Latent Class Indicator Odds Ratios for the Latent Classes} section.
#'       \item{\code{"ind.odds.ratio.config"}} for the \code{Latent Class Indicator Odds Ratios for the Latent Classes (configural)} section.
#'       \item{\code{"ind.odds.ratio.metric"}} for the \code{Latent Class Indicator Odds Ratios for the Latent Classes (metric)} section.
#'       \item{\code{"ind.odds.ratio.scalar"}} for the \code{Latent Class Indicator Odds Ratios for the Latent Classes (scalar)} section.
#'       \item{\code{"brant.wald"}} for the \code{Brant Wald Test For Proportional Odds} section.
#'       \item{\code{"within.std.mod.result"}} for the \code{Within-level Standardized Model Results For Each Cluster} section.
#'       \item{\code{"within.rsquare"}} for the \code{Within-level R-Square For Each Cluster} section.
#'    }
#' Note that all result sections are requested by specifying \code{result = "all"}.
#' The \code{result} argument is also used to select one (e.g., \code{result = "mod.result"})
#' or more than one result section (e.g., \code{result = c("mod.result", "std.mod.result")}).
#' The \code{exclude} argument is used to exclude result sections from the output
#' (e.g., \code{exclude = "mod.result"}). By default, all result sections except
#' \code{SUMMARY OF ANALYSIS} is shown on the R console when specifying \code{"all"}
#' or \code{"result"} for the argument \code{print}.
#' }
#' }
#'
#' @author
#' Takuya Yanagida
#'
#' @seealso
#' \code{\link{read.mplus}}, \code{\link{write.mplus}}, \code{\link{mplus}},
#' \code{\link{mplus.bayes}}, \code{\link{mplus.lca}}, \code{\link{mplus.lca.summa}},
#' \code{\link{mplus.plot}}, \code{\link{mplus.print}}, \code{\link{mplus.run}},
#' \code{\link{mplus.update}}
#'
#' @references
#' Muthen, L. K., & Muthen, B. O. (1998-2017). \emph{Mplus User's Guide} (8th ed.).
#' Muthen & Muthen.
#'
#' @return
#' Returns a list with Mplus input and result sections.
#'
#' @export
#'
#' @examples
#' \dontrun{
#' #————————————————————————————————————————————————————————————————————————————
#' # Write data, specify input, run input, and extract results
#'
#' # Write Mplus data file
#' write.mplus(ex3_1, file = "ex3_1.dat")
#'
#' # Specify Mplus input
#' input1 <- '
#' DATA:     FILE IS ex3_1.dat;
#' VARIABLE: NAMES ARE y1 x1 x3;
#' MODEL:    y1 ON x1 x3;
#' OUTPUT:   STANDARDIZED;
#' SAVEDATA: H5RESULTS = ex3_1.h5;
#' '
#'
#' # Run Mplus input
#' mplus(input1, file = "ex3_1.inp")
#'
#' # Example 1a: Extract results, no results are printed by default
#' results <- mplus.extract("ex3_1.h5")
#'
#' # Print 'Model Results' section
#' results$mod.result
#'
#' # Example 1b: Print all results except 'SUMMARY OF ANALYSIS'
#' mplus.extract("ex3_1.h5", print = "result")
#'
#' # Example 1c: Print 'Model Fit Information' section
#' mplus.extract("ex3_1.h5", print = "result", result = "fit")
#'
#' # Example 1c: Exclude 'Standardized Model Results' section
#' mplus.extract("ex3_1.h5", print = "result", exclude = "std.mod.result")
#'
#' # Example 1d: Print results with two digits
#' mplus.extract("ex3_1.h5", print = "result", digits = 2)
#'
#' #————————————————————————————————————————————————————————————————————————————
#' # Write Results
#'
#' # Example 2: Write Results into a text file
#' mplus.extract("ex3_1.h5", print = "result", write = "Output_3-1.txt")
#' }
mplus.extract <- function(x, print = c("none", "all", "input", "result"),
                          result = c("all", "summary.analysis", "fit", "mod.result",
                                     "conf.mod.result", "cred.mod.result", "std.mod.result",
                                     "conf.std.mod.result", "cred.std.mod.result", "rsquare",
                                     "irt.param", "modind", "prob.scale", "odds.ratio",
                                     "conf.prob.scale", "cred.prob.scale", "ind.odds.ratio",
                                     "brant.wald", "within.std.mod.result", "within.rsquare"),
                          exclude = NULL, digits = NULL, write = NULL, append = TRUE, check = TRUE) {

  #_____________________________________________________________________________
  #
  # Initial Check ——————————————————————————————————————————————————————————————

  # Check if input 'x' is missing or NULL
  if (isTRUE(missing(x) || is.null(x) || !is.character(x) || length(x) != 1L)) { stop("Please specify a character string indicating the name of a Mplus H5 file for the argument 'x'", call. = FALSE) }

  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  ## H5 File ####

  # File extension .h5
  x <- ifelse(isTRUE(!grepl(".h5", x)), file <- paste0(x, ".gh5"), x)

  # Check if 'x' exists
  if (isTRUE(!file.exists(x))) { stop(paste0("Unable to read the Mplus H5 file: ", sQuote(x), " does not exist."), call. = FALSE) }

  #_____________________________________________________________________________
  #
  # Input Check ————————————————————————————————————————————————————————————————

  .check.input(logical = "append", s.character = list(print = c("none", "all", "input", "result")), args = c("digits", "write1"), envir = environment(), input.check = check)

  #_____________________________________________________________________________
  #
  # Arguments ——————————————————————————————————————————————————————————————————

  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  ## 'print' Argument ####

  # Default
  if (isTRUE(all(c("none", "all", "input", "result") %in% print))) {

    print <- "none"

  # All, input and results
  } else if (isTRUE(any(print == "all"))) {

    print <- c("input", "result")

  }

  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  ## 'result' Argument ####

  # Default
  if (isTRUE(all(c("all", "summary.analysis", "fit", "mod.result", "conf.mod.result", "cred.mod.result", "std.mod.result", "conf.std.mod.result", "cred.std.mod.result", "rsquare", "irt.param", "modind", "prob.scale", "odds.ratio", "conf.prob.scale", "cred.prob.scale", "ind.odds.ratio", "brant.wald", "within.std.mod.result", "within.rsquare") %in% result))) {

    result <- c("fit", "mod.result", "conf.mod.result", "cred.mod.result", "std.mod.result", "conf.std.mod.result", "cred.std.mod.result", "rsquare", "irt.param", "modind", "prob.scale", "odds.ratio", "conf.prob.scale", "cred.prob.scale", "ind.odds.ratio", "brant.wald", "within.std.mod.result", "within.rsquare")

  # All results
  } else if (isTRUE(all(result == "all"))) {

    result <- c("summary.analysis", "fit", "fit.config", "fit.metric", "fit.scalar", "mod.result", "mod.result.config", "mod.result.metric", "mod.result.scalar", "conf.mod.result", "conf.mod.result.config", "conf.mod.result.metric", "conf.mod.result.scalar", "cred.mod.result", "cred.mod.result.config", "cred.mod.result.metric", "cred.mod.result.scalar", "std.mod.result", "conf.std.mod.result", "cred.std.mod.result", "rsquare", "rsquare.config", "rsquare.metric", "rsquare.scalar", "irt.param", "irt.param.config", "irt.param.metric", "irt.param.scalar", "modind", "prob.scale", "prob.scale.config", "prob.scale.metric", "prob.scale.scalar", "odds.ratio", "conf.prob.scale", "conf.prob.scale.config", "conf.prob.scale.metric", "conf.prob.scale.scalar", "cred.prob.scale", "cred.prob.scale.config", "cred.prob.scale.metric", "cred.prob.scale.scalar", "ind.odds.ratio", "ind.odds.ratio.config", "ind.odds.ratio.metric", "ind.odds.ratio.scalar", "brant.wald", "within.std.mod.result", "within.rsquare")

  }

  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  ## 'exclude' Argument ####

  if (isTRUE(!is.null(exclude))) { result <- setdiff(result, exclude) }

  #_____________________________________________________________________________
  #
  # Main Function ——————————————————————————————————————————————————————————————

  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  ## Read Mplus GH5 File ####

  gh5 <- tryCatch(hdf5r::h5file(x), error = function(y) { stop("Reading Mplus H5 file using the h5file() function from the hdf5r package failed.", call. = FALSE) })

  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  ## Extract Results ####

  # Result objects
  input <- warn <- summary.analysis <- fit <- fit.config <- fit.metric  <- fit.scalar <- mod.result <- mod.result.config <- mod.result.metric <- mod.result.scalar <- conf.mod.result <- conf.mod.result.config <- conf.mod.result.metric <- conf.mod.result.scalar <- cred.mod.result <- cred.mod.result.config <- cred.mod.result.metric <- cred.mod.result.scalar <- std.mod.result <- conf.std.mod.result <- cred.std.mod.result <- rsquare <- rsquare.config <- rsquare.metric <- rsquare.scalar <- irt.param <- irt.param.config <- irt.param.metric <- irt.param.scalar <- modind <- prob.scale <- prob.scale.config <- prob.scale.metric <- prob.scale.scalar <- odds.ratio <- conf.prob.scale <- conf.prob.scale.config <- conf.prob.scale.metric <- conf.prob.scale.scalar <- cred.prob.scale <- cred.prob.scale.config <- cred.prob.scale.metric <- cred.prob.scale.scalar <- ind.odds.ratio <- ind.odds.ratio.config <- ind.odds.ratio.metric <- ind.odds.ratio.scalar <- brant.wald <- within.std.mod.result <- within.rsquare <- NULL

  #—————————————————————————————————————— #
  ### Input instructions ####

  "Input instructions" |> (\(p) if (isTRUE(p %in% names(gh5))) { gh5[[p]] |> (\(q) { input <<- paste(misty::chr.trim(q[seq_len(q$maxdims)], side = "right", check = FALSE), collapse = "\n") })() })()

  #—————————————————————————————————————— #
  ### Errors and warnings ####

  "Errors and warnings" |> (\(p) if (isTRUE(p %in% names(gh5))) { gh5[[p]] |> (\(q) { warn <<- paste(misty::chr.trim(q[seq_len(q$maxdims)], side = "right", check = FALSE), collapse = "\n") })() })()

  #—————————————————————————————————————— #
  ### Summary of Analysis ####

  "Summary of Analysis" |> (\(p) if (isTRUE(p %in% names(gh5))) {

    # Extract attributes
    summary.analysis <<- hdf5r::h5attributes(gh5[["Summary of Analysis"]]) |> (\(p) lapply(p, function(y) if (isTRUE(is.character(y))) { misty::chr.trim(y, check = FALSE) } else { y } ))()

    # Extract listings
    gh5[[p]] |> (\(q) { for (i in names(q)) {

        #···················
        #### H5D ####

        if (isTRUE("H5D" %in% class(q[[i]]))) {

          summary.analysis <<- append(summary.analysis, eval(parse(text = paste0("list(\"", i, "\" = ", deparse1(q[[i]][seq_len(q[[i]]$maxdims)] |> (\(r) if (isTRUE(is.list(r))) { lapply(r, function(y) if (isTRUE(is.character(y))) { misty::chr.trim(y, check = FALSE) } else { y } ) } else { if (isTRUE(is.character(r))) { misty::chr.trim(r, check = FALSE) } else { r } })()), ")"))))

        #···················
        #### H5Group ####

        } else if (isTRUE("H5Group" %in% class(q[[i]]))) {

          # Listing
          if (isTRUE(length(names(q[[i]])) != 0L)) {

            templist <- list()

            # Attributes
            if (isTRUE(length(hdf5r::h5attributes(q[[i]])) != 0L)) { templist <- eval(parse(text = paste0(deparse1(hdf5r::h5attributes(q[[i]]) |> (\(r) if (isTRUE(is.list(r))) { lapply(r, function(y) if (isTRUE(is.character(y))) { misty::chr.trim(y, check = FALSE) } else { y } ) } else { if (isTRUE(is.character(r))) { misty::chr.trim(r, check = FALSE) } else { r } })())))) }

            # Loop across listings
            if (isTRUE(all(names(q[[i]]) != i))) {

              for (j in names(q[[i]])) {

                templist <- append(templist, eval(parse(text = paste0("list(\"", misty::chr.trim(j, check = FALSE), "\" = ", deparse1(if (isTRUE(is.null(q[[i]][[j]]$maxdims))) {

                  hdf5r::h5attributes(q[[i]][[j]]) |> (\(r) lapply(r, function(y) if (isTRUE(is.character(y))) { misty::chr.trim(y, check = FALSE) } else { y } ))()

                  } else {

                    q[[i]][[j]][seq_len(q[[i]][[j]]$maxdims)] |> (\(r) if (is.list(r)) { lapply(r, function(y) if (isTRUE(is.character(y))) { misty::chr.trim(y, check = FALSE) } else { y } ) } else { if (isTRUE(is.character(r))) { misty::chr.trim(r, check = FALSE) } else { r } })()

                  }, ")"), ")"))))

              }

              summary.analysis <<- append(summary.analysis, eval(parse(text = if (isTRUE(length(templist) == 1L && i != "Observed dependent variables")) { deparse1(templist) } else {   paste0("list(", deparse(rev(unlist(strsplit(q[[i]]$get_obj_name(), "/")))[1L]), " = ", deparse1(templist), ")") })))

            } else {

              if (isTRUE(length(templist) != 0L)) { summary.analysis <<- append(summary.analysis, templist) }

              summary.analysis <<- append(summary.analysis, eval(parse(text = paste0("list(\"", i, "\" = ", deparse1(q[[i]][[names(q[[i]])]][seq_len(q[[i]][[names(q[[i]])]]$maxdims)] |> (\(r) if (isTRUE(is.list(r))) { lapply(r, function(y) if (isTRUE(is.character(y))) { misty::chr.trim(y, check = FALSE) } else { y } ) } else { if (isTRUE(is.character(r))) { misty::chr.trim(r, check = FALSE) } else { r } })()), ")"))))

            }

          # No Listings
          } else {

            summary.analysis <<- append(summary.analysis, eval(parse(text = paste0("list(", deparse1(rev(unlist(strsplit(q[[i]]$get_obj_name(), "/")))[1L]), " = ", deparse1(hdf5r::h5attributes(q[[i]]) |> (\(r) if (isTRUE(is.list(r))) { lapply(r, function(y) if (isTRUE(is.character(y))) { misty::chr.trim(y, check = FALSE) } else { y } ) } else { if (isTRUE(is.character(r))) { misty::chr.trim(r, check = FALSE) } else { r } })()), ")"))))

          }

        }

      }

    })()

  })()

  #—————————————————————————————————————— #
  ### Model Fit Information ####

  "Model Fit Information" |> (\(p) if (isTRUE(p %in% names(gh5))) { fit <<- .mplus.extract.fit(p, gh5 = gh5) })()

  #—————————————————————————————————————— #
  ### Model Fit Information (configural) ####

  "Model Fit Information (configural)" |> (\(p) if (isTRUE(p %in% names(gh5))) { fit.config <<- .mplus.extract.fit(p, gh5 = gh5) })()

  #—————————————————————————————————————— #
  ### Model Fit Information (metric) ####

  "Model Fit Information (metric)" |> (\(p) if (isTRUE(p %in% names(gh5))) { fit.metric <<- .mplus.extract.fit(p, gh5 = gh5) })()

  #—————————————————————————————————————— #
  ### Model Fit Information (scalar) ####

  "Model Fit Information (scalar)" |> (\(p) if (isTRUE(p %in% names(gh5))) { fit.scalar <<- .mplus.extract.fit(p, gh5 = gh5) })()

  #—————————————————————————————————————— #
  ### Model Results ####

  "Model Results" |> (\(p) if (isTRUE(p %in% names(gh5))) { mod.result <<- .mplus.extract.mod.result(p, gh5 = gh5, summary.analysis = summary.analysis) })()

  #—————————————————————————————————————— #
  ### Model Results (configural) ####

  "Model Results (configural)" |> (\(p) if (isTRUE(p %in% names(gh5))) { mod.result.config <<- .mplus.extract.mod.result(p, gh5 = gh5, summary.analysis = summary.analysis) })()

  #—————————————————————————————————————— #
  ### Model Results (metric) ####

  "Model Results (metric)" |> (\(p) if (isTRUE(p %in% names(gh5))) { mod.result.metric <<- .mplus.extract.mod.result(p, gh5 = gh5, summary.analysis = summary.analysis) })()

  #—————————————————————————————————————— #
  ### Model Results (scalar) ####

  "Model Results (scalar)" |> (\(p) if (isTRUE(p %in% names(gh5))) { mod.result.scalar <<- .mplus.extract.mod.result(p, gh5 = gh5, summary.analysis = summary.analysis) })()

  #—————————————————————————————————————— #
  ### Confidence Intervals of Model Results ####

  "Confidence Intervals of Model Results" |> (\(p) if (isTRUE(p %in% names(gh5))) { conf.mod.result <<- .mplus.extract(p, gh5 = gh5) |> (\(q) which(!colnames(q) %in% c("Group", "Class", "Level", "Section", "Statement")) |> (\(r) setNames(q, nm = c(colnames(q)[setdiff(seq_len(ncol(q)), r)], c("Low .5%",  "Low 2.5%", "Low 5%", "Estimate", "Upp 5%", "Upp 2.5%", "Upp .5%"))))())() })()

  #—————————————————————————————————————— #
  ### Confidence Intervals of Model Results (configural) ####

  "Confidence Intervals of Model Results (configural)" |> (\(p) if (isTRUE(p %in% names(gh5))) { conf.mod.result.config <<- .mplus.extract(p, gh5 = gh5) |> (\(q) which(!colnames(q) %in% c("Group", "Class", "Level", "Section", "Statement")) |> (\(r) setNames(q, nm = c(colnames(q)[setdiff(seq_len(ncol(q)), r)], c("Low .5%",  "Low 2.5%", "Low 5%", "Estimate", "Upp 5%", "Upp 2.5%", "Upp .5%"))))())() })()

  #—————————————————————————————————————— #
  ### Confidence Intervals of Model Results (metric) ####

  "Confidence Intervals of Model Results (metric)" |> (\(p) if (isTRUE(p %in% names(gh5))) { conf.mod.result.metric <<- .mplus.extract(p, gh5 = gh5) |> (\(q) which(!colnames(q) %in% c("Group", "Class", "Level", "Section", "Statement")) |> (\(r) setNames(q, nm = c(colnames(q)[setdiff(seq_len(ncol(q)), r)], c("Low .5%",  "Low 2.5%", "Low 5%", "Estimate", "Upp 5%", "Upp 2.5%", "Upp .5%"))))())() })()

  #—————————————————————————————————————— #
  ### Confidence Intervals of Model Results (scalar) ####

  "Confidence Intervals of Model Results (scalar)" |> (\(p) if (isTRUE(p %in% names(gh5))) { conf.mod.result.scalar <<- .mplus.extract(p, gh5 = gh5) |> (\(q) which(!colnames(q) %in% c("Group", "Class", "Level", "Section", "Statement")) |> (\(r) setNames(q, nm = c(colnames(q)[setdiff(seq_len(ncol(q)), r)], c("Low .5%",  "Low 2.5%", "Low 5%", "Estimate", "Upp 5%", "Upp 2.5%", "Upp .5%"))))())() })()

  #—————————————————————————————————————— #
  ### Credibility Intervals of Model Results ####

  "Credibility Intervals of Model Results" |> (\(p) if (isTRUE(p %in% names(gh5))) { cred.mod.result <<- .mplus.extract(p, gh5 = gh5) |> (\(q) which(!colnames(q) %in% c("Group", "Class", "Level", "Section", "Statement")) |> (\(r) setNames(q, nm = c(colnames(q)[setdiff(seq_len(ncol(q)), r)], c("Low .5%",  "Low 2.5%", "Low 5%", "Estimate", "Upp 5%", "Upp 2.5%", "Upp .5%"))))())() })()

  #—————————————————————————————————————— #
  ### Credibility Intervals of Model Results (configural) ####

  "Credibility Intervals of Model Results (configural)" |> (\(p) if (isTRUE(p %in% names(gh5))) { cred.mod.result.config <<- .mplus.extract(p, gh5 = gh5) |> (\(q) which(!colnames(q) %in% c("Group", "Class", "Level", "Section", "Statement")) |> (\(r) setNames(q, nm = c(colnames(q)[setdiff(seq_len(ncol(q)), r)], c("Low .5%",  "Low 2.5%", "Low 5%", "Estimate", "Upp 5%", "Upp 2.5%", "Upp .5%"))))())() })()

  #—————————————————————————————————————— #
  ### Credibility Intervals of Model Results (metric) ####

  "Credibility Intervals of Model Results (metric)" |> (\(p) if (isTRUE(p %in% names(gh5))) { cred.mod.result.metric <<- .mplus.extract(p, gh5 = gh5) |> (\(q) which(!colnames(q) %in% c("Group", "Class", "Level", "Section", "Statement")) |> (\(r) setNames(q, nm = c(colnames(q)[setdiff(seq_len(ncol(q)), r)], c("Low .5%",  "Low 2.5%", "Low 5%", "Estimate", "Upp 5%", "Upp 2.5%", "Upp .5%"))))())() })()

  #—————————————————————————————————————— #
  ### Credibility Intervals of Model Results (scalar) ####

  "Credibility Intervals of Model Results (scalar)" |> (\(p) if (isTRUE(p %in% names(gh5))) { cred.mod.result.scalar <<- .mplus.extract(p, gh5 = gh5) |> (\(q) which(!colnames(q) %in% c("Group", "Class", "Level", "Section", "Statement")) |> (\(r) setNames(q, nm = c(colnames(q)[setdiff(seq_len(ncol(q)), r)], c("Low .5%",  "Low 2.5%", "Low 5%", "Estimate", "Upp 5%", "Upp 2.5%", "Upp .5%"))))())() })()

  #—————————————————————————————————————— #
  ### Standardized Model Results ####

  "Standardized Model Results" |> (\(p) if (isTRUE(p %in% names(gh5))) {

    if (isTRUE(any(c("STDYX Standardization", "STDY Standardization", "STD Standardization") %in% names(gh5[[p]])))) {

      # StdYX Standardization
      if (isTRUE("STDYX Standardization" %in% names(gh5[[p]]))) {

        std.mod.result$stdyx <<- .mplus.extract(p, std = "STDYX Standardization", gh5 = gh5) |> (\(q) which(!colnames(q) %in% c("Group", "Class", "Level", "Section", "Statement")) |> (\(r) setNames(q, nm = c(colnames(q)[setdiff(seq_len(ncol(q)), r)], switch(as.character(length(r)), "4" = c("Estimate", "S.E.", "Est./S.E.", "P-Value"), "5" = c("Estimate", "Posterior S.D.", "One-Tailed P-Value", "Low 2.5% C.I.", "Upp 2.5% C.I.")))))())()

      }

      # StdY Standardization
      if (isTRUE("STDY Standardization" %in% names(gh5[[p]]))) {

        std.mod.result$stdy <<- .mplus.extract(p, std = "STDY Standardization", gh5 = gh5) |> (\(q) which(!colnames(q) %in% c("Group", "Class", "Level", "Section", "Statement")) |> (\(r) setNames(q, nm = c(colnames(q)[setdiff(seq_len(ncol(q)), r)], switch(as.character(length(r)), "4" = c("Estimate", "S.E.", "Est./S.E.", "P-Value"), "5" = c("Estimate", "Posterior S.D.", "One-Tailed P-Value", "Low 2.5% C.I.", "Upp 2.5% C.I.")))))())()

      }

      # StdX Standardization
      if (isTRUE("STD Standardization" %in% names(gh5[[p]]))) {

        std.mod.result$std <<- .mplus.extract(p, std = "STD Standardization", gh5 = gh5) |> (\(q) which(!colnames(q) %in% c("Group", "Class", "Level", "Section", "Statement")) |> (\(r) setNames(q, nm = c(colnames(q)[setdiff(seq_len(ncol(q)), r)], switch(as.character(length(r)), "4" = c("Estimate", "S.E.", "Est./S.E.", "P-Value"), "5" = c("Estimate", "Posterior S.D.", "One-Tailed P-Value", "Low 2.5% C.I.", "Upp 2.5% C.I.")))))())()

      }

    } else {

      std.mod.result <<- .mplus.extract(p, gh5 = gh5) |> (\(q) which(!colnames(q) %in% c("Group", "Class", "Level", "Section", "Statement")) |> (\(r) setNames(q, nm = c(colnames(q)[setdiff(seq_len(ncol(q)), r)], switch(as.character(length(r)), "1" = "Std Estimate", "3" = c("StdYX Estimate", "StdY Estimate", "Std Estimate")))))())()

    }

  })()

  #—————————————————————————————————————— #
  ### Confidence Intervals of Standardized Model Results ####

  "Confidence Intervals of Standardized Model Results" |> (\(p) if (isTRUE(p %in% names(gh5))) {

    # StdYX Standardization
    if (isTRUE("STDYX Standardization" %in% names(gh5[[p]]))) {

      conf.std.mod.result$stdyx <<- .mplus.extract(p, std = "STDYX Standardization", gh5 = gh5) |> (\(q) which(!colnames(q) %in% c("Group", "Class", "Level", "Section", "Statement")) |> (\(r) setNames(q, nm = c(colnames(q)[setdiff(seq_len(ncol(q)), r)], c("Low .5%",  "Low 2.5%", "Low 5%", "Estimate", "Upp 5%", "Upp 2.5%", "Upp .5%"))))())()

    }

    # StdY Standardization
    if (isTRUE("STDY Standardization" %in% names(gh5[[p]]))) {

      conf.std.mod.result$stdy <<- .mplus.extract(p, std = "STDY Standardization", gh5 = gh5) |> (\(q) which(!colnames(q) %in% c("Group", "Class", "Level", "Section", "Statement")) |> (\(r) setNames(q, nm = c(colnames(q)[setdiff(seq_len(ncol(q)), r)], c("Low .5%",  "Low 2.5%", "Low 5%", "Estimate", "Upp 5%", "Upp 2.5%", "Upp .5%"))))())()

    }

    # StdX Standardization
    if (isTRUE("STD Standardization" %in% names(gh5[[p]]))) {

      conf.std.mod.result$std <<- .mplus.extract(p, std = "STD Standardization", gh5 = gh5) |> (\(q) which(!colnames(q) %in% c("Group", "Class", "Level", "Section", "Statement")) |> (\(r) setNames(q, nm = c(colnames(q)[setdiff(seq_len(ncol(q)), r)], c("Low .5%",  "Low 2.5%", "Low 5%", "Estimate", "Upp 5%", "Upp 2.5%", "Upp .5%"))))())()

    }

  })()

  #—————————————————————————————————————— #
  ### Credibility Intervals of Standardized Model Results ####

  "Credibility Intervals of Standardized Model Results" |> (\(p) if (isTRUE(p %in% names(gh5))) {

    # StdYX Standardization
    if (isTRUE("STDYX Standardization" %in% names(gh5[[p]]))) {

      cred.std.mod.result$stdyx <<- .mplus.extract(p, std = "STDYX Standardization", gh5 = gh5) |> (\(q) which(!colnames(q) %in% c("Group", "Class", "Level", "Section", "Statement")) |> (\(r) setNames(q, nm = c(colnames(q)[setdiff(seq_len(ncol(q)), r)], c("Low .5%",  "Low 2.5%", "Low 5%", "Estimate", "Upp 5%", "Upp 2.5%", "Upp .5%"))))())()

    }

    # StdY Standardization
    if (isTRUE("STDY Standardization" %in% names(gh5[[p]]))) {

      cred.std.mod.result$stdy <<- .mplus.extract(p, std = "STDY Standardization", gh5 = gh5) |> (\(q) which(!colnames(q) %in% c("Group", "Class", "Level", "Section", "Statement")) |> (\(r) setNames(q, nm = c(colnames(q)[setdiff(seq_len(ncol(q)), r)], c("Low .5%",  "Low 2.5%", "Low 5%", "Estimate", "Upp 5%", "Upp 2.5%", "Upp .5%"))))())()

    }

    # StdX Standardization
    if (isTRUE("STD Standardization" %in% names(gh5[[p]]))) {

      cred.std.mod.result$std <<- .mplus.extract(p, std = "STD Standardization", gh5 = gh5) |> (\(q) which(!colnames(q) %in% c("Group", "Class", "Level", "Section", "Statement")) |> (\(r) setNames(q, nm = c(colnames(q)[setdiff(seq_len(ncol(q)), r)], c("Low .5%",  "Low 2.5%", "Low 5%", "Estimate", "Upp 5%", "Upp 2.5%", "Upp .5%"))))())()

    }

  })()

  #—————————————————————————————————————— #
  ### R-Square ####

  "R-Square" |> (\(p) if (isTRUE(p %in% names(gh5))) {

    rsquare <<- .mplus.extract(p, gh5 = gh5) |> (\(q) which(!colnames(q) %in% c("Group", "Class", "Level", "Section", "Variable", "Statement")) |> (\(r) setNames(q, nm = c(colnames(q)[setdiff(seq_len(ncol(q)), r)], switch(as.character(length(r)), "1" = "Estimate",  "2" = c("Estimate", "Residual Variance"), "4" = c("Estimate", "S.E.", "Est./S.E.", "P-Value"), "5" = c("Estimate", "Posterior S.D.", "One-Tailed P-Value", "Low 2.5% C.I.", "Upp 2.5% C.I.")))))())() |>
                    (\(s) s[, sapply(s, function(y) any(y != "R-Square")) & sapply(s, function(y) any(y != "Observed Variable"))])() |>
                    (\(t) if (isTRUE(any(sapply(t, function(y) any(y == "Latent Variable"))))) {

                      if (isTRUE(any(t$Section == "R-Square"))) {

                        t[t$Section == "R-Square", "Section"] <- "Observed Variable"

                        return(t)

                      } else {

                        return(t)

                      }

                    } else {

                      return(t)

                    })()

  })()

  #—————————————————————————————————————— #
  ### R-Square (configural) ####

  "R-Square (configural)" |> (\(p) if (isTRUE(p %in% names(gh5))) {

    rsquare.config <<- .mplus.extract(p, gh5 = gh5) |> (\(q) which(!colnames(q) %in% c("Group", "Class", "Level", "Section", "Variable", "Statement")) |> (\(r) setNames(q, nm = c(colnames(q)[setdiff(seq_len(ncol(q)), r)], switch(as.character(length(r)), "1" = "Estimate",  "2" = c("Estimate", "Residual Variance"), "4" = c("Estimate", "S.E.", "Est./S.E.", "P-Value"), "5" = c("Estimate", "Posterior S.D.", "One-Tailed P-Value", "Low 2.5% C.I.", "Upp 2.5% C.I.")))))())() |>
                          (\(s) s[, sapply(s, function(y) any(y != "R-Square")) & sapply(s, function(y) any(y != "Observed Variable"))])() |>
                          (\(t) if (isTRUE(any(sapply(t, function(y) any(y == "Latent Variable"))))) {

                            if (isTRUE(any(t$Section == "R-Square"))) {

                              t[t$Section == "R-Square", "Section"] <- "Observed Variable"

                              return(t)

                            } else {

                              return(t)

                            }

                          } else {

                            return(t)

      })()

  })()

  #—————————————————————————————————————— #
  ### R-Square (metric) ####

  "R-Square (metric)" |> (\(p) if (isTRUE(p %in% names(gh5))) {

    rsquare.metric <<- .mplus.extract(p, gh5 = gh5) |> (\(q) which(!colnames(q) %in% c("Group", "Class", "Level", "Section", "Variable", "Statement")) |> (\(r) setNames(q, nm = c(colnames(q)[setdiff(seq_len(ncol(q)), r)], switch(as.character(length(r)), "1" = "Estimate",  "2" = c("Estimate", "Residual Variance"), "4" = c("Estimate", "S.E.", "Est./S.E.", "P-Value"), "5" = c("Estimate", "Posterior S.D.", "One-Tailed P-Value", "Low 2.5% C.I.", "Upp 2.5% C.I.")))))())() |>
                            (\(s) s[, sapply(s, function(y) any(y != "R-Square")) & sapply(s, function(y) any(y != "Observed Variable"))])() |>
                            (\(t) if (isTRUE(any(sapply(t, function(y) any(y == "Latent Variable"))))) {

                              if (isTRUE(any(t$Section == "R-Square"))) {

                                t[t$Section == "R-Square", "Section"] <- "Observed Variable"

                                return(t)

                              } else {

                                return(t)

                              }

                            } else {

                              return(t)

                            })()

  })()

  #—————————————————————————————————————— #
  ### R-Square (scalar) ####

  "R-Square (scalar)" |> (\(p) if (isTRUE(p %in% names(gh5))) {

    rsquare.scalar <<- .mplus.extract(p, gh5 = gh5) |> (\(q) which(!colnames(q) %in% c("Group", "Class", "Level", "Section", "Variable", "Statement")) |> (\(r) setNames(q, nm = c(colnames(q)[setdiff(seq_len(ncol(q)), r)], switch(as.character(length(r)), "1" = "Estimate",  "2" = c("Estimate", "Residual Variance"), "4" = c("Estimate", "S.E.", "Est./S.E.", "P-Value"), "5" = c("Estimate", "Posterior S.D.", "One-Tailed P-Value", "Low 2.5% C.I.", "Upp 2.5% C.I.")))))())() |>
                          (\(s) s[, sapply(s, function(y) any(y != "R-Square")) & sapply(s, function(y) any(y != "Observed Variable"))])() |>
                          (\(t) if (isTRUE(any(sapply(t, function(y) any(y == "Latent Variable"))))) {

                            if (isTRUE(any(t$Section == "R-Square"))) {

                              t[t$Section == "R-Square", "Section"] <- "Observed Variable"

                              return(t)

                            } else {

                              return(t)

                            }

                          } else {

                            return(t)

                          })()

  })()

  #—————————————————————————————————————— #
  ### IRT Parameterization ####

  "IRT Parameterization" |> (\(p) if (isTRUE(p %in% names(gh5))) { irt.param <<- .mplus.extract(p, gh5 = gh5) |> (\(q) which(!colnames(q) %in% c("Group", "Class", "Level", "Section", "Variable", "Category", "Statement")) |> (\(r) setNames(q, nm = c(colnames(q)[setdiff(seq_len(ncol(q)), r)], if (isTRUE(summary.analysis$Estimator != "BAYES")) { c("Estimate", "S.E.", "Est./S.E.", "P-Value") } else if (isTRUE(summary.analysis$Estimator == "BAYES")) { c("Estimate", "Posterior S.D.", "One-Tailed P-Value", "Low 2.5% C.I.", "Upp 2.5% C.I.") })))())() })()

  #—————————————————————————————————————— #
  ### IRT Parameterization (configural) ####

  "IRT Parameterization (configural)" |> (\(p) if (isTRUE(p %in% names(gh5))) { irt.param <<- .mplus.extract(p, gh5 = gh5) |> (\(q) which(!colnames(q) %in% c("Group", "Class", "Level", "Section", "Variable", "Category", "Statement")) |> (\(r) setNames(q, nm = c(colnames(q)[setdiff(seq_len(ncol(q)), r)], if (isTRUE(summary.analysis$Estimator != "BAYES")) { c("Estimate", "S.E.", "Est./S.E.", "P-Value") } else if (isTRUE(summary.analysis$Estimator == "BAYES")) { c("Estimate", "Posterior S.D.", "One-Tailed P-Value", "Low 2.5% C.I.", "Upp 2.5% C.I.") })))())() })()

  #—————————————————————————————————————— #
  ### IRT Parameterization (metric) ####

  "IRT Parameterization (metric)" |> (\(p) if (isTRUE(p %in% names(gh5))) { irt.param <<- .mplus.extract(p, gh5 = gh5) |> (\(q) which(!colnames(q) %in% c("Group", "Class", "Level", "Section", "Variable", "Category", "Statement")) |> (\(r) setNames(q, nm = c(colnames(q)[setdiff(seq_len(ncol(q)), r)], if (isTRUE(summary.analysis$Estimator != "BAYES")) { c("Estimate", "S.E.", "Est./S.E.", "P-Value") } else if (isTRUE(summary.analysis$Estimator == "BAYES")) { c("Estimate", "Posterior S.D.", "One-Tailed P-Value", "Low 2.5% C.I.", "Upp 2.5% C.I.") })))())() })()

  #—————————————————————————————————————— #
  ### IRT Parameterization (scalar) ####

  "IRT Parameterization (scalar)" |> (\(p) if (isTRUE(p %in% names(gh5))) { irt.param <<- .mplus.extract(p, gh5 = gh5) |> (\(q) which(!colnames(q) %in% c("Group", "Class", "Level", "Section", "Variable", "Category", "Statement")) |> (\(r) setNames(q, nm = c(colnames(q)[setdiff(seq_len(ncol(q)), r)], if (isTRUE(summary.analysis$Estimator != "BAYES")) { c("Estimate", "S.E.", "Est./S.E.", "P-Value") } else if (isTRUE(summary.analysis$Estimator == "BAYES")) { c("Estimate", "Posterior S.D.", "One-Tailed P-Value", "Low 2.5% C.I.", "Upp 2.5% C.I.") })))())() })()

  #—————————————————————————————————————— #
  ### Model Modification Indices ####

  "Model Modification Indices" |> (\(p) if (isTRUE(p %in% names(gh5))) {

    modind <<- .mplus.extract(p, gh5 = gh5) |> (\(q) if (isTRUE(!is.null(q))) { which(!colnames(q) %in% c("Group", "Class", "Level", "Section", "Statement", "Variable", "Category")) |> (\(r) setNames(q, nm = c(colnames(q)[setdiff(seq_len(ncol(q)), r)], c("M.I.", "E.P.C.", "Std E.P.C.", "StdYX E.P.C."))))() } else { list() })()

    # Minimum value and message
    hdf5r::h5attributes(gh5[[p]]) |> (\(q) if (isTRUE(length(q) == 1L)) {

        attr(modind, "Minimum value") <<- q

      } else if (isTRUE(length(q) != 1L)) {

        attr(modind, "Minimum value") <<- q[[1L]]
        attr(modind, "Message") <<- q[[2L]]

      })()

  })()

  #—————————————————————————————————————— #
  ### Results in Probability Scale ####

  "Results in Probability Scale" |> (\(p) if (isTRUE(p %in% names(gh5))) { prob.scale <<- .mplus.extract(p, gh5 = gh5) |> (\(q) which(!colnames(q) %in% c("Group", "Class", "Level", "Section", "Statement", "Variable", "Category")) |> (\(r) setNames(q, nm = c(colnames(q)[setdiff(seq_len(ncol(q)), r)], switch(as.character(length(r)), "1" = "Estimate", "4" = c("Estimate", "S.E.", "Est./S.E.", "P-Value")))))())() })()

  #—————————————————————————————————————— #
  ### Results in Probability Scale (configural) ####

  "Results in Probability Scale (configural)" |> (\(p) if (isTRUE(p %in% names(gh5))) { prob.scale.config <<- .mplus.extract(p, gh5 = gh5) |> (\(q) which(!colnames(q) %in% c("Group", "Class", "Level", "Section", "Statement", "Variable", "Category")) |> (\(r) setNames(q, nm = c(colnames(q)[setdiff(seq_len(ncol(q)), r)], switch(as.character(length(r)), "1" = "Estimate", "4" = c("Estimate", "S.E.", "Est./S.E.", "P-Value")))))())() })()

  #—————————————————————————————————————— #
  ### Results in Probability Scale (metric) ####

  "Results in Probability Scale (configural)" |> (\(p) if (isTRUE(p %in% names(gh5))) { prob.scale.metric <<- .mplus.extract(p, gh5 = gh5) |> (\(q) which(!colnames(q) %in% c("Group", "Class", "Level", "Section", "Statement", "Variable", "Category")) |> (\(r) setNames(q, nm = c(colnames(q)[setdiff(seq_len(ncol(q)), r)], switch(as.character(length(r)), "1" = "Estimate", "4" = c("Estimate", "S.E.", "Est./S.E.", "P-Value")))))())() })()

  #—————————————————————————————————————— #
  ### Results in Probability Scale (scalar) ####

  "Results in Probability Scale (configural)" |> (\(p) if (isTRUE(p %in% names(gh5))) { prob.scale.scalar <<- .mplus.extract(p, gh5 = gh5) |> (\(q) which(!colnames(q) %in% c("Group", "Class", "Level", "Section", "Statement", "Variable", "Category")) |> (\(r) setNames(q, nm = c(colnames(q)[setdiff(seq_len(ncol(q)), r)], switch(as.character(length(r)), "1" = "Estimate", "4" = c("Estimate", "S.E.", "Est./S.E.", "P-Value")))))())() })()

  #—————————————————————————————————————— #
  ### Logistic Regression Odds Ratio Results ####

  "Logistic Regression Odds Ratio Results" |> (\(p) if (isTRUE(p %in% names(gh5))) { odds.ratio <<- .mplus.extract(p, gh5 = gh5) |> (\(q) which(!colnames(q) %in% c("Group", "Class", "Level", "Section", "Statement", "Variable", "Category")) |> (\(r) setNames(q, nm = c(colnames(q)[setdiff(seq_len(ncol(q)), r)], switch(as.character(length(r)), "1" = "Estimate", "4" = c("Estimate", "S.E.", "Low 2.5%", "Upp 2.5%"), "6" = c("Estimate", "Posterior S.D.", "One-Tailed P-Value", "Low 2.5% C.I.", "Upp 2.5% C.I.", "Sig.")))))())() })()

  #—————————————————————————————————————— #
  ### Confidence Intervals in Probability Scale ####

  "Confidence Intervals in Probability Scale" |> (\(p) if (isTRUE(p %in% names(gh5))) { conf.prob.scale <<- .mplus.extract(p, gh5 = gh5) |> (\(q) which(!colnames(q) %in% c("Group", "Class", "Level", "Section", "Statement", "Variable", "Category")) |> (\(r) setNames(q, nm = c(colnames(q)[setdiff(seq_len(ncol(q)), r)], c("Low .5%",  "Low 2.5%", "Low 5%", "Estimate", "Upp 5%", "Upp 2.5%", "Upp .5%"))))())() })()

  #—————————————————————————————————————— #
  ### Confidence Intervals in Probability Scale (configural) ####

  "Confidence Intervals in Probability Scale (configural)" |> (\(p) if (isTRUE(p %in% names(gh5))) { conf.prob.scale.config <<- .mplus.extract(p, gh5 = gh5) |> (\(q) which(!colnames(q) %in% c("Group", "Class", "Level", "Section", "Statement", "Variable", "Category")) |> (\(r) setNames(q, nm = c(colnames(q)[setdiff(seq_len(ncol(q)), r)], c("Low .5%",  "Low 2.5%", "Low 5%", "Estimate", "Upp 5%", "Upp 2.5%", "Upp .5%"))))())() })()

  #—————————————————————————————————————— #
  ### Confidence Intervals in Probability Scale (metric) ####

  "Confidence Intervals in Probability Scale (metric)" |> (\(p) if (isTRUE(p %in% names(gh5))) { conf.prob.scale.metric <<- .mplus.extract(p, gh5 = gh5) |> (\(q) which(!colnames(q) %in% c("Group", "Class", "Level", "Section", "Statement", "Variable", "Category")) |> (\(r) setNames(q, nm = c(colnames(q)[setdiff(seq_len(ncol(q)), r)], c("Low .5%",  "Low 2.5%", "Low 5%", "Estimate", "Upp 5%", "Upp 2.5%", "Upp .5%"))))())() })()

  #—————————————————————————————————————— #
  ### Confidence Intervals in Probability Scale (scalar) ####

  "Confidence Intervals in Probability Scale (scalar)" |> (\(p) if (isTRUE(p %in% names(gh5))) { conf.prob.scale.scalar <<- .mplus.extract(p, gh5 = gh5) |> (\(q) which(!colnames(q) %in% c("Group", "Class", "Level", "Section", "Statement", "Variable", "Category")) |> (\(r) setNames(q, nm = c(colnames(q)[setdiff(seq_len(ncol(q)), r)], c("Low .5%",  "Low 2.5%", "Low 5%", "Estimate", "Upp 5%", "Upp 2.5%", "Upp .5%"))))())() })()

  #—————————————————————————————————————— #
  ### Credibility Intervals in Probability Scale ####

  "Credibility Intervals in Probability Scale" |> (\(p) if (isTRUE(p %in% names(gh5))) { cred.prob.scale <<- .mplus.extract(p, gh5 = gh5) |> (\(q) which(!colnames(q) %in% c("Group", "Class", "Level", "Section", "Statement", "Variable", "Category")) |> (\(r) setNames(q, nm = c(colnames(q)[setdiff(seq_len(ncol(q)), r)], c("Low .5%",  "Low 2.5%", "Low 5%", "Estimate", "Upp 5%", "Upp 2.5%", "Upp .5%"))))())() })()

  #—————————————————————————————————————— #
  ### Credibility Intervals in Probability Scale (configural) ####

  "Credibility Intervals in Probability Scale (configural)" |> (\(p) if (isTRUE(p %in% names(gh5))) { cred.prob.scale.config <<- .mplus.extract(p, gh5 = gh5) |> (\(q) which(!colnames(q) %in% c("Group", "Class", "Level", "Section", "Statement", "Variable", "Category")) |> (\(r) setNames(q, nm = c(colnames(q)[setdiff(seq_len(ncol(q)), r)], c("Low .5%",  "Low 2.5%", "Low 5%", "Estimate", "Upp 5%", "Upp 2.5%", "Upp .5%"))))())() })()

  #—————————————————————————————————————— #
  ### Credibility Intervals in Probability Scale (metric) ####

  "Credibility Intervals in Probability Scale (metric)" |> (\(p) if (isTRUE(p %in% names(gh5))) { cred.prob.scale.metric <<- .mplus.extract(p, gh5 = gh5) |> (\(q) which(!colnames(q) %in% c("Group", "Class", "Level", "Section", "Statement", "Variable", "Category")) |> (\(r) setNames(q, nm = c(colnames(q)[setdiff(seq_len(ncol(q)), r)], c("Low .5%",  "Low 2.5%", "Low 5%", "Estimate", "Upp 5%", "Upp 2.5%", "Upp .5%"))))())() })()

  #—————————————————————————————————————— #
  ### Credibility Intervals in Probability Scale (scalar) ####

  "Credibility Intervals in Probability Scale (scalar)" |> (\(p) if (isTRUE(p %in% names(gh5))) { cred.prob.scale.scalar <<- .mplus.extract(p, gh5 = gh5) |> (\(q) which(!colnames(q) %in% c("Group", "Class", "Level", "Section", "Statement", "Variable", "Category")) |> (\(r) setNames(q, nm = c(colnames(q)[setdiff(seq_len(ncol(q)), r)], c("Low .5%",  "Low 2.5%", "Low 5%", "Estimate", "Upp 5%", "Upp 2.5%", "Upp .5%"))))())() })()

  #—————————————————————————————————————— #
  ### Latent Class Indicator Odds Ratios for the Latent Classes ####

  "Latent Class Indicator Odds Ratios for the Latent Classes" |> (\(p) if (isTRUE(p %in% names(gh5))) { ind.odds.ratio <<- data.frame(gh5[[p]][["Statements"]] |> (\(q) misty::chr.trim(q[seq_len(q$maxdims[1L]), seq_len(q$maxdims[2L])]))() |> (\(r) if (isTRUE(is.null(dim(r)))) { matrix(r, ncol = length(r)) } else { r })() |> (\(s) data.frame(Section = s[, 1L], "Variable" = s[, 2L], Category = s[, 3L], check.names = FALSE))(), gh5[[p]][["Results"]] |> (\(q) q[seq_len(q$maxdims[1L]), seq_len(q$maxdims[2L])])() |> (\(r) if (isTRUE(is.null(dim(r)))) { as.data.frame(matrix(r, ncol = length(r))) } else { as.data.frame(r) })() |> (\(s) setNames(s, nm = c("Estimate", "S.E.", "Low 2.5%", "Upp 2.5%")))(), check.names = FALSE) })()

  #—————————————————————————————————————— #
  ### Latent Class Indicator Odds Ratios for the Latent Classes (configural) ####

  "Latent Class Indicator Odds Ratios for the Latent Classes (configural)" |> (\(p) if (isTRUE(p %in% names(gh5))) { ind.odds.ratio.config <<- data.frame(gh5[[p]][["Statements"]] |> (\(q) misty::chr.trim(q[seq_len(q$maxdims[1L]), seq_len(q$maxdims[2L])]))() |> (\(r) if (isTRUE(is.null(dim(r)))) { matrix(r, ncol = length(r)) } else { r })() |> (\(s) data.frame(Section = s[, 1L], "Variable" = s[, 2L], Category = s[, 3L], check.names = FALSE))(), gh5[[p]][["Results"]] |> (\(q) q[seq_len(q$maxdims[1L]), seq_len(q$maxdims[2L])])() |> (\(r) if (isTRUE(is.null(dim(r)))) { as.data.frame(matrix(r, ncol = length(r))) } else { as.data.frame(r) })() |> (\(s) setNames(s, nm = c("Estimate", "S.E.", "Low 2.5%", "Upp 2.5%")))(), check.names = FALSE) })()

  #—————————————————————————————————————— #
  ### Latent Class Indicator Odds Ratios for the Latent Classes (metric) ####

  "Latent Class Indicator Odds Ratios for the Latent Classes (metric)" |> (\(p) if (isTRUE(p %in% names(gh5))) { ind.odds.ratio.metric <<- data.frame(gh5[[p]][["Statements"]] |> (\(q) misty::chr.trim(q[seq_len(q$maxdims[1L]), seq_len(q$maxdims[2L])]))() |> (\(r) if (isTRUE(is.null(dim(r)))) { matrix(r, ncol = length(r)) } else { r })() |> (\(s) data.frame(Section = s[, 1L], "Variable" = s[, 2L], Category = s[, 3L], check.names = FALSE))(), gh5[[p]][["Results"]] |> (\(q) q[seq_len(q$maxdims[1L]), seq_len(q$maxdims[2L])])() |> (\(r) if (isTRUE(is.null(dim(r)))) { as.data.frame(matrix(r, ncol = length(r))) } else { as.data.frame(r) })() |> (\(s) setNames(s, nm = c("Estimate", "S.E.", "Low 2.5%", "Upp 2.5%")))(), check.names = FALSE) })()

  #—————————————————————————————————————— #
  ### Latent Class Indicator Odds Ratios for the Latent Classes (scalar) ####

  "Latent Class Indicator Odds Ratios for the Latent Classes (scalar)" |> (\(p) if (isTRUE(p %in% names(gh5))) { ind.odds.ratio.scalar <<- data.frame(gh5[[p]][["Statements"]] |> (\(q) misty::chr.trim(q[seq_len(q$maxdims[1L]), seq_len(q$maxdims[2L])]))() |> (\(r) if (isTRUE(is.null(dim(r)))) { matrix(r, ncol = length(r)) } else { r })() |> (\(s) data.frame(Section = s[, 1L], "Variable" = s[, 2L], Category = s[, 3L], check.names = FALSE))(), gh5[[p]][["Results"]] |> (\(q) q[seq_len(q$maxdims[1L]), seq_len(q$maxdims[2L])])() |> (\(r) if (isTRUE(is.null(dim(r)))) { as.data.frame(matrix(r, ncol = length(r))) } else { as.data.frame(r) })() |> (\(s) setNames(s, nm = c("Estimate", "S.E.", "Low 2.5%", "Upp 2.5%")))(), check.names = FALSE) })()

  #—————————————————————————————————————— #
  ### Brant Wald Test For Proportional Odds ####

  "Brant Wald Test For Proportional Odds" |> (\(p) if (isTRUE(p %in% names(gh5))) { brant.wald <<- data.frame(gh5[[p]][["Statements"]] |> (\(q) misty::chr.trim(q[seq_len(q$maxdims[1L]), seq_len(q$maxdims[2L])]))() |> (\(r) if (isTRUE(is.null(dim(r)))) { matrix(r, ncol = length(r)) } else { r })() |> (\(s) data.frame(Variable = s[, 1L], "Test/Variable" = misty::chr.trim(apply(s[, -1, drop = FALSE], 1L, paste, collapse = " "), check = FALSE), check.names = FALSE))(), gh5[[p]][["Results"]] |> (\(q) q[seq_len(q$maxdims[1L]), seq_len(q$maxdims[2L])])() |> (\(r) if (isTRUE(is.null(dim(r)))) { as.data.frame(matrix(r, ncol = length(r))) } else { as.data.frame(r) })() |> (\(s) setNames(s, nm = if (isTRUE(summary.analysis$Estimator != "BAYES")) { c("Chi-Square", "df", "P-Value") } else if (isTRUE(summary.analysis$Estimator == "BAYES")) { c("Estimate", "Posterior S.D.", "One-Tailed P-Value", "Low 2.5% C.I.", "Upp 2.5% C.I.", "Sig.") }))(), check.names = FALSE) })()

  #—————————————————————————————————————— #
  ### Within-level Standardized Model Results For Each Cluster ####

  "Within-level Standardized Model Results For Each Cluster" |> (\(p) if (isTRUE(p %in% names(gh5))) {

    if (isTRUE(any(c("STDYX Standardization", "STDY Standardization", "STD Standardization") %in% names(gh5[[p]])))) {

      # StdYX Standardization
      if (isTRUE("STDYX Standardization" %in% names(gh5[[p]]))) {

        within.std.mod.result$stdyx <<- .mplus.extract(p, std = "STDYX Standardization", gh5 = gh5) |> (\(q) which(!colnames(q) %in% c("Group", "Cluster", "Class", "Level", "Section", "Statement")) |> (\(r) setNames(q, nm = c(colnames(q)[setdiff(seq_len(ncol(q)), r)], switch(as.character(length(r)), "5" = c("Estimate", "Posterior S.D.", "One-Tailed P-Value", "Low 2.5% C.I.", "Upp 2.5% C.I."), "6" = c("Estimate", "Posterior S.D.", "One-Tailed P-Value", "Low 2.5% C.I.", "Upp 2.5% C.I.", "Sig.")))))())()

      }

      # StdY Standardization
      if (isTRUE("STDY Standardization" %in% names(gh5[[p]]))) {

        within.std.mod.result$stdy <<- .mplus.extract(p, std = "STDY Standardization", gh5 = gh5) |> (\(q) which(!colnames(q) %in% c("Group", "Cluster", "Class", "Level", "Section", "Statement")) |> (\(r) setNames(q, nm = c(colnames(q)[setdiff(seq_len(ncol(q)), r)], switch(as.character(length(r)), "5" = c("Estimate", "Posterior S.D.", "One-Tailed P-Value", "Low 2.5% C.I.", "Upp 2.5% C.I."), "6" = c("Estimate", "Posterior S.D.", "One-Tailed P-Value", "Low 2.5% C.I.", "Upp 2.5% C.I.", "Sig.")))))())()

      }

      # StdX Standardization
      if (isTRUE("STD Standardization" %in% names(gh5[[p]]))) {

        within.std.mod.result$std <<- .mplus.extract(p, std = "STD Standardization", gh5 = gh5) |> (\(q) which(!colnames(q) %in% c("Group", "Cluster", "Class", "Level", "Section", "Statement")) |> (\(r) setNames(q, nm = c(colnames(q)[setdiff(seq_len(ncol(q)), r)], switch(as.character(length(r)), "5" = c("Estimate", "Posterior S.D.", "One-Tailed P-Value", "Low 2.5% C.I.", "Upp 2.5% C.I."), "6" = c("Estimate", "Posterior S.D.", "One-Tailed P-Value", "Low 2.5% C.I.", "Upp 2.5% C.I.", "Sig.")))))())()

      }

    } else {

      within.std.mod.result <<- .mplus.extract(p, gh5 = gh5) |> (\(q) which(!colnames(q) %in% c("Group", "Cluster", "Class", "Level", "Section", "Statement")) |> (\(r) setNames(q, nm = c(colnames(q)[setdiff(seq_len(ncol(q)), r)], c("StdYX Estimate", "StdY Estimate", "Std Estimate"))))())()

    }

  })()

  #—————————————————————————————————————— #
  ### Within-level R-Square For Each Cluster ####

  "Within-level R-Square For Each Cluster" |> (\(p) if (isTRUE(p %in% names(gh5))) { within.rsquare <<- .mplus.extract(p, gh5 = gh5) |> (\(q) which(!colnames(q) %in% c("Cluster", "Cluster", "Group", "Class", "Level", "Section", "Statement", "Variable", "Category")) |> (\(r) setNames(q, nm = c(colnames(q)[setdiff(seq_len(ncol(q)), r)], c("Estimate", "Posterior S.D.", "One-Tailed P-Value", "Low 2.5% C.I.", "Upp 2.5% C.I."))))())() })()

  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  ## Close Mplus GH5 File ####

  hdf5r::h5close(gh5)

  #_____________________________________________________________________________
  #
  # Return Object ——————————————————————————————————————————————————————————————

  object <- Filter(Negate(is.null), list(input = input, warn = warn, summary.analysis = summary.analysis, fit = fit, fit.config = fit.config, fit.metric = fit.metric, fit.scalar = fit.scalar, mod.result = mod.result, mod.result.config = mod.result.config, mod.result.metric = mod.result.metric, mod.result.scalar = mod.result.scalar, conf.mod.result = conf.mod.result, conf.mod.result.config = conf.mod.result.config, conf.mod.result.metric = conf.mod.result.metric, conf.mod.result.scalar = conf.mod.result.scalar, cred.mod.result = cred.mod.result, cred.mod.result.config = cred.mod.result.config, cred.mod.result.metric = cred.mod.result.metric, cred.mod.result.scalar = cred.mod.result.scalar, std.mod.result = std.mod.result, conf.std.mod.result = conf.std.mod.result, cred.std.mod.result = cred.std.mod.result, rsquare = rsquare, rsquare.config = rsquare.config, rsquare.metric = rsquare.metric, rsquare.scalar = rsquare.scalar, irt.param = irt.param, irt.param.config = irt.param.config, irt.param.metric = irt.param.metric, irt.param.scalar = irt.param.scalar, modind = modind, prob.scale = prob.scale, prob.scale.config = prob.scale.config, prob.scale.metric = prob.scale.metric, prob.scale.scalar = prob.scale.scalar, odds.ratio = odds.ratio, conf.prob.scale = conf.prob.scale, conf.prob.scale.config = conf.prob.scale.config, conf.prob.scale.metric = conf.prob.scale.metric, conf.prob.scale.scalar = conf.prob.scale.scalar, cred.prob.scale = cred.prob.scale, cred.prob.scale.config = cred.prob.scale.config, cred.prob.scale.metric = cred.prob.scale.metric, cred.prob.scale.scalar = cred.prob.scale.scalar, ind.odds.ratio = ind.odds.ratio, ind.odds.ratio.config = ind.odds.ratio.config, ind.odds.ratio.metric = ind.odds.ratio.metric, ind.odds.ratio.scalar = ind.odds.ratio.scalar, brant.wald = brant.wald, within.std.mod.result = within.std.mod.result, within.rsquare = within.rsquare))

  #_____________________________________________________________________________
  #
  # Print Object ———————————————————————————————————————————————————————————————

  if (isTRUE(all(print != "none"))) {

    if (isTRUE(all(print == "input"))) { result.print <- c("input", "warn") } else { result.print <- c(setdiff(print, "result") |> (\(p) if (isTRUE("input" %in% p)) { c("input", "warn") } else { p } )(), result) }

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ## Results Available ####

    if (isTRUE(any(result.print %in% names(object)))) {

      # Available results
      result.print <- result.print[result.print %in% names(object)]

      # Send R output to text file
      if (isTRUE(grepl("\\.txt", write))) {

        sink(file = write, append = ifelse(isTRUE(file.exists(write)), append, FALSE), type = "output", split = TRUE)

        if (isTRUE(append && file.exists(write))) { write("", file = write, append = TRUE) }

      }

      for (i in result.print) {

        #—————————————————————————————————————— #
        ### Input Instructions and Errors and Warnings ####

        if (isTRUE(i %in% c("input", "warn"))) {

          cat(object[[i]], "\n")

        #—————————————————————————————————————— #
        ### Result Sections ####

        } else {

          if (i %in% names(object)) {

            cat(switch(i,
                       "summary.analysis" = "Summary of Analysis",
                       "fit" = "Model Fit Information",
                       "fit.config" = "Model Fit Information (configural)",
                       "fit.metric" = "Model Fit Information (metric)" ,
                       "fit.scalar" = "Model Fit Information (scalar)",
                       "mod.result" = "Model Results",
                       "mod.result.config" = "Model Results (configural)",
                       "mod.result.metric" = "Model Results (metric)",
                       "mod.result.scalar" = "Model Results (scalar)",
                       "conf.mod.result" = "Confidence Intervals of Model Results",
                       "conf.mod.result.config" = "Confidence Intervals of Model Results (configural)",
                       "conf.mod.result.metric" = "Confidence Intervals of Model Results (metric)",
                       "conf.mod.result.scalar" = "Confidence Intervals of Model Results (scalar)",
                       "cred.mod.result" = "Credibility Intervals of Model Results",
                       "cred.mod.result.config" = "Credibility Intervals of Model Results (configural)",
                       "cred.mod.result.metric" = "Credibility Intervals of Model Results (metric)",
                       "cred.mod.result.scalar" = "Credibility Intervals of Model Results (scalar)",
                       "std.mod.result" = "Standardized Model Results",
                       "conf.std.mod.result" = "Confidence Intervals of Standardized Model Results",
                       "cred.std.mod.result" = "Credibility Intervals of Standardized Model Results",
                       "rsquare" = "R-Square",
                       "rsquare.config" = "R-Square (configural)",
                       "rsquare.metric" = "R-Square (metric)",
                       "rsquare.scalar" = "R-Square (scalar)",
                       "irt.param" = "IRT Parameterization",
                       "irt.param.config" = "IRT Parameterization (configural)",
                       "irt.param.metric" = "IRT Parameterization (metric)",
                       "irt.param.scalar" = "IRT Parameterization (scalar)",
                       "modind" = "Model Modification Indices",
                       "prob.scale" = "Results in Probability Scale",
                       "prob.scale.config" = "Results in Probability Scale (configural)",
                       "prob.scale.metric" = "Results in Probability Scale (metric)",
                       "prob.scale.scalar" = "Results in Probability Scale (scalar)",
                       "odds.ratio" = "Logistic Regression Odds Ratio Results",
                       "conf.prob.scale" = "Confidence Intervals in Probability Scale",
                       "conf.prob.scale.config" = "Confidence Intervals in Probability Scale (configural)",
                       "conf.prob.scale.metric" = "Confidence Intervals in Probability Scale (metric)",
                       "conf.prob.scale.scalar" = "Confidence Intervals in Probability Scale (scalar)",
                       "cred.prob.scale" = "Credibility Intervals in Probability Scale",
                       "cred.prob.scale.config" = "Credibility Intervals in Probability Scale (configural)",
                       "cred.prob.scale.metric" = "Credibility Intervals in Probability Scale (metric)",
                       "cred.prob.scale.scalar" = "Credibility Intervals in Probability Scale (scalar)",
                       "ind.odds.ratio" = "Latent Class Indicator Odds Ratios for the Latent Classes",
                       "ind.odds.ratio.config" = "Latent Class Indicator Odds Ratios for the Latent Classes (configural)",
                       "ind.odds.ratio.metric" = "Latent Class Indicator Odds Ratios for the Latent Classes (metric)",
                       "ind.odds.ratio.scalar" = "Latent Class Indicator Odds Ratios for the Latent Classes (scalar)",
                       "brant.wald" = "Brant Wald Test For Proportional Odds",
                       "within.std.mod.result" = "Within-level Standardized Model Results For Each Cluster",
                       "within.rsquare" = "Within-level R-Square For Each Cluster"), "\n")

          object[[i]] |> (\(p) {

            #···················
            #### Round ####

            if (isTRUE(!is.null(digits))) {

              if (isTRUE(is.data.frame(p))) {

                print(misty::df.round(p, digits = digits))

              } else if (i != "summary.analysis") {

                # (Confidence or Credible Intervals of) Standardized Model Results
                if (isTRUE(all(sapply(p, is.data.frame)))) {

                  print(lapply(p, function(y) misty::df.round(y, digits = digits)))

                # Multiple Imputation or Monte Carlo Simulation
                } else {

                  print(lapply(p, function(y) lapply(y, function(z) if (isTRUE(!is.data.frame(z))) { lapply(z, function(v) misty::df.round(v, digits = digits, check = FALSE)) } else { misty::df.round(z, digits = digits, check = FALSE) })))

                }

              } else {

                print(p)

              }

            #···················
            #### No Rounding ####

            } else {

              print(p)

            }

          })()

          }

        }

        if (isTRUE(!i %in% c("summary.analysis", "std.mod.result", "conf.std.mod.result", "cred.std.mod.result", "within.std.mod.result", rev(result.print)[1L]))) { cat("\n") }

        if (isTRUE(i == "std.mod.result" && is.data.frame(object[[i]]))) { cat("\n")}

      }

      # Close file connection
      if (isTRUE(grepl("\\.txt", write))) { sink() }

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ## Results Not Available ####

    } else {

      warning(if (isTRUE(length(result.print) == 1L)) { "Result section requested in the argument 'result' is not available" } else { "Result sections requested in the argument 'result' are all not available"  }, call. = FALSE)

    }

  }

  #_____________________________________________________________________________
  #
  # Return —————————————————————————————————————————————————————————————————————

  return(invisible(object))

}

#_____________________________________________________________________________

Try the misty package in your browser

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

misty documentation built on Nov. 24, 2026, 5:06 p.m.