R语言Bland Altman图排除异常值及置信区间设置方法
Bland-Altman图调整方案
BlandAltmanLeh包的bland.altman.plot()没有内置一键剔除异常值、仅显示置信区间的专用参数,可通过以下步骤实现需求:
1 异常值剔除
先调用包内置的统计量计算函数拿到基础计算结果,按规则筛除异常值后再绘图,常规异常值判定规则为「两个方法的差值落在 差值均值±1.96倍差值标准差(即95%一致性界限)范围外」,可按需调整阈值:
library(BlandAltmanLeh) library(ggplot2) # 计算Bland-Altman分析所有基础统计量 ba_stats <- bland.altman.stats(m1, m2) # 组装绘图用数据框 ba_df <- data.frame( mean_val = ba_stats$means, diff_val = ba_stats$diffs ) # 标记并剔除异常值 ba_df$is_outlier <- with(ba_df, diff_val < ba_stats$lower.limit | diff_val > ba_stats$upper.limit ) ba_df_clean <- ba_df[!ba_df$is_outlier, ]
2 仅展示置信区间
直接基于清洗后的数据自定义绘图,可自由控制显示的元素,不需要的参考线、图层直接注释删除即可,比修改bland.altman.plot()返回的默认ggplot对象(需要手动删内置图层,容易出错)可控性更高:
# 从统计量结果中直接取所需的CI值 ci_mean <- ba_stats$CI.lines ci_loa_low <- ba_stats$CI.lines.loa.lower ci_loa_high <- ba_stats$CI.lines.loa.upper # 自定义绘图 S_plot_clean <- ggplot(ba_df_clean, aes(x = mean_val, y = diff_val)) + geom_point(size = 1.8) + # 若需要显示均值、一致性界限的参考线,取消下面三行注释即可 # geom_hline(yintercept = ba_stats$mean.diffs, linetype = "dashed", color = "navy") + # geom_hline(yintercept = ba_stats$lower.limit, linetype = "dashed", color = "firebrick") + # geom_hline(yintercept = ba_stats$upper.limit, linetype = "dashed", color = "firebrick") + # 绘制置信区间带,不需要的区间直接注释删除 annotate("rect", xmin = -Inf, xmax = Inf, ymin = ci_mean[2], ymax = ci_mean[3], fill = "navy", alpha = 0.2) + annotate("rect", xmin = -Inf, xmax = Inf, ymin = ci_loa_low[2], ymax = ci_loa_low[3], fill = "firebrick", alpha = 0.2) + annotate("rect", xmin = -Inf, xmax = Inf, ymin = ci_loa_high[2], ymax = ci_loa_high[3], fill = "firebrick", alpha = 0.2) + labs(x = "两种方法测量均值", y = "两种方法测量差值", title = "Bland Altman plot Stress") + theme_bw() print(S_plot_clean)
如果一定要修改默认生成的
S_plot对象,可通过S_plot$layers查看所有内置图层,定位到绘制参考线的GeomHline图层对应的索引后,用S_plot$layers[[索引]] <- NULL删除对应图层即可,但这种方法依赖包内置的图层顺序,版本更新后可能失效,更推荐用自定义绘图的方式。
内容的提问来源于stack exchange,提问作者Christine
相关产品推荐
相关产品推荐

