LPA Latent Class Means Extractor
Extract per-class variable Means and Class Counts from Mplus LPA output files and generate a multi-sheet Excel workbook (one sheet per model).
When to Use
- User has multiple Mplus
.outfiles from LPA models (e.g., 2-class through 6-class) - User wants a table showing, for each latent class, the Means of each analysis variable
- User also wants Class Counts (integer counts) in the same table
- Output format: one Excel sheet per model, with Class1/Class2/... as columns and variables as rows
Workflow
Step 1: Identify inputs
Ask the user or infer from context:
- Which directory contains the
.outfiles? - What is the file naming pattern? (e.g.,
T3 LPA*.out) - Which variables to extract as row labels? (e.g.,
T3PF_hos,T3PF_con,T3PF_dis,T3PM_hos,T3PM_con,T3PM_dis,T3CF_m) - What prefix to strip from filenames for sheet labels? (e.g.,
T3to getLPA2,LPA3) - Output Excel filename?
Step 2: Run the extraction script
python scripts/extract_lpa_means.py <directory> \
--pattern "GLOB_PATTERN" \
--output OUTPUT.xlsx \
--variables "VAR1,VAR2,VAR3,..." \
[--sheet-prefix LPA] \
[--row-prefix "PREFIX_TO_STRIP"]
Arguments:
directory(required): Path to the directory containing.outfiles--pattern(default*.out): Glob pattern to match files--output(defaultLPA_Class_Means.xlsx): Output Excel filename--variables(required): Comma-separated variable names to extract as row labels--sheet-prefix(defaultLPA): Prefix for sheet names (e.g.,LPA->LPA2,LPA3)--row-prefix(default empty): Prefix to strip from filenames for sheet labels. E.g., if files areT3 LPA2.outand you want sheet labelLPA2, use--row-prefix "T3 "--row-suffix(default.out): Suffix to strip from filenames for sheet labels
Step 3: Present the result
After the script completes, call present_files with the output Excel file path.
Also summarize the extracted values in a markdown table in the reply.
Extracted Values
Each sheet in the Excel workbook has the following structure:
| Row | Content | Source in .out file |
|-----|---------|-------------------|
| Header | "", Class1, Class2, ... | Generated |
| Class Counts | Integer count per class | Class Counts and Proportions section (under "BASED ON THEIR MOST LIKELY LATENT CLASS MEMBERSHIP") -> 2nd column |
| Variable rows | Mean estimate per class | MODEL RESULTS -> Latent Class N -> Means sub-section -> Estimate column (1st numeric column) |
Extraction details
- Class Counts: Extracted from the "Class Counts and Proportions" section that appears under "BASED ON THEIR MOST LIKELY LATENT CLASS MEMBERSHIP". The 2nd column (integer counts) is used.
- Variable Means: Extracted from the
MODEL RESULTSsection. For eachLatent Class Nblock, theMeanssub-section is parsed. Each variable line has the formatVAR_NAME estimate SE est/SE p-value; the estimate (1st numeric column) is extracted. - Variable matching: Variable names are matched case-insensitively (uppercased). If a variable in the user's list is not found in the .out file (e.g., not in USEVARIABLES), its cell is left empty.
- Number of classes: Inferred from the number of Class Counts entries, or from the highest
Latent Class Nin MODEL RESULTS, or from the numeric suffix in the filename.
Notes
- The script requires
openpyxl. If not installed, install it first with pip. - Mean values are formatted with
0.000(3 decimal places) in Excel. - Class Counts are displayed as integers.
- The Excel output uses Times New Roman font, centered alignment, thin borders, and light blue header fill.
- Files with spaces in names (e.g.,
T3 LPA2.out) are supported. - If a variable is not in the model (not in USEVARIABLES), its row will be empty -- this is expected behavior, not an error.
- The
MODEL RESULTSsection is isolated by findingMODEL RESULTSat the start andFINAL CLASS COUNTS/QUALITY OF CLASSIFICATION/TECHNICALat the end to avoid cross-section contamination. - Within each
Latent Class Nblock, only theMeanssub-section (beforeVariances) is parsed.
Example
python scripts/extract_lpa_means.py /path/to/T3 \
--pattern "T3 LPA*.out" \
--output T3_LPA_Class_Means.xlsx \
--variables "T3PF_hos,T3PF_con,T3PF_dis,T3PM_hos,T3PM_con,T3PM_dis,T3CF_m" \
--sheet-prefix LPA \
--row-prefix "T3 "
This produces a 5-sheet Excel file:
- Sheet
LPA2: 2 class columns (Class1, Class2) - Sheet
LPA3: 3 class columns (Class1, Class2, Class3) - Sheet
LPA4: 4 class columns - Sheet
LPA5: 5 class columns - Sheet
LPA6: 6 class columns
Each sheet has rows: Class Counts, then one row per variable.
微信扫一扫