返回 Skill 列表
extension
分类: 数据与分析无需 API Key

jjc_lpa_means

Extract Latent Class Means and Class Counts from Mplus LPA output files and generate a multi-sheet Excel workbook. Each sheet (LPA2~LPA6) contains Class Counts and variable Means for each Latent Class. Trigger phrases: LPA均值提取, 潜剖面均值表, Latent Class Means提取, Mplus MODEL RESULTS转Excel, LPA各类别变量均值表格, or any request to extract per-class variable means from multiple LPA out files.

person作者: user_35e28d05hubcommunity

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 .out files 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 .out files?
  • 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., T3 to get LPA2, 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 .out files
  • --pattern (default *.out): Glob pattern to match files
  • --output (default LPA_Class_Means.xlsx): Output Excel filename
  • --variables (required): Comma-separated variable names to extract as row labels
  • --sheet-prefix (default LPA): 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 are T3 LPA2.out and you want sheet label LPA2, 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 RESULTS section. For each Latent Class N block, the Means sub-section is parsed. Each variable line has the format VAR_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 N in 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 RESULTS section is isolated by finding MODEL RESULTS at the start and FINAL CLASS COUNTS / QUALITY OF CLASSIFICATION / TECHNICAL at the end to avoid cross-section contamination.
  • Within each Latent Class N block, only the Means sub-section (before Variances) 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.