File contents Skill: survival-analysis
生存分析 (Survival Analysis) 與 Word 報告匯出。
Metadata
Description : 執行 Kaplan-Meier 非參數估計、Log-rank 組別比較、Cox 比例風險迴歸 (含 PH 假設檢驗)、Schoenfeld 殘差診斷;輸出生存曲線、風險比森林圖、累積事件圖。
Version : 1.0.0
Entrypoint : orchestrator.py(CLI: --source {local,sql,hadoop} + --time <col> + --event <col> + --groups <cols> + --covariates <cols> + --analysis {km,cox,both})
Related :
假設檢驗:advanced-data-analytics
迴歸建模(線性/Logistic):regression-analytics
多因子 ANOVA:factorial-anova
資料規格
生存分析的資料每一列代表一個觀測對象,需要以下欄位:
欄位
型別
說明
--time
數值
觀測時間(距離起始的時間單位,例如天/週/月)
--event
0/1
事件狀態:1 = 事件發生 (死亡/流失/失效),0 = 審查 (censored)
--groups(可選)
類別
分組變量(例如治療組別、性別);用於 KM 曲線分組與 Log-rank
--covariates(可選)
數值或類別
Cox 迴歸的共變量(年齡、劑量、其他 predictor)
審查 (Censoring) 的直覺:觀察截止時該對象「還沒發生事件」的情況,例如研究結束時仍存活、失聯、退出研究。生存分析的核心就是要正確利用這些不完整的資料。
Analysis Modes
1. km — Kaplan-Meier 非參數估計
輸入 : --time + --event(+ 可選 --groups)
輸出 :
生存函數 S(t) 表 : 各時間點的估計生存率 + 95% CI
中位生存時間 (median survival) + 95% CI
限制平均生存時間 (RMST, Restricted Mean Survival Time)
若有 --groups:Log-rank 檢驗 (survdiff) → χ² + p 值 + 各組別對比
Kaplan-Meier 曲線圖 + Risk Table (每時間點還在觀察的人數)
2. cox — Cox 比例風險迴歸
輸入 : --time + --event + --covariates(可與 --groups 混用)
輸出 :
係數表 : 每個 covariate 的 β 估計、SE、z、p、Hazard Ratio (HR) + 95% CI
模型指標 : Concordance (C-index)、Likelihood ratio test、Wald test、Score test
比例風險假設檢驗 (cox.zph, Schoenfeld residuals) → 每個變量的檢驗 p 值 + Global test
森林圖 (HR + 95% CI)
Schoenfeld 殘差圖 (診斷 PH 假設)
3. both — 依序執行 KM 與 Cox
KM 部分:--groups 若存在則做組別比較
Cox 部分:如果 --covariates 為空且有 --groups,將 --groups 當作 covariate 使用
Output Location
預設輸出目錄 :./outcome-temp/
fetched_data.csv:資料快照
Survival_Report.docx:Word 報告
km_curve.png:Kaplan-Meier 曲線
cox_forest.png:Cox 森林圖
schoenfeld.png:PH 假設殘差圖
Workflow (SOP)
fetch : schema 校驗(time > 0、event ∈ {0, 1}、groups 至少 2 levels 等)
execute : R 端 survfit (KM) / coxph (Cox) / cox.zph (PH check)
render : 依 analysis mode 動態組裝 Word 章節
交付 : .docx 落地在 output-dir
Constraints
time 必須 > 0;不允許負值或 0
event 僅接受 0/1(或 TRUE/FALSE)
Cox 迴歸的類別 covariate 自動 to factor + dummy encoding
Listwise deletion:任一必需欄位為 NA 的列丟棄,記入報告
中位生存時間若在觀察期內未達到(>50% 存活),顯示為 "not reached"
事件數 <10 時強烈警告(Cox 統計功效不足)
Dependencies
Python 3.10+: pandas sqlalchemy python-docx openpyxl pyhive thrift thrift-sasl pure-sasl
R 4.x:
必要 : ggplot2 jsonlite survival survminer broom
可選 : showtext sysfonts (CJK 字型)
1 --- 2 name: survival-analysis 3 description: Skill: survival-analysis 4 --- 5 6 # Skill: survival-analysis 7 8 生存分析 (Survival Analysis) 與 Word 報告匯出。 9 10 ## Metadata 11 - **Description**: 執行 Kaplan-Meier 非參數估計、Log-rank 組別比較、Cox 比例風險迴歸 (含 PH 假設檢驗)、Schoenfeld 殘差診斷;輸出生存曲線、風險比森林圖、累積事件圖。 12 - **Version**: 1.0.0 13 - **Entrypoint**: `orchestrator.py`(CLI: `--source {local,sql,hadoop}` + `--time <col>` + `--event <col>` + `--groups <cols>` + `--covariates <cols>` + `--analysis {km,cox,both}`) 14 - **Related**: 15 - 假設檢驗:`advanced-data-analytics` 16 - 迴歸建模(線性/Logistic):`regression-analytics` 17 - 多因子 ANOVA:`factorial-anova` 18 19 ## 資料規格 20 21 生存分析的資料每一列代表一個觀測對象,需要以下欄位: 22 23 | 欄位 | 型別 | 說明 | 24 |---|---|---| 25 | `--time` | 數值 | 觀測時間(距離起始的時間單位,例如天/週/月) | 26 | `--event` | 0/1 | 事件狀態:**1 = 事件發生**(死亡/流失/失效),**0 = 審查** (censored) | 27 | `--groups`(可選) | 類別 | 分組變量(例如治療組別、性別);用於 KM 曲線分組與 Log-rank | 28 | `--covariates`(可選) | 數值或類別 | Cox 迴歸的共變量(年齡、劑量、其他 predictor) | 29 30 **審查 (Censoring)** 的直覺:觀察截止時該對象「還沒發生事件」的情況,例如研究結束時仍存活、失聯、退出研究。生存分析的核心就是要正確利用這些不完整的資料。 31 32 ## Analysis Modes 33 34 ### 1. `km` — Kaplan-Meier 非參數估計 35 - **輸入**: `--time` + `--event`(+ 可選 `--groups`) 36 - **輸出**: 37 - **生存函數 S(t) 表**: 各時間點的估計生存率 + 95% CI 38 - **中位生存時間** (median survival) + 95% CI 39 - **限制平均生存時間** (RMST, Restricted Mean Survival Time) 40 - 若有 `--groups`:**Log-rank 檢驗** (`survdiff`) → χ² + p 值 + 各組別對比 41 - **Kaplan-Meier 曲線圖** + Risk Table (每時間點還在觀察的人數) 42 43 ### 2. `cox` — Cox 比例風險迴歸 44 - **輸入**: `--time` + `--event` + `--covariates`(可與 `--groups` 混用) 45 - **輸出**: 46 - **係數表**: 每個 covariate 的 β 估計、SE、z、p、**Hazard Ratio (HR)** + 95% CI 47 - **模型指標**: Concordance (C-index)、Likelihood ratio test、Wald test、Score test 48 - **比例風險假設檢驗** (`cox.zph`, Schoenfeld residuals) → 每個變量的檢驗 p 值 + Global test 49 - **森林圖**(HR + 95% CI) 50 - **Schoenfeld 殘差圖**(診斷 PH 假設) 51 52 ### 3. `both` — 依序執行 KM 與 Cox 53 - KM 部分:`--groups` 若存在則做組別比較 54 - Cox 部分:如果 `--covariates` 為空且有 `--groups`,將 `--groups` 當作 covariate 使用 55 56 ## Output Location 57 58 **預設輸出目錄**:`./outcome-temp/` 59 60 - `fetched_data.csv`:資料快照 61 - `Survival_Report.docx`:Word 報告 62 - `km_curve.png`:Kaplan-Meier 曲線 63 - `cox_forest.png`:Cox 森林圖 64 - `schoenfeld.png`:PH 假設殘差圖 65 66 ## Workflow (SOP) 67 1. **fetch**: schema 校驗(`time > 0`、`event ∈ {0, 1}`、`groups` 至少 2 levels 等) 68 2. **execute**: R 端 `survfit` (KM) / `coxph` (Cox) / `cox.zph` (PH check) 69 3. **render**: 依 analysis mode 動態組裝 Word 章節 70 4. **交付**: `.docx` 落地在 output-dir 71 72 ## Constraints 73 - `time` 必須 > 0;不允許負值或 0 74 - `event` 僅接受 0/1(或 TRUE/FALSE) 75 - Cox 迴歸的類別 covariate 自動 to factor + dummy encoding 76 - Listwise deletion:任一必需欄位為 NA 的列丟棄,記入報告 77 - 中位生存時間若在觀察期內未達到(>50% 存活),顯示為 "not reached" 78 - 事件數 <10 時強烈警告(Cox 統計功效不足) 79 80 ## Dependencies 81 - Python 3.10+: `pandas sqlalchemy python-docx openpyxl pyhive thrift thrift-sasl pure-sasl` 82 - R 4.x: 83 - **必要**: `ggplot2 jsonlite survival survminer broom` 84 - **可選**: `showtext sysfonts` (CJK 字型)
kaito41418-ux/r-stats-skills/tree/main/skills/survival-analysis commit c13d03b37c
Frequently asked questions How do I install the Survival Analysis skill? Run npx skillmds@latest add kaito41418-ux/survival-analysis in your terminal (requires Node.js), paste this page's agent-chat prompt into Claude, Cursor, or any MCP-connected agent, or download the SKILL.md file and copy it into your agent's skills directory.
What does the Survival Analysis skill do? Skill: survival-analysis It is listed under Coding & Dev Tools on SkillMD.
Is Survival Analysis safe to use? This skill has not completed SkillMD's automated safety review yet. SkillMD never runs a skill's scripts for you; review the SKILL.md before installing.
Which AI agents work with Survival Analysis? This skill is tagged as working with Claude Code, Claude.ai, OpenAI Codex. SKILL.md is an open format, so most agents that read a skills directory can load it too.
Is Survival Analysis free to use? Yes. Installing skills from SkillMD is free, and the skill stays under its author's original license.
Who published Survival Analysis? kaito41418-ux (@kaito41418-ux) published this skill. Their other Agent Skills are listed on their SkillMD profile.