michaelhallquist / MplusAutomation

The MplusAutomation package leverages the flexibility of the R language to automate latent variable model estimation and interpretation using Mplus, a powerful latent variable modeling program developed by Muthen and Muthen (www.statmodel.com). Specifically, MplusAutomation provides routines for creating related groups of models, running batches of models, and extracting and tabulating model parameters and fit statistics.
81 stars 46 forks source link

Issue with mixtureSummaryTable and readModels #206

Open linem7 opened 2 months ago

linem7 commented 2 months ago

Hi,

I'm currently using the mixtureSummaryTable function in the MplusAutomation package to summarize several mixture models, particularly for comparing model fits. I've encountered an issue where the objects returned by readModels("nameofMplusoutfile.out", what="summaries")$summaries don't seem to function as effectively as extractModelSummaries, which is now deprecated.

Could you please provide any guidance or workaround to address this issue? Any help would be greatly appreciated!

Thanks!

linem7 commented 2 months ago

I'd like to add some more details regarding the issue I mentioned. While the summaryTable can read and display the object returned by the readModels, certain indices such as min_N and max_N for mixture models are still not available. This makes it challenging as I still require a suitable object format, like what was returned by the now-deprecated extractModelSummaries, which could be processed by mixtureSummaryTable.

linem7 commented 1 month ago

Hi everyone,

I would like to share my solution addressing this issue,

# Read information from output file
res <- readModels("./LCA") # Total 5 models are imported

# Function to enhance the SummaryTable with class proportions
summary_with_proportions <- function(res, keepCols) {
  # Retrieve the initial summary table
  summary_data <- SummaryTable(res_post, keepCols = keepCols) %>%
    select({{ keepCols }})

  # Extract and format the class proportions for each model
  proportions_data <- map(res, ~ {
    proportions <- .x[["class_counts"]][["mostLikely"]][["proportion"]]
    proportions <- sort(proportions, decreasing = TRUE)
    proportion_string <- paste(formatC(proportions * 100, format = "f", digits = 1), collapse = "/")
    return(proportion_string)
  })

  # Combine the proportion data with the summary table
  summary_data$"Proportion (%)" <- unlist(proportions_data)

  return(summary_data)
}

# Assuming 'res' is your list of models
summary_with_proportions (res, c("Title", "AIC", "BIC", "aBIC", "Entropy", "T11_LMR_PValue", "BLRT_PValue"))

and the corresponding result looks like below, where the proportion for each class will be sorted in descending order and separated by a slash. This may help you consider whether there is enough proportion in each class.

       Title      AIC      BIC     aBIC Entropy T11_LMR_PValue BLRT_PValue         Proportion (%)
1  1 classes 5957.652 5998.196 5966.462      NA             NA          NA                  100.0
2  2 classes 5730.675 5803.655 5746.534   0.953         0.0000           0              76.3/23.7
3  3 classes 5595.470 5700.886 5618.378   0.932         0.3342           0          69.5/21.8/8.7
4  4 classes 5090.423 5228.274 5120.379   1.000         0.3240           0      60.3/24.4/9.4/5.9
5  5 classes 4867.573 5037.859 4904.578   0.980         0.0377           0 56.8/18.8/16.0/4.9/3.5

I hope this function will be of some help to your work.