Seaborn 中经过修改的 Bland–Altman 图

Modified Bland–Altman plot in Seaborn

我的实验室使用我们的 PI 所说的 "modified Bland–Altman plots" 来分析回归质量。我用Seaborn写的代码只处理离散数据,我想概括一下。

A Bland–Altman plot 将两个度量之间的差异与其平均值进行比较。 "modification" 是 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 is a hack for stripplot
    diff = predicted - truth

    ax = sns.stripplot(truth, diff, jitter=True)
    ax.set(xlabel='truth', ylabel='difference from truth', title="Modified Bland-Altman Plot")

    # Plot a horizontal line at 0
    ax.axhline(0, ls=":", c=".2")

    return ax

不可否认,这个例子在其预测中存在严重的偏差,如向下的斜率所示。


我对两件事很好奇:

  1. 这些 "modified Bland–Altman plots" 是否有普遍接受的名称?
  2. 如何为非离散数据创建这些?我们使用 stripplot,这需要离散数据。我知道 seaborn 有 residplot 函数,但它没有为测量残差的行采用自定义函数,例如predicted=true。相反,它从计算出的最佳拟合线开始测量。

看来您正在寻找标准散点图:

import matplotlib.pyplot as plt
import 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")

    # Plot a horizontal line at 0
    ax.axhline(0, ls=":", c=".2")

    return ax

x = 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()