## ----setup, include=FALSE-----------------------------------------------------
# The Chinese title in the YAML header is intentional; the index entry stays in
# ASCII so that the vignette index is readable in any locale.
options(rmarkdown.html_vignette.check_title = FALSE)
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 6,
  fig.height = 4,
  dpi = 110
)

## ----quick-start--------------------------------------------------------------
library(uniml)

development <- read.csv(
  system.file("extdata", "uniml-cvd-mortality.csv", package = "uniml")
)
set.seed(123)
development <- development[1:400, ]

# 1. 声明任务
spec <- uniml_spec(
  task_type = "binary",
  outcome = "died",
  positive = "yes",
  id = "id"
)

# 2. 预处理：中位数/众数填补 + z 分数标准化 + one-hot
spec <- set_preprocess(spec, impute = "median_mode", scale = "zscore", encode = "onehot")

# 3. 特征筛选：单因素 + LASSO 取交集
spec <- set_feature_selection(spec, methods = c("univariate", "lasso"), combine = "intersect")

# 4. 模型：logistic（glmnet）和决策树
spec <- set_learners(spec, c("lr", "cart"), tune = FALSE)

# 5. 评价设置：bootstrap 置信区间 + Platt 校准 + 用验证集选模型
spec <- set_evaluation(
  spec,
  ci = "bootstrap",
  calibration = "platt",
  select_on = "validation"
)

# 6. SHAP：Kernel SHAP 是工作流里最贵的一步，解释行数与背景样本量由规格控制，
#    这里取小值让 vignette 跑得快；要出论文级图时把两个数调大即可。
spec <- set_explain(spec, n_explain = 20, bg_n = 20)

# 7. 切分并跑通
split <- make_split(as_uniml_data(development, spec), prop = c(0.8, 0.2),
                    stratify = TRUE, seed = 123)

result <- run_workflow(
  spec,
  train = split$train$data,
  validation = split$validation$data,
  ci_R = 25
)

## ----results------------------------------------------------------------------
result$metrics[, c("model", "set", "probability", "auc", "brier", "accuracy")]
result$ranking[, c("model", "score", "rank", "set")]
result$selection$selected_features

## ----roc, fig.cap = "验证集 ROC 曲线"----------------------------------------------
plot_roc(result, set = "validation")

## ----calibration, fig.cap = "校准曲线（原始 vs 校准后）"---------------------------------
plot_calibration(result, set = "validation")

## ----dca, fig.cap = "决策曲线分析"--------------------------------------------------
plot_dca(result)

## ----shap, fig.cap = "最优模型的 SHAP 重要性"-----------------------------------------
explained <- explain_shap(result, model = "best", n_explain = 25, bg_n = 15)
plot_shap_importance(explained)

## ----predict------------------------------------------------------------------
scored <- predict_uniml(result, newdata = split$validation$data, model = "best")
head(scored[, c("model", "prob", "calibrated_prob", "probability", "predicted")])

## ----export-------------------------------------------------------------------
out <- tempfile("uniml-zh-")
exported <- export_results(result, dir = out, include_plots = FALSE)
basename(exported$files)

## ----tripod-------------------------------------------------------------------
checklist <- tripod_ai_check(result)
table(checklist$status)
subset(checklist, status != "reported", select = c("domain", "status"))

