你希望看到 **完整的最优参数筛选表格**,包含以下性能指标:
- 灵敏度(Sensitivity)
- 特异度(Specificity)
- 阳性预测值(PPV)
- 阴性预测值(NPV)
- 阳性似然比(PLR)
- 阴性似然比(NLR)
- 马修相关系数(MCC)
- 约登指数(Youden's Index)
---
## ✅ 目标
我们将:
1. 使用 `roc.analysis()` 生成完整 ROC 分析数据
2. 提取并整理成一个 **清晰、可读性强的性能评估表**
3. 按 **约登指数从高到低排序**,标记出最优参数组合
4. 输出为 R 控制台显示 + 可导出的 `data.frame`
---
### ✅ 第一步:确保正确运行 `roc.analysis()` 并生成完整网格
```r
# 定义参数搜索范围(必须是向量!)
delta_grid <- seq(2.0, 3.0, by = 0.1) # δ 参数(基线波动阈值)
detect_grid <- seq(2.1, 2.9, by = 0.1) # detection 阈值
# 执行 ROC 分析
parms <- roc.analysis(
flucyl_yours, # 你的时间序列对象(ts 类型)
i.param.values = delta_grid,
i.detection.values = detect_grid,
i.min.seasons = 6, # 至少6个流行季用于建模
i.graph = TRUE, # 绘制 ROC 曲线
i.graph.file = TRUE,
i.graph.file.name = "roc_curve_optimal_delta",
i.output = ".",
i.mem.info = TRUE
)
```
> ⚠️ 注意:如果 `parms$roc.data` 中没有 `i.param.value` 和 `i.detection.value`,说明输入不是向量,请检查上面代码。
---
### ✅ 第二步:提取并整理完整性能评估表
```r
# 获取原始结果
roc_full <- as.data.frame(parms$roc.data)
# 查看列名(用于映射)
cat("🔍 实际列名:\n")
print(names(roc_full))
# 标准化列名映射(自动识别)
col_map <- list(
sensitivity = grep("sens|SENS", names(roc_full), value = TRUE)[1],
specificity = grep("spec|SPEC", names(roc_full), value = TRUE)[1],
ppv = grep("pos.pred|PPV|positive.predictive", names(roc_full), value = TRUE)[1],
npv = grep("neg.pred|NPV|negative.predictive", names(roc_full), value = TRUE)[1],
plr = grep("pos.like|PLR|positive.likehood", names(roc_full), value = TRUE)[1],
nlr = grep("neg.like|NLR|negative.likehood", names(roc_full), value = TRUE)[1],
mcc = grep("matthew|correlation.coefficient|MCC", names(roc_full), value = TRUE)[1],
youden = grep("you|You|jou|YI|youdens.index", names(roc_full), value = TRUE)[1]
)
# 构建最终表格
performance_table <- data.frame(
`δ (i.param.value)` = roc_full$i.param.value,
`Detection Threshold` = roc_full$i.detection.value,
Sensitivity = round(roc_full[[col_map$sensitivity]], 3),
Specificity = round(roc_full[[col_map$specificity]], 3),
`Positive Predictive Value (PPV)` = round(roc_full[[col_map$ppv]], 3),
`Negative Predictive Value (NPV)` = round(roc_full[[col_map$npv]], 3),
`Positive Likelihood Ratio (PLR)` = round(roc_full[[col_map$plr]], 3),
`Negative Likelihood Ratio (NLR)` = round(roc_full[[col_map$nlr]], 3),
`Matthews Correlation Coefficient (MCC)` = round(roc_full[[col_map$mcc]], 3),
`Youden's Index (J)` = round(roc_full[[col_map$youden]], 3)
)
# 排序:按 Youden 指数降序
performance_table <- performance_table[order(-performance_table$`Youden's Index (J)`), ]
# 添加一列表示“是否最优”
performance_table$`Optimal?` <- ""
performance_table$`Optimal?`[1] <- "⭐ Best"
# 重置行名
rownames(performance_table) <- NULL
```
---
### ✅ 第三步:在控制台打印美观表格(前10行示例)
```r
# 显示前10个最佳参数组合
cat("🏆 最优参数组合(按 Youden 指数排序,Top 10):\n\n")
print(head(performance_table, 10))
```
#### 示例输出(模拟):
```
δ (i.param.value) Detection Threshold Sensitivity Specificity PPV NPV PLR NLR MCC Youden's Index (J) Optimal?
1 2.5 2.5 0.880 0.920 0.78 0.96 11.0 0.13 0.76 0.800 ⭐ Best
2 2.4 2.5 0.860 0.930 0.79 0.95 12.3 0.15 0.75 0.790
3 2.6 2.6 0.870 0.910 0.77 0.96 9.7 0.14 0.74 0.780
...
```
---
### ✅ 第四步:导出为 CSV 文件(便于 Excel 查看)
```r
# 导出完整表格
write.csv(performance_table, "mem_roc_performance_full.csv", row.names = FALSE, fileEncoding = "UTF-8")
cat("\n✅ 完整性能表已保存为:mem_roc_performance_full.csv\n")
```
---
### ✅ 第五步:可视化 —— 绘制 Youden Index 热图(可选)
```r
library(ggplot2)
library(reshape2)
# 提取 δ 和 detection 的组合与 Youden 值
heatmap_data <- data.frame(
Delta = performance_table$`δ (i.param.value)`,
Detection = performance_table$`Detection Threshold`,
Youden = performance_table$`Youden's Index (J)`
)
# 热图
ggplot(heatmap_data, aes(x = Detection, y = Delta, fill = Youden)) +
geom_tile() +
scale_fill_gradient(low = "white", high = "red", name = "Youden Index") +
theme_minimal() +
labs(title = "Youden Index Heatmap: Optimal δ vs Detection Threshold",
x = "Detection Threshold", y = "Delta (Base Threshold)") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
# 保存图像
ggsave("youden_heatmap.png", width = 8, height = 6, dpi = 150)
```
---
## ✅ 总结:你现在可以做到
| 功能 | 是否实现 |
|------|----------|
| 展示所有参数组合的性能 | ✅ |
| 包含全部8项关键指标 | ✅ |
| 按 Youden 指数排序 | ✅ |
| 标记最优参数 | ✅ |
| 导出为 CSV | ✅ |
| 可视化热图 | ✅ |
---
##