{"data":{"slug":"github-aipoch-medical-research-skills-awesome-med-research-skills-data-analysis-univariate-multivariable-cox-regression-bd0f1c652f6f","title":"Run Univariate and Multivariable Cox Regression for Survival Analysis","summary":"Perform univariate and multivariable Cox regression on clinical survival data to identify prognostic variables and generate forest plots in PDF format.","url":"https:\/\/skillshunter.net\/skills\/github-aipoch-medical-research-skills-awesome-med-research-skills-data-analysis-univariate-multivariable-cox-regression-bd0f1c652f6f","json_url":"https:\/\/skillshunter.net\/api\/public\/v1\/skills\/github-aipoch-medical-research-skills-awesome-med-research-skills-data-analysis-univariate-multivariable-cox-regression-bd0f1c652f6f","markdown_url":"https:\/\/skillshunter.net\/skills\/github-aipoch-medical-research-skills-awesome-med-research-skills-data-analysis-univariate-multivariable-cox-regression-bd0f1c652f6f.md","download_url":"https:\/\/skillshunter.net\/skills\/github-aipoch-medical-research-skills-awesome-med-research-skills-data-analysis-univariate-multivariable-cox-regression-bd0f1c652f6f\/download","category":{"slug":"data-research","name":"Data & Research"},"editor_note":"The input file must be a CSV with sample IDs in the first column as row names. The `time_col` must contain finite numeric values greater than 0, and the `event_col` must contain only 0 and 1. At least 10 complete samples and 2 events are required after filtering.","instructions":"---\nname: univariate-multivariable-cox-regression\ndescription: \"Use when running prognostic survival analysis on a clinical cohort with time-to-event data to estimate univariate and multivariable Cox proportional hazards models, export result tables, and generate forest plots. NOT for: nomogram construction, calibration curves, time-dependent ROC analysis, or model training\/feature selection beyond the built-in univariate screening rule.\"\n---\n\n# Univariate and Multivariable Cox Regression\n\n## When to Use\n\nUse this skill when you need to:\n- run univariate and multivariable Cox regression on a clinical survival cohort;\n- identify prognostic clinical variables associated with time-to-event outcomes;\n- export hazard-ratio tables and then render forest plots from those results.\n\nTypical user requests:\n- \"Run single-factor and multi-factor Cox regression on this survival dataset.\"\n- \"Find prognostic variables from my clinical cohort and give me forest plots.\"\n- \"Use futime and fustat to do Cox regression for age, stage, and risk.\"\n\n## When Not to Use\n\nDo not use this skill for:\n- nomogram construction or calibration analysis;\n- time-dependent ROC analysis or external prognostic model validation;\n- feature discovery pipelines beyond the built-in univariate screening rule;\n- non-survival outcomes such as binary diagnosis or differential expression.\n\n## When to Read External Files\n\n| Situation | File to Read | Purpose |\n|-----------|--------------|---------|\n| **Need algorithm details** | `references\/algorithm.md` | Statistical workflow, assumptions, and feature-selection rule |\n| **Need to run analysis** | `scripts\/main.R` | Execute `Rscript scripts\/main.R <command> [options]` |\n| **Encounter errors** | `references\/troubleshooting.md` | Common `SKILL_*` errors and fixes |\n| **Need CLI examples** | `references\/cli-guide.md` | Command-specific argument examples |\n| **Need test data** | `tests\/data\/` | Minimal runnable cohort for smoke testing |\n\n---\n\n## Usage\n\n### 1. Run Cox Analysis\n\n```bash\nRscript scripts\/main.R analyze \\\n  --data_file .\/clinical_data.csv \\\n  --features age,gender,stage,risk \\\n  --time_col futime \\\n  --event_col fustat \\\n  --output_dir .\/output\/ \\\n  --seed 42\n```\n\n### 2. Generate Univariate Forest Plot\n\n```bash\nRscript scripts\/main.R forest-plot \\\n  --data_file .\/output\/table\/prognosis_uni_cox_results.xlsx \\\n  --plot_save .\/output\/plot\/uni_forest_plot.pdf\n```\n\n### 3. Generate Multivariable Forest Plot\n\n```bash\nRscript scripts\/main.R multi-forest-plot \\\n  --data_file .\/output\/table\/prognosis_multi_cox_results.xlsx \\\n  --plot_save .\/output\/plot\/multi_forest_plot.pdf\n```\n\n---\n\n## Arguments\n\n### Analyze Command\n\n| Short | Long | Type | Default | Description |\n|-------|------|------|---------|-------------|\n| `-d` | `--data_file` | character | **required** | Clinical CSV file with sample IDs as row names |\n| `-f` | `--features` | character | `age,gender,stage,Tstage,Nstage,Mstage,risk` | Comma-separated features for Cox analysis |\n| `-t` | `--time_col` | character | `futime` | Survival time column |\n| `-e` | `--event_col` | character | `fustat` | Event column encoded as `1=event`, `0=censored` |\n| `-u` | `--skip_univariate` | character | `false` | Skip univariate screening and fit multivariable model on all requested features |\n| `-o` | `--output_dir` | character | `.\/output\/` | Output directory |\n|  | `--overwrite` | flag | `FALSE` | Allow writing into a non-empty output directory |\n| `-s` | `--seed` | integer | `42` | Random seed for reproducibility |\n| `-T` | `--timeout_seconds` | integer | `0` | Elapsed time limit in seconds; `0` disables timeout |\n\n### Forest Plot Commands\n\nThese arguments apply to both `forest-plot` and `multi-forest-plot`.\n\n| Short | Long | Type | Default | Description |\n|-------|------|------|---------|-------------|\n| `-d` | `--data_file` | character | **required** | Cox result table in `.xlsx`, `.xls`, or `.csv` format |\n| `-p` | `--plot_save` | character | **required** | Output PDF path |\n| `-w` | `--width` | double | `8` | Plot width in inches |\n| `-H` | `--height` | double | `6` | Plot height in inches |\n| `-F` | `--font_size` | double | `11` | Font size for forest-plot labels |\n| `-s` | `--seed` | integer | `42` | Random seed for reproducibility |\n| `-T` | `--timeout_seconds` | integer | `0` | Elapsed time limit in seconds; `0` disables timeout |\n\n---\n\n## Input Format\n\n### Clinical Data (`--data_file` for `analyze`)\n\nCSV file with sample IDs as row names and one column per feature\/end-point variable.\n\n```csv\n\",age,gender,stage,futime,fustat,risk\nSAMPLE_001,65,Male,StageIII,365,1,high\nSAMPLE_002,52,Female,StageII,730,0,low\nSAMPLE_003,78,Male,StageIV,180,1,high\n```\n\n**Requirements**\n- The file must be CSV.\n- Sample IDs must be stored in the first column as row names.\n- `time_col` must contain finite numeric values greater than `0`.\n- `event_col` must contain only `0` and `1`.\n- All requested `features`, `time_col`, and `event_col` must exist.\n- At least 10 complete samples and at least 2 events are required after filtering incomplete rows.\n\n### Cox Result Table (`--data_file` for plot commands)\n\nThe plotting commands read the output table created by `analyze`.\n\nRequired columns:\n- `Characteristics`\n- `Total(N)`\n- `HR (95% CI)`\n- `P value`\n\n---\n\n## Output Files\n\n### Analyze Command\n\n| File | Description |\n|------|-------------|\n| `table\/prognosis_uni_cox_results.xlsx` | Univariate Cox result table. Present unless `--skip_univariate true` |\n| `table\/prognosis_multi_cox_results.xlsx` | Multivariable Cox result table |\n| `data\/analysis_data.rds` | Serialized complete-case dataset used for Cox fitting |\n| `session_info.txt` | Session info and recorded run parameters |\n\n### Plot Commands\n\n| File | Description |\n|------|-------------|\n| `plot\/uni_forest_plot.pdf` | PDF forest plot generated by `forest-plot` |\n| `plot\/multi_forest_plot.pdf` | PDF forest plot generated by `multi-forest-plot` |\n| `plot\/session_info.txt` | Session info and plotting parameters written beside the PDF |\n\n### Result Table Columns\n\n| Column | Description |\n|--------|-------------|\n| `Characteristics` | Variable name for continuous terms, or level label for categorical terms |\n| `Total(N)` | Number of complete-case samples used for modeling |\n| `HR (95% CI)` | Hazard ratio with 95% confidence interval |\n| `P value` | Wald-test p-value formatted to three decimals or `<0.001` |\n| `feature` | Source feature corresponding to each row |\n\n---\n\n## Workflow\n\n### Step 1: Validate and Prepare Data\n- Read the clinical CSV.\n- Check required columns and data types.\n- Convert character predictors to factors.\n- Remove rows with missing values across requested model variables.\n\n### Step 2: Run Univariate Cox Models\n- Fit one Cox model per feature when `--skip_univariate false`.\n- Export hazard ratios, confidence intervals, and p-values.\n\n### Step 3: Run Multivariable Cox Model\n- Use all significant univariate features with `p < 0.05`.\n- If fewer than 3 significant features are found, fall back to all requested features.\n- Export adjusted hazard ratios, confidence intervals, and p-values.\n\n### Step 4: Generate Forest Plots\n- Read the result table.\n- Parse `HR (95% CI)` values.\n- Render a one-page PDF forest plot.\n\n---\n\n## Examples\n\n### Basic Analysis\n\n```bash\nRscript scripts\/main.R analyze \\\n  -d tests\/data\/sample_clinical_survival_data.csv \\\n  -o tests\/expected_output\/ \\\n  --overwrite\n```\n\n### Analysis With Selected Features and Overwrite\n\n```bash\nRscript scripts\/main.R analyze \\\n  -d clinical_data.csv \\\n  -f age,gender,stage,risk \\\n  -o .\/results\/ \\\n  --overwrite \\\n  -T 600\n```\n\n### Direct Multivariable Fit Without Univariate Screening\n\n```bash\nRscript scripts\/main.R analyze \\\n  -d clinical_data.csv \\\n  -f age,stage,risk \\\n  -u true \\\n  -o .\/results\/\n```\n\n### Plot Generation\n\n```bash\nRscript scripts\/main.R forest-plot \\\n  -d .\/results\/table\/prognosis_uni_cox_results.xlsx \\\n  -p .\/results\/plot\/uni_forest_plot.pdf \\\n  -w 10 -H 7 -F 12\n\nRscript scripts\/main.R multi-forest-plot \\\n  -d .\/results\/table\/prognosis_multi_cox_results.xlsx \\\n  -p .\/results\/plot\/multi_forest_plot.pdf \\\n  -w 10 -H 7 -F 12\n```\n\n---\n\n## Error Handling\n\n| Error | Cause | Solution |\n|-------|-------|----------|\n| `SKILL_INVALID_PARAMETER` | Missing required CLI value, invalid extension, unknown command, unreadable CSV input, invalid clinical values, too few complete samples\/events, or Cox model fitting failure caused by unsupported input data | Check argument names, file types, clinical value constraints, and model input suitability |\n| `SKILL_FILE_NOT_FOUND` | Input file path does not exist | Verify the input path |\n| `SKILL_MISSING_COLUMNS` | Required columns are absent from the clinical file or plot table | Check column names and spelling |\n| `SKILL_EMPTY_DATA` | Input file or plot table contains no usable rows | Verify file content and export process |\n| `SKILL_PACKAGE_NOT_FOUND` | Required R package is missing | Install the listed CRAN package(s) |\n\n**IF error persists**, READ: `references\/troubleshooting.md`\n\n---\n\n## Testing\n\n### Smoke Test With Included Data\n\n```bash\nRscript scripts\/main.R --help\n\nRscript scripts\/main.R analyze \\\n  -d tests\/data\/sample_clinical_survival_data.csv \\\n  -o tests\/expected_output\/ \\\n  --overwrite\n\nRscript scripts\/main.R forest-plot \\\n  -d tests\/expected_output\/table\/prognosis_uni_cox_results.xlsx \\\n  -p tests\/expected_output\/plot\/uni_forest_plot.pdf\n\nRscript scripts\/main.R multi-forest-plot \\\n  -d tests\/expected_output\/table\/prognosis_multi_cox_results.xlsx \\\n  -p tests\/expected_output\/plot\/multi_forest_plot.pdf\n```\n\n### Automated Smoke Test Script\n\n```bash\nRscript tests\/run_smoke_test.R\n```\n\nOptional shell wrapper:\n\n```bash\nbash tests\/run_smoke_test.sh\n```\n\n### Expected Outputs\n\n```text\ntests\/expected_output\/\n|-- data\/analysis_data.rds\n|-- plot\/multi_forest_plot.pdf\n|-- plot\/session_info.txt\n|-- plot\/uni_forest_plot.pdf\n|-- session_info.txt\n|-- table\/prognosis_multi_cox_results.xlsx\n`-- table\/prognosis_uni_cox_results.xlsx\n```\n\n---\n\n## References\n\n1. Cox DR (1972). Regression Models and Life-Tables. *Journal of the Royal Statistical Society: Series B*.\n2. Therneau TM, Grambsch PM (2000). *Modeling Survival Data: Extending the Cox Model*.\n3. Harrell FE (2015). *Regression Modeling Strategies*.\n\n**For detailed algorithm**, READ: `references\/algorithm.md`\n\n---\n\n## Implementation Checklist\n\n- [x] CLI parsing with `optparse`\n- [x] `set.seed()` for reproducibility\n- [x] `requireNamespace()` dependency checks\n- [x] Session info recording\n- [x] Timeout parameter exposed as CLI option\n- [x] File reading instructions in `SKILL.md`\n- [x] Modular script structure in `scripts\/`\n- [x] Test data provided in `tests\/data\/`\n- [x] Error handling with `SKILL_*` codes\n- [x] References documented in `references\/`\n\n---\n\n*Last updated: 2026-04-16 | Version: 1.1.0*\n","use_cases":["Run single-factor and multi-factor Cox regression on a survival dataset.","Identify prognostic variables from a clinical cohort and generate forest plots.","Use futime and fustat to perform Cox regression for age, stage, and risk factors."],"limitations":[],"compatibility":[],"setup_tip":null,"sample_input":null,"sample_output":null,"source_url":"https:\/\/github.com\/aipoch\/medical-research-skills\/blob\/686e09dfdb45d9fa1a08664f06bb93769b3c9d91\/awesome-med-research-skills\/Data%20Analysis\/univariate-multivariable-cox-regression\/SKILL.md","source_commit":"686e09dfdb45d9fa1a08664f06bb93769b3c9d91","license":null,"version":null,"listing_approved_at":"2026-11-24","created_at":"2026-11-23T07:39:25.000000Z","updated_at":"2026-11-24T07:13:22.000000Z","seo_title":"Run Cox Regression for Survival Analysis","seo_description":"Perform univariate and multivariable Cox regression on clinical survival data to identify prognostic variables and generate forest plots.","seo_keywords":["Cox regression survival analysis","Prognostic variable identification","Forest plot generation","Clinical survival data analysis","Univariate and multivariable Cox models"]}}