本项目是一个肝细胞癌(HCC)经动脉化疗栓塞(TACE)治疗异质性(HTE)分析系统,采用机器学习方法预测个体化治疗效果(CATE,条件平均治疗效应)。
- 变量重要性排序:基于 RMST-Qini 指标评估各临床变量对治疗异质性的贡献
- 多算法递归特征增加:使用
survlearners包中的多种算法(Lasso、GRF、CoxPH 等)进行特征选择 - 最终模型训练与预测:使用
surv_fl_grf(因果生存森林)训练最终模型,预测患者级 CATE - 十分位 HR 验证:按 CATE 将患者分组,验证治疗效果的异质性
- 模型解释:基于 DALEX、SHAP 等方法提供全局和局部解释
flowchart TD
s1["步骤1: 基础函数定义"]
s2["步骤2: 变量重要性排序"]
s3["步骤3: 递归特征增加"]
s4["步骤4: PS加权HR函数"]
s5["步骤5: 三等分OW-HR分析"]
s6["步骤6: HR趋势筛选"]
s7["步骤7: 最终模型预测 (top12)"]
s8["步骤8: 十分位HR森林图"]
s9["步骤9: 最终模型预测 (top24)"]
s10["步骤10: 模型解释"]
s11["步骤11: Shiny应用准备"]
s1 --> s2
s2 --> s3
s3 --> s5
s5 --> s6
s2 --> s7
s7 --> s8
s2 --> s9
s7 --> s10
s7 --> s11
- 操作系统:Windows 10/11、macOS 10.15+、Linux(Ubuntu 18.04+)
- R 版本:R 4.1.0 或更高版本(推荐 R 4.3.0+)
- 内存:至少 8GB RAM(推荐 16GB)
- 磁盘空间:至少 2GB 可用空间
本项目依赖以下 R 包,按安装优先级排列:
核心依赖包:
pacman:包管理器tidyverse:数据处理套件openxlsx:Excel 文件读写here:项目路径管理
统计建模包:
grf:广义随机森林(用于 PS 估计和因果森林)survlearners:生存学习器(核心 CATE 算法)survival:生存分析基础包
机器学习与解释包:
DALEX:模型解释框架ingredients:模型解释组件kernelshap:SHAP 值计算shapviz:SHAP 可视化glmnet:正则化回归xgboost:梯度提升树ranger:随机森林e1071:支持向量机
- 下载并安装 R:https://cran.r-project.org/
- 下载并安装 RStudio(可选):https://posit.co/downloads/
打开命令行(PowerShell/Terminal),执行:
Rscript --version应显示 R 版本信息,例如 R scripting front-end version 4.3.x
在 R 或 RStudio 控制台中执行:
# 安装 pacman 包管理器
install.packages("pacman")
# 使用 pacman 批量安装依赖
pacman::p_load(
tidyverse,
openxlsx,
here,
grf,
survival,
DALEX,
ingredients,
kernelshap,
shapviz,
glmnet,
xgboost,
ranger,
e1071
)cd /path/to/HTE-HCC-TACE_V3/demoRscript generate_test_data.R此脚本将生成:
test_data/01_ISMIO2501_train_tidy.xlsx(训练集,300 样本)test_data/02_ISMIO2501_validation_tidy.xlsx(验证集,200 样本)test_data/03_ISMIO2501prevalidation_tidy.xlsx(前验证集,150 样本)test_data/PS变量最终确定.txt(PS 变量定义文件)
Rscript demo_run.R此脚本将检查环境并输出使用说明。
功能:逐变量计算 RMST-Qini 指标,评估变量对治疗异质性的贡献
运行命令:
cd code
Rscript 2-rmst24_qini_rank_by_variable_train.R输出文件:
output/2-rmst24_qini_rank_by_variable_train/06_rmst24_qini_by_variable_ranked_train.csv
运行时间:约 5-15 分钟(取决于样本量和变量数)
功能:按变量重要性顺序逐步增加特征,使用多种生存学习器算法拟合 CATE 模型
运行命令:
Rscript 3-survlearners_recursive_feature_growth.R输出文件:
output/3-survlearners_recursive_feature_growth/07_survlearners_recursive_feature_growth_all_algorithms.csvoutput/3-survlearners_recursive_feature_growth/08_survlearners_recursive_feature_growth_all_algorithms_patient_cate.csv
运行时间:约 30-60 分钟(取决于算法数量和特征数)
功能:按 CATE 三等分患者,在每个分组内计算 OW 加权 Cox HR
运行命令:
Rscript 5-survlearners_tertile_ow_neglogp_sum.R输出文件:
output/5-survlearners_tertile_ow_neglogp_sum/06_survlearners_tertile_ow_hr_p_results.csv
功能:根据 HR 趋势规则筛选最优算法和特征组合
运行命令:
Rscript 6-hr_trend_screening.R输出文件:
output/6-hr_trend_screening/04_hr_trend_top_candidates.csv
功能:使用前 12 个变量训练最终模型,并在三个数据集上预测 CATE
运行命令:
Rscript 7-final_surv_fl_grf_top12_predict.R输出文件:
output/7-final_surv_fl_grf_top12_predict/06_train_with_cate.xlsxoutput/7-final_surv_fl_grf_top12_predict/07_validation_with_cate.xlsxoutput/7-final_surv_fl_grf_top12_predict/08_prevalidation_with_cate.xlsx
运行时间:约 10-20 分钟
功能:按 CATE 十分位分组,绘制 OW/IPW/未调整 HR 森林图
运行命令:
Rscript 8-cate_decile_hr_forest.R输出文件:
output/8-cate_decile_hr_forest/09_train_cate_decile_hr_forest.pngoutput/8-cate_decile_hr_forest/10_validation_cate_decile_hr_forest.png
功能:使用前 24 个变量训练最终模型
运行命令:
Rscript 9-surv_fl_grf_top24_train_and_decile.R功能:使用 surrogate 模型和 SHAP 方法解释最终模型
运行命令:
Rscript 10-cate_surrogate_models_explain.R输出文件:
output/explain/目录下包含各模型的 SHAP 图和变量重要性图
运行时间:约 20-40 分钟
功能:为 Shiny Web 应用准备模型工件
运行命令:
Rscript 11-prepare_shiny_final_model_app_artifacts.R输出文件:
output/shiny_final_model_app/final_model_shiny_artifact.rds
demo/
├── README.md # 本说明文档
├── generate_test_data.R # 测试数据生成脚本
├── demo_run.R # 演示运行脚本
├── code/ # 原始代码(完整复刻)
│ ├── 1-rmst_hte_qini_fun.R # 基础函数定义
│ ├── 2-rmst24_qini_rank_by_variable_train.R
│ ├── 3-survlearners_recursive_feature_growth.R
│ ├── 4-ps_weighted_hr_function.R
│ ├── 5-survlearners_tertile_ow_neglogp_sum.R
│ ├── 6-hr_trend_screening.R
│ ├── 7-final_surv_fl_grf_top12_predict.R
│ ├── 8-cate_decile_hr_forest.R
│ ├── 9-surv_fl_grf_top24_train_and_decile.R
│ ├── 10-cate_surrogate_models_explain.R
│ ├── 11-prepare_shiny_final_model_app_artifacts.R
│ └── analysis_pipeline_and_dalex_interpretation_plan.md
├── test_data/ # 模拟测试数据
│ ├── 01_ISMIO2501_train_tidy.xlsx
│ ├── 02_ISMIO2501_validation_tidy.xlsx
│ ├── 03_ISMIO2501prevalidation_tidy.xlsx
│ └── PS变量最终确定.txt
└── output/ # 演示输出目录(运行后生成)
- 代码完全复刻:
code/目录包含原项目所有原始代码,未做任何修改 - 数据完全隔离:
test_data/包含独立生成的模拟数据,不覆盖原项目数据 - 输出完全隔离:所有结果输出到
demo/output/,不影响原项目
This project is a Hepatocellular Carcinoma (HCC) Transarterial Chemoembolization (TACE) Treatment Heterogeneity (HTE) Analysis System, which employs machine learning to predict individualized treatment effects (CATE, Conditional Average Treatment Effect).
- No-code Shiny online version: https://zhangkaimedicalapp.shinyapps.io/hcc-tace-treatment-recommendation/
- Variable Importance Ranking: Evaluate the contribution of each clinical variable to treatment heterogeneity based on the RMST-Qini metric
- Multi-Algorithm Recursive Feature Augmentation: Perform feature selection using multiple algorithms from the
survlearnerspackage (Lasso, GRF, CoxPH, etc.) - Final Model Training & Prediction: Train the final model with
surv_fl_grf(Causal Survival Forest) to generate patient-level CATE predictions - Decile HR Validation: Stratify patients by CATE values to validate heterogeneity of treatment effects
- Model Interpretation: Provide global and local explanations via DALEX, SHAP and other methodologies
flowchart TD
s1["Step 1: Define Base Functions"]
s2["Step 2: Rank Variables by Importance"]
s3["Step 3: Recursive Feature Augmentation"]
s4["Step 4: PS-Weighted HR Functions"]
s5["Step 5: Tertile OW-HR Analysis"]
s6["Step 6: HR Trend Screening"]
s7["Step 7: Final Model Prediction (Top12 Features)"]
s8["Step 8: Decile HR Forest Plot"]
s9["Step 9: Final Model Prediction (Top24 Features)"]
s10["Step 10: Model Interpretation"]
s11["Step 11: Shiny App Preparation"]
s1 --> s2
s2 --> s3
s3 --> s5
s5 --> s6
s2 --> s7
s7 --> s8
s2 --> s9
s7 --> s10
s7 --> s11
- Operating System: Windows 10/11, macOS 10.15+, Linux (Ubuntu 18.04+)
- R Version: R 4.1.0 or higher (R 4.3.0+ recommended)
- RAM: Minimum 8GB (16GB recommended)
- Disk Space: At least 2GB free storage
All required R packages listed below, sorted by installation priority:
Core Utility Packages:
pacman: Package managertidyverse: Integrated data processing suiteopenxlsx: Read/write Excel fileshere: Project path management
Statistical Modeling Packages:
grf: Generalized Random Forest (for propensity score estimation & causal forest)survlearners: Survival learning library (core CATE algorithm toolkit)survival: Foundation package for survival analysis
Machine Learning & Model Interpretation Packages:
DALEX: Unified model explanation frameworkingredients: Supporting components for model interpretationkernelshap: Efficient SHAP value computationshapviz: SHAP visualization toolkitglmnet: Regularized regressionxgboost: Gradient boosting decision treesranger: Fast random forest implementatione1071: Support vector machine & miscellaneous ML utilities
- Download and install base R: https://cran.r-project.org/
- Download and install RStudio (optional IDE): https://posit.co/downloads/
Open your terminal (PowerShell / Terminal) and run:
Rscript --versionA valid installation will output your R version, e.g. R scripting front-end version 4.3.x
Execute the following code in your R or RStudio console:
# Install pacman package manager first
install.packages("pacman")
# Bulk install all required packages via pacman
pacman::p_load(
tidyverse,
openxlsx,
here,
grf,
survival,
DALEX,
ingredients,
kernelshap,
shapviz,
glmnet,
xgboost,
ranger,
e1071
)cd /path/to/HTE-HCC-TACE_V3/demoRscript generate_test_data.RThis script outputs the following files:
test_data/01_ISMIO2501_train_tidy.xlsx(Training set, 300 samples)test_data/02_ISMIO2501_validation_tidy.xlsx(Validation set, 200 samples)test_data/03_ISMIO2501prevalidation_tidy.xlsx(Pre-validation set, 150 samples)test_data/PS_variable_definitions.txt(Propensity score variable dictionary)
Rscript demo_run.RThis script validates your runtime environment and prints usage instructions.
Function: Calculate RMST-Qini metrics for each predictor to quantify its impact on treatment heterogeneity Run Command:
cd code
Rscript 2-rmst24_qini_rank_by_variable_train.ROutput Files:
output/2-rmst24_qini_rank_by_variable_train/06_rmst24_qini_by_variable_ranked_train.csvRuntime: ~5–15 minutes (varies with sample size and predictor count)
Function: Iteratively add predictors ordered by importance, and fit CATE models with multiple survival learning algorithms Run Command:
Rscript 3-survlearners_recursive_feature_growth.ROutput Files:
output/3-survlearners_recursive_feature_growth/07_survlearners_recursive_feature_growth_all_algorithms.csvoutput/3-survlearners_recursive_feature_growth/08_survlearners_recursive_feature_growth_all_algorithms_patient_cate.csvRuntime: ~30–60 minutes (varies with algorithm count and feature pool size)
Function: Stratify patients into 3 equal CATE subgroups, and compute OW-weighted Cox hazard ratios within each stratum Run Command:
Rscript 5-survlearners_tertile_ow_neglogp_sum.ROutput Files:
output/5-survlearners_tertile_ow_neglogp_sum/06_survlearners_tertile_ow_hr_p_results.csv
Function: Filter optimal algorithm & feature combinations by predefined hazard ratio monotonicity criteria Run Command:
Rscript 6-hr_trend_screening.ROutput Files:
output/6-hr_trend_screening/04_hr_trend_top_candidates.csv
Function: Train final causal survival forest model on the top 12 most predictive variables, generate CATE predictions across all three datasets Run Command:
Rscript 7-final_surv_fl_grf_top12_predict.ROutput Files:
output/7-final_surv_fl_grf_top12_predict/06_train_with_cate.xlsxoutput/7-final_surv_fl_grf_top12_predict/07_validation_with_cate.xlsxoutput/7-final_surv_fl_grf_top12_predict/08_prevalidation_with_cate.xlsxRuntime: ~10–20 minutes
Function: Split patients into 10 CATE quantiles, generate forest plots comparing crude, OW-weighted, and IPW-adjusted hazard ratios Run Command:
Rscript 8-cate_decile_hr_forest.ROutput Files:
output/8-cate_decile_hr_forest/09_train_cate_decile_hr_forest.pngoutput/8-cate_decile_hr_forest/10_validation_cate_decile_hr_forest.png
Function: Train causal survival forest model using the top 24 ranked predictors Run Command:
Rscript 9-surv_fl_grf_top24_train_and_decile.RFunction: Generate surrogate explanation models and compute SHAP values to interpret the final CATE estimator Run Command:
Rscript 10-cate_surrogate_models_explain.ROutput Files:
- SHAP summary plots, partial dependence plots, and variable importance charts stored under
output/explain/Runtime: ~20–40 minutes
Function: Export serialized model artifacts required for deployment of the interactive Shiny web app Run Command:
Rscript 11-prepare_shiny_final_model_app_artifacts.ROutput Files:
output/shiny_final_model_app/final_model_shiny_artifact.rds
demo/
├── README.md # This documentation file
├── generate_test_data.R # Synthetic dataset generation script
├── demo_run.R # Main demo entry validation script
├── code/ # Full unmodified source code archive
│ ├── 1-rmst_hte_qini_fun.R # Base metric & utility function definitions
│ ├── 2-rmst24_qini_rank_by_variable_train.R
│ ├── 3-survlearners_recursive_feature_growth.R
│ ├── 4-ps_weighted_hr_function.R
│ ├── 5-survlearners_tertile_ow_neglogp_sum.R
│ ├── 6-hr_trend_screening.R
│ ├── 7-final_surv_fl_grf_top12_predict.R
│ ├── 8-cate_decile_hr_forest.R
│ ├── 9-surv_fl_grf_top24_train_and_decile.R
│ ├── 10-cate_surrogate_models_explain.R
│ ├── 11-prepare_shiny_final_model_app_artifacts.R
│ └── analysis_pipeline_and_dalex_interpretation_plan.md
├── test_data/ # Synthetic isolated test datasets
│ ├── 01_ISMIO2501_train_tidy.xlsx
│ ├── 02_ISMIO2501_validation_tidy.xlsx
│ ├── 03_ISMIO2501prevalidation_tidy.xlsx
│ └── PS_variable_definitions.txt
└── output/ # Auto-generated results directory (created after execution)
- Full Code Replication: The
code/folder contains unaltered original source code from the parent research project - Data Isolation: All synthetic test data resides in
test_data/and will not overwrite original clinical datasets - Output Isolation: All analytical outputs are saved exclusively to
demo/output/, with zero risk of modifying parent project files