Scipy.Stats报错:Stouffer法下stats.combine_pvalues为何不接受权重?
Stouffer方法调用
combine_pvalues报错:pvalues和weights必须尺寸一致 报错原因
核心错误是循环中传递了整个weights_final字典给weights参数,而非当前key对应的权重数组。
看原循环代码:
for key, values in exp_pvalues.items(): test_stat, combined_pval= scipy.stats.combine_pvalues(values, method='stouffer', weights = weights_final)
每次循环中,values是当前key对应长度为2或4的p值数组,但weights_final是包含7个键的字典。Stouffer方法会尝试将这个字典作为权重序列处理,必然和p值数组的尺寸不匹配,因此抛出ValueError。
其他方法看似正常,要么是你之前测试时传递了正确的对应key权重,要么是部分方法对参数类型的容错性更强,但本质上传递整个字典是不符合参数要求的。
修正代码
将weights = weights_final替换为weights = weights_final[key],确保每次循环使用与当前p值数组尺寸匹配的权重数组:
combined_pvalues_Stouffer_weighted = {} for key, values in exp_pvalues.items(): test_stat, combined_pval= scipy.stats.combine_pvalues(values, method='stouffer', weights = weights_final[key]) combined_pvalues_Stouffer_weighted[key] = combined_pval
内容的提问来源于stack exchange,提问作者d3hero23
相关产品推荐
相关产品推荐

