$npx -y skills add Aperivue/medsci-skills --skill batch-cohortGenerate N analysis scripts from a single methodology template × multiple exposure/outcome combinations. The "80-person team" pattern — same validated method, swap variables only. Produces batch R/Python code + summary matrix.
| 1 | # Batch Cohort Analysis Skill |
| 2 | |
| 3 | You are assisting a medical researcher in generating multiple analysis scripts from a single |
| 4 | validated methodology template, each differing only in the exposure/outcome variable combination. |
| 5 | This replicates the "80-person research team" pattern: one PI designs the methodology, and |
| 6 | many researchers execute the same approach with different variable swaps. |
| 7 | |
| 8 | ## When to Use |
| 9 | |
| 10 | - Researcher has a **validated analysis template** (e.g., from /replicate-study or /cross-national) |
| 11 | - Wants to explore **multiple exposure → outcome combinations** on the same database |
| 12 | - Goal: systematic variable-swap code generation + batch execution + result matrix |
| 13 | |
| 14 | ## Inputs |
| 15 | |
| 16 | 1. **Database path(s)**: CSV/SAS data files (KNHANES, NHANES, NHIS, or any cleaned cohort) |
| 17 | 2. **Methodology template**: One of: |
| 18 | - Path to a validated R/Python analysis script (from /replicate-study or /cross-national) |
| 19 | - A paper type template name: `nhis_cohort`, `cross_national`, `survey_weighted` |
| 20 | - A source paper to extract methodology from (falls back to /replicate-study Phase 1) |
| 21 | 3. **Combination spec**: A list of exposure/outcome pairs, provided as: |
| 22 | - Inline list: `exposures: [depression, obesity, smoking]; outcomes: [diabetes, hypertension, CVD]` |
| 23 | - CSV file with columns: `exposure`, `outcome`, (optional) `subgroup_vars` |
| 24 | - `"all"` keyword: generates all pairwise combinations from the lists |
| 25 | |
| 26 | ### Optional Inputs |
| 27 | |
| 28 | - **Covariate set**: Fixed covariate list for all analyses (default: use template's set) |
| 29 | - **Subgroup variables**: Variables to stratify by (default: sex, age group) |
| 30 | - **Output format**: `code_only` (just scripts) | `execute` (run + collect results) | `full` (code + results + summary) |
| 31 | - **Cross-national mode**: If TRUE, generates paired scripts for both countries per combination |
| 32 | |
| 33 | ## Workflow |
| 34 | |
| 35 | ### Phase 1: Template Validation |
| 36 | |
| 37 | 1. Read the methodology template (R script or paper type reference). |
| 38 | 2. Identify the **slot variables** — parts that change per combination: |
| 39 | - `EXPOSURE_VAR`: raw variable name in the database |
| 40 | - `EXPOSURE_LABEL`: human-readable label for tables/figures |
| 41 | - `EXPOSURE_CODING`: how to derive binary/categorical exposure |
| 42 | - `OUTCOME_VAR`: raw variable name |
| 43 | - `OUTCOME_LABEL`: human-readable label |
| 44 | - `OUTCOME_CODING`: how to derive binary outcome |
| 45 | 3. Verify the template runs successfully on at least one combination before batch generation. |
| 46 | 4. Output: template summary with identified slots → user approval. |
| 47 | |
| 48 | ### Phase 2: Variable Specification |
| 49 | |
| 50 | For each exposure and outcome in the combination spec: |
| 51 | |
| 52 | 1. **Look up** the variable in the database: |
| 53 | - KNHANES: check variable name exists in the CSV header |
| 54 | - NHANES: check which table contains the variable (use codebook.csv if available) |
| 55 | - NHIS: check claims code or variable name |
| 56 | 2. **Define coding**: |
| 57 | - Binary: threshold or category mapping (e.g., `HE_glu >= 126 → diabetes = 1`) |
| 58 | - Categorical: level definitions (e.g., `smoking: current/former/never`) |
| 59 | 3. **Check covariate overlap**: If the exposure IS one of the standard covariates, remove it from the adjustment set for that analysis (no self-adjustment). |
| 60 | 4. Output: **combination matrix** with all variable specifications. |
| 61 | |
| 62 | ``` |
| 63 | | # | Exposure | Exposure Coding | Outcome | Outcome Coding | Covariates (adjusted) | Notes | |
| 64 | |---|----------|-----------------|---------|----------------|----------------------|-------| |
| 65 | | 1 | Depression (PHQ≥10) | BP_PHQ sum ≥10 | Diabetes | HE_glu≥126|HbA1c≥6.5|DE1_dg=1 | age,sex,edu,income,smoking,alcohol,obesity,CVD | — | |
| 66 | | 2 | Obesity (BMI≥25) | HE_obe ≥4 | Diabetes | same | age,sex,edu,income,smoking,alcohol,depression,CVD | obesity removed from covariates | |
| 67 | | ... | | | | | | | |
| 68 | ``` |
| 69 | |
| 70 | ### Phase 3: Batch Code Generation |
| 71 | |
| 72 | For each combination in the matrix: |
| 73 | |
| 74 | 1. **Clone** the template script. |
| 75 | 2. **Replace** slot variables with the combination-specific values. |
| 76 | 3. **Adjust covariates**: Remove exposure variable from covariate list if present. |
| 77 | 4. **Set output paths**: Each combination gets its own results subdirectory. |
| 78 | 5. **Generate a master runner script** (`run_all.R` or `run_all.sh`) that: |
| 79 | - Executes all N scripts sequentially (or in parallel via `future`/`parallel`) |
| 80 | - Captures errors per script without stopping the batch |
| 81 | - Logs execution time per analysis |
| 82 | |
| 83 | ### Phase 4: Batch Execution (if `execute` or `full` mode) |
| 84 | |
| 85 | 1. Run the master script. |
| 86 | 2. Collect results from each combination's output directory. |
| 87 | 3. Handle failures gracefully: |
| 88 | - Log which combinations failed |