Python_基于statsmodel包画Bland altman plot (Mean Difference Plot)用于预测结果分析

本文介绍了如何使用Python的statsmodels库来绘制BlandAltman图,这是一种评估两个测量方法间一致性的重要工具。文章提供了一个API接口的详细说明,包括参数解释,并给出了一个示例代码,展示如何在同一图上绘制三个Bland-Altman子图。此外,还给出了一个自定义函数`bland_altman_plot_my`的实现,用于绘制BlandAltman图。
摘要由CSDN通过智能技术生成


本文基于Python包——statsmodel的Bland Altman plot的画法,statsmodel的官方介绍参考: https://www.statsmodels.org/stable/index.html

API接口介绍

在这里插入图片描述
上图为mean_diff_plot的API接口,其中各个参数
m1 & m2: 1-d array, 其中一个是estimated value sequence, 另一个是ground-truth value sequence;
ax: 如果ax为None, 则会创建一个figure; 否则,该mean_diff_plot会绘制在该参数指定的axis上;
scatter_kwds: dict format, 指定了 m1和m2中的值组成的点对pair的呈现格式;
mean_line_kwds: dict format, 指定了mean line的呈现格式;
limit_lines_kwdsdict format, 制定了limit line的呈现格式;

使用示例

下面的示例代码: 将三张Bland-Altman subplot画在同一张figure中, 并将该figure保存在dir目录下, 保存格式为: jpg和svg.


```python
# -*- coding: utf-8 -*-
import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import statsmodels.api as sm

def bland_altman_plot_wrap(gts, preds, alg_name, dir='../result', random_seed=0, calibration=False):
	fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(18, 5))
	sm.graphics.mean_diff_plot(np.array(gts[0]), np.array(preds[0]), ax=ax1, scatter_kwds=dict(color='deepskyblue'), mean_line_kwds=dict(color='red'), limit_lines_kwds=dict(color='black', linewidth=1.5))  # m1, m2: 1-dim vector, modified source file.
	sm.graphics.mean_diff_plot(np.array(gts[1]), np.array(preds[1]), ax=ax2, scatter_kwds=dict(color='deepskyblue'), mean_line_kwds=dict(color='red'), limit_lines_kwds=dict(color='black', linewidth=1.5))
	sm.graphics.mean_diff_plot(np.array(gts[2]), np.array(preds[2]), ax=ax3, scatter_kwds=dict(color='deepskyblue'), mean_line_kwds=dict(color='red'), limit_lines_kwds=dict(color='black', linewidth=1.5))
	
    fig_name = 'bland-altman-plot_{}_{}_seed-{}'.format(alg_name, calibration, random_seed)
	#plt.subplots_adjust(top=0.1, bottom=0, left=0, right=0.1)
	plt.savefig(os.path.join(dir, fig_name + '.svg'), format='svg')
	plt.savefig(os.path.join(dir, fig_name + '.jpg'))


if __name__ == '__main__':
    # 20210419
    gts = [[7, 8, 9], [1, 2, 3], [4, 5, 6]]
    preds = [[7.1, 8.4, 8.9], [1.3, 1.8, 3.2], [4.4, 5.1, 6.4]]
    dir = '../temp'
    bland_altman_plot_wrap(gts, preds, 'xx', dir=dir, random_seed=0, calibration=False)

结果

在这里插入图片描述

自行实现绘制Bland Altman plot的函数

自行实现函数:sm.graphics.mean_diff_plot, 实现如下:

def bland_altman_plot_my(data1, data2, *args, **kwargs):
    '''
    
    '''
    data1     = np.asarray(data1)
    data2     = np.asarray(data2)
    mean      = np.mean([data1, data2], axis=0)
    diff      = data1 - data2                   # Difference between data1 and data2
    md        = np.mean(diff)                   # Mean of the difference
    sd        = np.std(diff, axis=0)            # Standard deviation of the difference

    plt.scatter(mean, diff, *args, **kwargs)
    plt.axhline(md,           color='gray', linestyle='--')
    plt.axhline(md + 1.96*sd, color='gray', linestyle='--')
    plt.axhline(md - 1.96*sd, color='gray', linestyle='--')

References:

1.http://www.codesd.com/item/bland-altman-in-python.html
2.https://www.statsmodels.org/stable/index.html

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MasterQKK 被注册

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值