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

Enhancement Request for plotMixtures Function to Include "Probability" Information #207

Open linem7 opened 2 months ago

linem7 commented 2 months ago

Hi,

I've been using the plotMixtures function to visualize several mixture models that are constructed with both continuous and binary indicators. Currently, the function allows plotting based on "Means", "Thresholds", or "Intercepts" information. However, for binary indicators, this method may not provide the most intuitive interpretation since higher thresholds correspond to lower probabilities of category endorsement.

It would be incredibly helpful if the function could also plot the "Probability" information for each categorical indicator, providing a more direct interpretation of the model results. Is it possible to add this feature to the plotMixtures function?

Thank you for considering this enhancement. Your efforts are greatly appreciated!

cjvanlissa commented 2 months ago

Dear @linem7 ,

this functionality is in tidySEM, but not for Mplus mixture models. However, it's possible to adapt those functions with not too much effort.

linem7 commented 2 months ago

Dear @cjvanlissa ,

Thank you for your prompt reply and for the guidance provided. I have followed up on the documentation for tidySEM and encountered a limitation: functions such as plot_profiles, plot_density, or plot_prob do not seem to be able to process information from Mplus outputs using the readModels function in MplusAutomation.

Additionally, I suspect that the complexity of my model, which includes both binary and continuous indicators, might be contributing to the issue. The plotting data for continuous indicators are typically retrieved from the 'model result' section in Mplus, whereas binary indicators require data from 'results in probability scale'.

Is there a recommended approach or workaround that could allow integration of these mixed-indicator outputs into tidySEM for visualization? Any further insights or suggestions would be immensely helpful.

Thank you again for your time and assistance.

cjvanlissa commented 2 months ago

Dear Linem, that's what I'm saying: you'd need to edit the functions yourself to use them for mplus output. You can plot the binary and continuous indicators separately, or plot the thresholds.

linem7 commented 2 months ago

Thank you for your clarifying. I will try to convert the data and plot them in an integrated format. Thanks again for your time and assistance.

linem7 commented 1 month ago

Hi everyone,

I would like to share a function which addresses this issue. I hope it helps in your work.

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

# Exact plot data
extract_data <- function(model, class_number) {
  # Extracting 'Means' data
  means_data <- model[["parameters"]][["unstandardized"]] %>%
    filter(paramHeader == "Means", !str_detect(LatentClass, "^Categorical")) %>%
    select(param, est, LatentClass)

  # Extracting 'probability.scale' data
  prob_scale_data <- model[["parameters"]][["probability.scale"]] %>%
    filter(category == 2) %>%
    select(param, est, LatentClass)

  # Combine the data frames
  combined_data <- bind_rows(means_data, prob_scale_data) %>%
    mutate(Classes = class_number) %>%
    select(Classes, LatentClass, param, est)

  return(combined_data)
}

# Applying the function to each model object in the list
plot_data <- map2(res, 1:5, extract_data) %>%
  bind_rows()

After that, you can use plot_data with ggplot2 to create plots.

cjvanlissa commented 1 month ago

Nicely done!