我们的实验室使用我们PI称为“修改后的Bland–Altman图”来分析回归质量。我使用Seaborn编写的代码只能处理离散数据,我希望能将其推广到更广泛的应用。
Bland–Altman图比较两个测量值之间的差异与它们的平均值。所谓的“修改”是指x轴不再是平均值,而是真实值。y轴是预测值与真实值之间的差异。实际上,修改后的B–A图可以被视为从y=x线的残差图——即predicted=truth
线的残差图。
生成此图的代码以及一个示例如下所示。
def modified_bland_altman_plot(predicted, truth): predicted = np.asarray(predicted) truth = np.asarray(truth, dtype=np.int) # np.int是用于stripplot的临时解决方案 diff = predicted - truth ax = sns.stripplot(truth, diff, jitter=True) ax.set(xlabel='truth', ylabel='difference from truth', title="Modified Bland-Altman Plot") # 在0处绘制一条水平线 ax.axhline(0, ls=":", c=".2") return ax
诚然,这个例子显示了预测中的严重偏差,表现为向下的斜率。
我对以下两点感到好奇:
- 这些“修改后的Bland–Altman图”是否有公认的名称?
- 如何为非离散数据创建这些图?我们使用
stripplot
,它需要离散数据。我知道seaborn有residplot
函数,但它不接受自定义函数来测量残差的线,例如predicted=true
。相反,它是从它计算的最佳拟合线来测量残差的。
回答:
看起来你在这里需要的是一个标准的散点图:
import matplotlib.pyplot as pltimport numpy as np; np.random.seed(1)def modified_bland_altman_plot(predicted, truth): predicted = np.asarray(predicted) truth = np.asarray(truth) diff = predicted - truth fig, ax = plt.subplots() ax.scatter(truth, diff, s=9, c=truth, cmap="rainbow") ax.set_xlabel('truth') ax.set_ylabel('difference from truth') ax.set_title("Modified Bland-Altman Plot") # 在0处绘制一条水平线 ax.axhline(0, ls=":", c=".2") return axx = np.random.rayleigh(scale=10, size=201)y = np.random.normal(size=len(x))+10-x/10.modified_bland_altman_plot(y, x)plt.show()