{"data":{"slug":"github-aipoch-medical-research-skills-awesome-med-research-skills-data-analysis-model-calibration-curve-f285c5def4f4","title":"Generate Survival Model Calibration Curves from Clinical CSV Files","summary":"Validate survival models by generating bootstrap calibration curves from clinical CSV files. The process includes fitting a Cox model and exporting calibration statistics and a PDF visualization at specified prediction horizons.","url":"https:\/\/skillshunter.net\/skills\/github-aipoch-medical-research-skills-awesome-med-research-skills-data-analysis-model-calibration-curve-f285c5def4f4","json_url":"https:\/\/skillshunter.net\/api\/public\/v1\/skills\/github-aipoch-medical-research-skills-awesome-med-research-skills-data-analysis-model-calibration-curve-f285c5def4f4","markdown_url":"https:\/\/skillshunter.net\/skills\/github-aipoch-medical-research-skills-awesome-med-research-skills-data-analysis-model-calibration-curve-f285c5def4f4.md","download_url":"https:\/\/skillshunter.net\/skills\/github-aipoch-medical-research-skills-awesome-med-research-skills-data-analysis-model-calibration-curve-f285c5def4f4\/download","category":{"slug":"data-research","name":"Data & Research"},"editor_note":"Install required R packages (`rms`, `qs`, `openxlsx`, `optparse`) before running the script. Ensure the clinical CSV file has unique row names, valid survival time values, and event columns encoded as 0\/1. The `--help` option requires `optparse` to be loaded first.","instructions":"---\nname: model-calibration-curve\ndescription: \"Use when assessing how well a survival model's predicted probabilities agree with observed outcomes by fitting a Cox model and generating bootstrap calibration curves at one or more prediction horizons from a clinical CSV file. NOT for: nomogram construction, univariate Cox screening, ROC analysis, or decision-curve analysis.\"\nlicense: MIT\nskill-author: AIPOCH\n---\n\n# Model Calibration Curve\n\n## When to Use\n\nUse this skill when you need to:\n- validate a survival model with bootstrap calibration curves;\n- compare predicted and observed survival probabilities at multiple horizons;\n- export calibration statistics together with a PDF visualization.\n\nTypical user requests:\n- \"Generate 1-, 2-, and 3-year calibration curves for this prognosis model.\"\n- \"Check whether the Cox model built from age, gender, and risk is well calibrated.\"\n- \"Export calibration statistics and a calibration PDF from this clinical cohort.\"\n\n## When Not to Use\n\nDo not use this skill for:\n- nomogram construction;\n- univariate or multivariable Cox feature screening;\n- ROC, calibration-free discrimination, or decision-curve analysis;\n- non-survival endpoints or multiclass classification tasks.\n\n## When to Read External Files\n\n| Situation | File to Read | Purpose |\n|-----------|--------------|---------|\n| Need algorithm details | `references\/algorithm.md` | Statistical method and formulas |\n| Need to run analysis | `scripts\/main.R` | Get the complete command |\n| Encounter errors | `references\/troubleshooting.md` | Find solutions |\n| Need CLI examples | `references\/cli-guide.md` | Parameter usage examples |\n\n## Input Validation\n\nThis skill accepts:\n\n- A clinical CSV file with sample IDs as row names, survival time, event indicator, and pre-selected prognostic features\n- Requests to assess calibration of a survival (Cox) model via bootstrap resampling at one or more prediction horizons\n\nIf the user's request does not involve survival model calibration from a clinical CSV file \u2014 for example, asking to construct a nomogram, screen Cox features, generate an ROC curve, analyze a decision curve, or work with non-survival outcomes \u2014 do not proceed with this workflow. Instead respond:\n\n> \"model-calibration-curve is designed to validate survival model calibration by generating bootstrap calibration curves from a clinical CSV file. Your request appears to be outside this scope. Please use a nomogram-construction skill for nomogram building, a roc-diagnostic-performance skill for ROC analysis, or a decision-curve-analysis skill for DCA.\"\n\n---\n\n## Prerequisites\n\nR packages required: `rms`, `qs`, `openxlsx`, `optparse`.\n\nInstall with:\n```r\ninstall.packages(c(\"rms\", \"qs\", \"openxlsx\", \"optparse\"), repos = \"https:\/\/cloud.r-project.org\")\n```\n\nOr run the bootstrap installer:\n```bash\nRscript scripts\/install_dependencies.R\n```\n\n> Note: `--help` requires `optparse` to be loaded. If the package check fires before option parsing, install `optparse` first, then run `--help`. The root fix (deferring heavy package checks until after argument parsing) must be applied in `scripts\/main.R`.\n\n---\n\n## Usage\n\n```bash\nRscript scripts\/main.R \\\n  --data_file .\/clinical_data.csv \\\n  --features age,stage,risk \\\n  --years 1,2,3 \\\n  --output_dir .\/output\/\n```\n\n---\n\n## Arguments\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 | **required** | Comma-separated model features used in the Cox model |\n| `-t` | `--time_col` | character | `futime` | Survival time column |\n| `-e` | `--event_col` | character | `fustat` | Event indicator column using 0\/1 encoding |\n| `-y` | `--years` | character | `1,2,3` | Prediction horizons in the same units as `time_col` |\n| `-b` | `--bootstrap_reps` | integer | `1000` | Bootstrap replications for `rms::calibrate()` |\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|  | `--plot_width` | double | `6` | PDF width in inches |\n|  | `--plot_height` | double | `6` | PDF height in inches |\n|  | `--font_family` | character | `sans` | PDF font family |\n|  | `--line_width` | double | `1.5` | Calibration curve line width |\n|  | `--colors` | character | `#0073C2,#EFC000,#868686,#CD534C,#7AA6DD` | Comma-separated colors for time-point curves |\n|  | `--plot_title` | character | `Calibration Curve` | Plot title |\n|  | `--base_cex` | double | `0.9` | Base text-size multiplier |\n\n---\n\n## Input Format\n\n### Clinical Data (`--data_file`)\n\nCSV file with row names as sample IDs and columns for model features, survival time, and event indicator.\n\n```csv\n\"\",age,gender,stage,futime,fustat,risk\n\"SAMPLE_001\",\">65\",\"Female\",\"StageI&II\",1.12,0,\"high\"\n\"SAMPLE_002\",\"<=65\",\"Male\",\"StageIII&IV\",1.92,1,\"high\"\n\"SAMPLE_003\",\">65\",\"Male\",\"StageI&II\",4.47,1,\"low\"\n```\n\n**Requirements**\n- File extension must be `.csv`.\n- Row names must be unique sample IDs.\n- All requested features plus `time_col` and `event_col` must exist.\n- Survival time values must be finite numbers greater than `0`.\n- Event values must use `0\/1` encoding.\n- Complete-case filtering must leave at least 30 samples and at least 10 events.\n\n### Feature Selection (`--features`)\n\n- Comma-separated without line breaks: `age,gender,risk`\n- Character predictors are converted to factors before Cox fitting.\n- If every requested feature is absent after validation, the run stops.\n\n---\n\n## Output Files\n\n| File | Format | Description |\n|------|--------|-------------|\n| `data\/calibration_data.qs` | QS serialized object | Serialized calibration result bundle, including calibration objects and summary metadata |\n| `table\/calibration_statistics.xlsx` | Excel workbook (`.xlsx`) | Per-time-point means and overall model summary |\n| `plot\/calibration_curve.pdf` | PDF (`.pdf`) | Combined calibration curve visualization |\n| `session_info.txt` | Plain text (`.txt`) | Session information and run parameters |\n\n### `calibration_statistics.xlsx`\n\nWorkbook sheets:\n- `Time_Point_Stats`: predicted mean, observed mean, and bias-corrected mean for each calibration horizon.\n- `Model_Summary`: overall C-index, sample count, event count, selected features, and fitted formula.\n\n---\n\n## Workflow\n\n### Step 1: Validate Input\n- Confirm the clinical CSV exists and is readable.\n- Check that requested features, survival time, and event columns are present.\n- Remove incomplete rows across all required columns.\n\n### Step 2: Prepare Survival Modeling Data\n- Convert survival time and event columns to numeric.\n- Convert character predictors to factors.\n- Reject data with non-positive follow-up times, invalid event coding, too few samples, or too few events.\n\n### Step 3: Build Calibration Models\n- Fit a Cox proportional hazards model with the requested features.\n- Run `rms::calibrate()` for each prediction horizon using bootstrap resampling.\n- Compute the concordance index for the fitted model.\n\n### Step 4: Save Outputs\n- Serialize the calibration result bundle as `.qs`.\n- Export statistics to Excel.\n- Render a combined calibration PDF.\n- Record session metadata for reproducibility.\n\n---\n\n## Examples\n\n### Basic Calibration Analysis\n\n```bash\nRscript scripts\/main.R \\\n  --data_file clinical_data.csv \\\n  --features age,stage,risk \\\n  --output_dir .\/output\/\n```\n\n### Custom Horizons And Bootstrap Count\n\n```bash\nRscript scripts\/main.R \\\n  --data_file clinical_data.csv \\\n  --features age,gender,risk \\\n  --years 1,3,5 \\\n  --bootstrap_reps 1500 \\\n  --output_dir .\/custom_output\/\n```\n\n### Custom Plot Styling\n\n```bash\nRscript scripts\/main.R \\\n  --data_file clinical_data.csv \\\n  --features age,stage,risk \\\n  --plot_width 7 \\\n  --plot_height 6 \\\n  --line_width 2 \\\n  --colors \"#1B9E77,#D95F02,#7570B3\" \\\n  --plot_title \"Three-Horizon Calibration\" \\\n  --output_dir .\/styled_output\/\n```\n\n### With Bundled Test Data\n\n```bash\nRscript scripts\/main.R \\\n  --data_file tests\/data\/sample_clinical_survival_data.csv \\\n  --features age,gender,risk \\\n  --bootstrap_reps 20 \\\n  --output_dir tests\/output\/ \\\n  --overwrite\n```\n\n---\n\n## Error Handling\n\n| Error | Cause | Solution |\n|-------|-------|----------|\n| `SKILL_INVALID_PARAMETER` | Missing required argument, invalid numeric values, invalid event coding, insufficient complete cases, insufficient events, or failed model fitting | Check argument values, data validity, and event\/sample counts |\n| `SKILL_FILE_NOT_FOUND` | Input CSV does not exist | Verify the path |\n| `SKILL_MISSING_COLUMNS` | Required feature\/time\/event columns are absent | Check column names and spelling |\n| `SKILL_EMPTY_DATA` | Input file is empty, complete-case filtering removed all rows, or no requested features remained | Check file content and requested feature names |\n| `SKILL_SAMPLE_MISMATCH` | Reserved for cross-file sample mismatch scenarios | Not expected for this single-file workflow |\n| `SKILL_PACKAGE_NOT_FOUND` | Required R package is missing | Install with: `Rscript -e \"install.packages(c('rms', 'qs', 'openxlsx'), repos='https:\/\/cloud.r-project.org')\"` |\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 \\\n  --data_file tests\/data\/sample_clinical_survival_data.csv \\\n  --features age,gender,risk \\\n  --bootstrap_reps 20 \\\n  --output_dir tests\/output\/ \\\n  --overwrite\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 Output\n\n```text\ntests\/output\/\n|-- data\/calibration_data.qs\n|-- plot\/calibration_curve.pdf\n|-- session_info.txt\n`-- table\/calibration_statistics.xlsx\n```\n\n---\n\n## References\n\n1. Harrell FE. *Regression Modeling Strategies*.\n2. Steyerberg EW et al. Assessing the performance of prediction models.\n3. Austin PC, Steyerberg EW. Graphical assessment of calibration for survival models.\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] Top-level CRAN dependency checks\n- [x] Session info recording\n- [x] Timeout parameter exposed as CLI option\n- [x] Relative-path `source()` usage via `get_script_dir()`\n- [x] Modular script structure in `scripts\/`\n- [x] Test data provided in `tests\/data\/`\n- [x] Error handling with `SKILL_*` codes\n- [x] Reference docs provided in `references\/`\n\n---\n\n*Last updated: 2026-04-27 | Version: 2.1.0*\n","use_cases":["Validate a survival model with bootstrap calibration curves","Compare predicted and observed survival probabilities at multiple horizons","Export calibration statistics and a calibration PDF from a clinical cohort"],"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\/model-calibration-curve\/SKILL.md","source_commit":"686e09dfdb45d9fa1a08664f06bb93769b3c9d91","license":"MIT","version":null,"listing_approved_at":"2026-11-24","created_at":"2026-11-23T07:22:03.000000Z","updated_at":"2026-11-24T07:13:04.000000Z","seo_title":"Generate Survival Model Calibration Curves from Clinical CSV Files","seo_description":"Validate survival models with bootstrap calibration curves from clinical CSV files. Assess model calibration at multiple prediction horizons and export statistics and a P","seo_keywords":["survival model calibration","bootstrap calibration curves","clinical data analysis","model validation","calibration curve generation"]}}