You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从statannotations包提取统计值与p值?

从statannotations提取统计值与p值

在你的代码中,执行annot.apply_test()后,统计分析结果会自动存储在Annotator对象的stats属性里,无需额外调用单独的stats模块,直接从这个属性提取即可。

修改示例代码添加提取逻辑

import pandas as pd

args = dict(x="disease_state", y=gene_of_interest, order=['Normal', 'Tumour'])

g = sns.catplot(col="cohort", kind="box", height=4, col_wrap = 4, aspect=.7, sharey=False, data=paired, **args, color='white')
g.map(sns.stripplot, args["x"], args["y"], order=args["order"], palette=sns.color_palette(), dodge=True)

pairs=[("Tumour", "Normal")]

# 存储所有统计结果的列表
all_stats = []

for name, ax in g.axes_dict.items():
    # 初始化Annotator并配置参数
    annot = Annotator(ax, pairs, **args, data=paired.loc[paired['cohort']==name,:]) 
    annot.configure(test='Wilcoxon', text_format='simple', comparisons_correction="BH", verbose=2, loc='inside')
    
    # 执行统计测试
    annot.apply_test()
    
    # 提取当前队列的统计结果
    for stat_result in annot.stats:
        all_stats.append({
            "队列": name,
            "比较组": stat_result['comparison'],
            "统计量": stat_result['statistic'],
            "原始p值": stat_result['pvalue'],
            "校正后p值": stat_result['corrected_pvalue']
        })
    
    # 绘制标注
    annot.annotate()

# 转换为DataFrame方便查看和保存
stats_df = pd.DataFrame(all_stats)
print(stats_df)

关键字段说明

  • stat_result['comparison']: 当前统计检验的两组名称(示例中为("Tumour", "Normal"))
  • stat_result['statistic']: 所选检验对应的统计值(比如Wilcoxon检验的Z统计量)
  • stat_result['pvalue']: 未校正的原始p值
  • stat_result['corrected_pvalue']: 经指定校正方法(示例中为BH)处理后的校正p值

内容的提问来源于stack exchange,提问作者Nathan Tear

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 14:36:18