寻求支持比例偏差与异方差的Bland-Altman图工具
支持比例偏差/异方差的Bland-Altman工具包(R & Python)
R语言工具包
1. methodcompare包
- 自带的
bland.altman.plot()函数能直接检测比例偏差,通过拟合线性模型分析差异与均值的关联,自动适配异方差场景——会用加权置信区间调整,避免普通置信区间的偏差。 - 简单示例:
library(methodcompare) # 模拟带比例偏差的测试数据 set.seed(123) mean_vals <- runif(100, 10, 50) diff_vals <- 0.1*mean_vals + rnorm(100, 0, 2) method1 <- mean_vals + diff_vals/2 method2 <- mean_vals - diff_vals/2 # 生成带加权CI的Bland-Altman图,自动检测比例偏差 bland.altman.plot(method1, method2, plot.type = "difference", weighted = TRUE)
2. BlandAltmanLeh包
- 专门针对Bland-Altman分析做了扩展,支持比例偏差的统计检验(通过回归分析差异和均值的相关性),还能生成异方差调整后的置信区间图,输出里会直接给出比例偏差的p值等统计结果。
- 简单示例:
library(BlandAltmanLeh) # 用上面的模拟数据 ba_result <- bland.altman.plot(method1, method2, probs = c(0.05, 0.95), weighted = TRUE) # 查看比例偏差的检验结果 summary(ba_result)
Python语言工具包
1. blandaltman第三方库
- 这个库支持比例偏差检测,会自动拟合差异与均值的线性回归来判断是否存在比例偏差,还能通过加权置信区间处理异方差问题,输出里包含回归系数和检验统计量。
- 简单示例:
import numpy as np from blandaltman import bland_altman_plot import matplotlib.pyplot as plt # 模拟带比例偏差的数据 np.random.seed(123) mean_vals = np.random.uniform(10, 50, 100) diff_vals = 0.1*mean_vals + np.random.normal(0, 2, 100) method1 = mean_vals + diff_vals/2 method2 = mean_vals - diff_vals/2 # 绘制带比例偏差检测和加权CI的图 fig, ax = plt.subplots() bland_altman_plot(method1, method2, ax=ax, weighted=True, regression=True) plt.title("Bland-Altman Plot with Proportional Bias Detection") plt.show()
2. 手动结合scipy+matplotlib定制实现
- 如果需要更灵活的自定义分析,可以用
scipy.stats.linregress检验差异与均值的相关性(判断比例偏差),用加权最小二乘法计算异方差调整后的置信区间,再手动绘图。 - 简单示例:
import numpy as np import matplotlib.pyplot as plt from scipy.stats import linregress # 用上面的模拟数据 mean_vals = (method1 + method2)/2 diff_vals = method1 - method2 # 检验比例偏差 slope, intercept, r_value, p_value, std_err = linregress(mean_vals, diff_vals) print(f"比例偏差检验:斜率={slope:.4f},p值={p_value:.4f}") # 计算加权置信区间(用均值的倒数做权重,适配异方差) weights = 1/mean_vals weighted_mean_diff = np.average(diff_vals, weights=weights) weighted_std = np.sqrt(np.average((diff_vals - weighted_mean_diff)**2, weights=weights)) ci_lower = weighted_mean_diff - 1.96*weighted_std ci_upper = weighted_mean_diff + 1.96*weighted_std # 绘图 plt.scatter(mean_vals, diff_vals) plt.axhline(weighted_mean_diff, color='red', linestyle='--') plt.axhline(ci_lower, color='gray', linestyle=':') plt.axhline(ci_upper, color='gray', linestyle=':') # 添加比例偏差回归线 plt.plot(mean_vals, intercept + slope*mean_vals, color='blue') plt.xlabel("Mean of Two Methods") plt.ylabel("Difference Between Methods") plt.title("Custom Bland-Altman Plot with Proportional Bias & Heteroscedasticity Adjustment") plt.show()
内容的提问来源于stack exchange,提问作者Milad Shahidi
相关产品推荐
相关产品推荐

