{"data":{"slug":"github-aipoch-medical-research-skills-awesome-med-research-skills-data-analysis-km-survival-curve-7118e53bc9ef","title":"Kaplan-Meier Survival Curve Analysis","summary":"Generate Kaplan-Meier survival curves from tabular survival data containing time, event status, and a precomputed risk group. Export a single PDF figure.","url":"https:\/\/skillshunter.net\/skills\/github-aipoch-medical-research-skills-awesome-med-research-skills-data-analysis-km-survival-curve-7118e53bc9ef","json_url":"https:\/\/skillshunter.net\/api\/public\/v1\/skills\/github-aipoch-medical-research-skills-awesome-med-research-skills-data-analysis-km-survival-curve-7118e53bc9ef","markdown_url":"https:\/\/skillshunter.net\/skills\/github-aipoch-medical-research-skills-awesome-med-research-skills-data-analysis-km-survival-curve-7118e53bc9ef.md","download_url":"https:\/\/skillshunter.net\/skills\/github-aipoch-medical-research-skills-awesome-med-research-skills-data-analysis-km-survival-curve-7118e53bc9ef\/download","category":{"slug":"data-research","name":"Data & Research"},"editor_note":"Ensure `Rscript` is available in the shell and install required R packages (`optparse`, `data.table`, `survival`, `survminer`, `ggplot2`) if missing. The input file must contain time, status, and group columns with specific formatting requirements.","instructions":"---\nname: km-survival-curve\ndescription: Use when generating Kaplan-Meier survival curves from tabular survival data containing time, event status, and a precomputed risk group. Supports command-line parameter input, parameter validation, automatic time-unit handling, single-file PDF figure export, and session metadata capture.\nlicense: MIT\nauthor: AIPOCH\n---\n> **Source**: [https:\/\/github.com\/aipoch\/medical-research-skills](https:\/\/github.com\/aipoch\/medical-research-skills)\n\n# Kaplan-Meier Survival Curve Analysis\n\nUse this skill to run Kaplan-Meier survival analysis on a tabular dataset and export a single PDF survival figure.\n\n## Use This Skill When\n\n- You need a Kaplan-Meier survival curve from a table containing time, status, and group columns.\n- You need a command-line survival workflow with parameter validation.\n- You need a single Kaplan-Meier plot as the final analysis result.\n\n## Primary Command\n\n```bash\nRscript scripts\/main.R \\\n  --input_file <input_file> \\\n  --output_dir <output_dir> \\\n  --time_col <time_column> \\\n  --status_col <status_column> \\\n  --risk_col <group_column>\n```\n\n## Prerequisites\n\n- `Rscript` is available in the shell.\n- Required R packages: `optparse`, `data.table`, `survival`, `survminer`, `ggplot2`.\n- Install missing packages with `Rscript -e 'install.packages(c(\"optparse\", \"data.table\", \"survival\", \"survminer\", \"ggplot2\"), repos=\"https:\/\/cloud.r-project.org\")'`.\n\n## Core Arguments\n\n| Argument | Required | Description |\n|----------|----------|-------------|\n| `--input_file` | Yes | Input data file in CSV or tab-delimited TXT\/TSV format |\n| `--output_dir` | No | Output directory, default `.\/KM_Results` |\n| `--time_col` | No | Survival time column, default `futime` |\n| `--status_col` | No | Event status column, default `fustat` |\n| `--risk_col` | No | Risk group column, default `risk_group` |\n| `--time_unit` | No | Time unit label: `year`, `month`, or `day`, default `year` |\n| `--auto_convert_days` | No | Heuristically convert large time values from days when `time_unit` is `year` or `month`, default `true` |\n| `--statistics_method` | No | `logrank` or `wald`, default `logrank` |\n\n## Plot Customization Arguments\n\n| Argument | Default | Description |\n|----------|---------|-------------|\n| `--figure_width` | `10` | Figure width in inches |\n| `--figure_height` | `7` | Figure height in inches |\n| `--figure_family` | `sans` | Font family |\n| `--title_x` | `Time` | X-axis title. If left as default, the script renders `Time (<time_unit>)` |\n| `--title_y` | `Survival probability` | Y-axis title |\n| `--title_main` | empty | Plot title |\n| `--legend_position` | `top` | `top`, `bottom`, `left`, `right`, `none` |\n| `--legend_show` | `true` | Whether to show legend |\n| `--legend_title` | empty | Legend title |\n| `--line_type` | `solid` | Survival line type: `solid`, `dashed`, `dotted`, `dotdash`, `longdash`, `twodash` |\n| `--line_size` | `1` | Survival line width |\n| `--line_colors` | `#4DBBD5,#E64B35,#00A087,#3C5488,#F39B7F,#8491B4,#91D1C2,#DC0000` | Comma-separated group colors |\n| `--censor_show` | `true` | Whether to show censor markers |\n| `--censor_size` | `7` | Censor marker size |\n| `--confidence_show` | `true` | Whether to show confidence interval |\n| `--confidence_alpha` | `0.2` | Confidence band transparency |\n| `--risk_table_show` | `true` | Whether to show the risk table |\n| `--risk_table_border` | `true` | Whether to show the risk table border |\n| `--risk_table_panel` | `false` | Whether to show the risk table panel background |\n| `--risk_table_size` | `6` | Risk table font size |\n| `--axis_title_size` | `12` | Axis title font size |\n| `--axis_text_size` | `10` | Axis tick-label font size |\n| `--legend_text_size` | `11` | Legend text font size |\n\n## Input Requirements\n\n- The input file must contain the requested time, status, and group columns.\n- `.txt` inputs must be tab-delimited.\n- `time` must contain finite non-negative numeric values.\n- `status` must be coded as `0` for censored and `1` for event.\n- The risk group column must be a precomputed categorical grouping variable, not a continuous score column.\n- The risk group column must contain at least 2 groups after filtering.\n- `--line_colors` must provide at least one color per retained group when you override the default palette.\n- Rows with missing time, status, or group values are removed before analysis.\n- At least 2 complete observations must remain after filtering.\n- Near-unique or continuous-looking grouping columns are rejected before model fitting.\n- If `--auto_convert_days true` and `max(time) > 365`, the script assumes the retained time values are in days and converts them to the requested `--time_unit` when `time_unit` is `year` or `month`.\n- Only use `--auto_convert_days true` when the source time column is known to be in days.\n- If your source data are already in years or months, disable `--auto_convert_days` to avoid incorrect conversion.\n- `--statistics_method wald` only supports exactly 2 retained groups; use `logrank` for multi-group comparisons.\n- Invalid plotting parameters such as unsupported `--line_type` values are rejected before plotting.\n\nExample input:\n\n```text\nid\tfustat\tfutime\trisk_score\trisk_group\tGPR161\tRIBC2\nTCGA-C5-A1M5\t1\t5.62191780821918\t-1.10702407761445\tlow\t2.82521576230566\t5.35318564979635\nTCGA-VS-A94W\t0\t3.40547945205479\t-0.671246677921865\thigh\t4.26241812321536\t4.00802068790173\n```\n\nBundled test datasets:\n\n- `tests\/data\/km_sample1.txt`: baseline KM example with `risk_group`\n- `tests\/data\/km_sample2.txt`: alternate cohort for plotting and statistics examples\n- `tests\/data\/km_sample3.txt`: additional cohort for validation and repeated testing\n\n## Minimal Workflow\n\n1. Confirm the input file exists and identify the time, status, and group columns.\n2. Run `scripts\/main.R` with the requested output directory and any optional plot parameters.\n3. Check the output directory for `km-plot.pdf`.\n\nIf you omit `--input_file`, the script exits with `SKILL_MISSING_INPUT`.\n\n## Outputs\n\nExpected output:\n\n```text\n<output_dir>\/\n\u251c\u2500\u2500 km-plot.pdf\n\u2514\u2500\u2500 session_info.txt\n```\n\n## Interpretation Guide\n\n- Use the survival figure to inspect separation between groups over time.\n- Use the p-value annotation, confidence interval, and risk table in the figure to interpret group separation.\n\n## Time Conversion Caution\n\n- Automatic conversion is a convenience heuristic, not a unit detector.\n- The script only checks whether `max(time) > 365`; it does not infer the true source unit from metadata.\n- If the input time column is already expressed in years or months, run with `--auto_convert_days false`.\n- Review the console log for the conversion warning whenever `time_unit` is `year` or `month`.\n\n## Reproducibility Note\n\n- Repeated runs on identical input should be analytically consistent.\n- The exported `km-plot.pdf` may not be byte-identical across repeated runs because PDF metadata and graphics-device output can vary.\n- If you need byte-stable artifacts, add a deterministic PDF post-processing step outside this skill.\n\n## Do Not Use This Skill When\n\n- You need this tool to derive a cutoff or split a continuous score into risk groups.\n- You need multivariable Cox regression, covariate adjustment, or hazard-ratio modeling beyond the p-value route already exposed here.\n- You need a broader survival-analysis workflow with upstream feature engineering, biomarker selection, or data harmonization.\n- You need multiple plots, report generation, or downstream interpretation beyond producing one Kaplan-Meier figure and session metadata.\n\n## Read These Files When Needed\n\n| Need | File |\n|------|------|\n| Kaplan-Meier method details and interpretation | `references\/algorithm.md` |\n| More CLI examples | `references\/cli-guide.md` |\n| Error diagnosis | `references\/troubleshooting.md` |\n| Main execution entry point | `scripts\/main.R` |\n| Sample test data | `tests\/data\/km_sample1.txt`, `tests\/data\/km_sample2.txt`, `tests\/data\/km_sample3.txt` |\n\n## Quick Examples\n\nBasic Kaplan-Meier analysis:\n\n```bash\nRscript scripts\/main.R \\\n  --input_file tests\/data\/km_sample1.txt \\\n  --output_dir tests\/output_basic\n```\n\nCustom column names:\n\n```bash\nRscript scripts\/main.R \\\n  --input_file tests\/data\/km_sample1.txt \\\n  --time_col futime \\\n  --status_col fustat \\\n  --risk_col risk_group \\\n  --output_dir tests\/output_custom_columns\n```\n\nCustom plot title:\n\n```bash\nRscript scripts\/main.R \\\n  --input_file tests\/data\/km_sample2.txt \\\n  --title_main \"Study KM Curve\" \\\n  --output_dir tests\/output_title\n```\n\nHide confidence interval and risk table:\n\n```bash\nRscript scripts\/main.R \\\n  --input_file tests\/data\/km_sample3.txt \\\n  --confidence_show false \\\n  --risk_table_show false \\\n  --output_dir tests\/output_simple\n```\n\n## Validation\n\n```bash\nRscript scripts\/main.R --help\n```\n\n```bash\nRscript scripts\/main.R \\\n  --input_file tests\/data\/km_sample1.txt \\\n  --output_dir tests\/validation_output\n```\n\nAfter running analysis, verify that `tests\/validation_output\/km-plot.pdf` exists.\n\n## Common Errors\n\n- `SKILL_FILE_NOT_FOUND`: Input file path is wrong or inaccessible.\n- `SKILL_MISSING_COLUMNS`: A requested time, status, or risk group column is missing.\n- `SKILL_INVALID_DATA`: Input data is malformed or unsuitable for survival analysis.\n- `SKILL_INVALID_DATA`: A continuous or near-unique risk column was supplied where a categorical group column is required.\n- `SKILL_INVALID_PARAMETER`: An argument value is invalid.\n- `SKILL_INVALID_PARAMETER`: Plotting options such as `--line_type` or `--line_colors` are incompatible with the retained groups.\n- `SKILL_INSUFFICIENT_DATA`: Too few complete observations remain after filtering.\n- `SKILL_DEPENDENCY_MISSING`: A required R package such as `optparse` or `survival` is unavailable.\n\nIf the issue is not obvious, read `references\/troubleshooting.md`.\n","use_cases":["Generate survival curves from medical research datasets","Visualize survival differences between risk groups","Export single PDF survival figures for reports"],"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\/km-survival-curve\/SKILL.md","source_commit":"686e09dfdb45d9fa1a08664f06bb93769b3c9d91","license":"MIT","version":null,"listing_approved_at":"2026-11-24","created_at":"2026-11-23T07:16:47.000000Z","updated_at":"2026-11-24T07:13:04.000000Z","seo_title":"Kaplan-Meier Survival Curve Analysis Tool","seo_description":"Generate Kaplan-Meier survival curves from tabular data with time, event status, and risk group columns. Export single PDF figures.","seo_keywords":["Kaplan-Meier survival analysis","Survival curve generation","Medical research analysis","Survival data visualization","Survival curve export"]}}