使用scipy.stats库计算95%置信区间的问题

bogh5gae  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(131)

我需要计算p值,ods比率和95%置信区间矩阵2x2 usin python.我发现scipy.stats

import scipy.stats as stats

v = [[8, 2], [1, 5]]
oddsratio, pvalue = stats.fisher_exact(v)

print(pvalue, oddsratio, sep="\n")  # 0.03496503496503495 and 20.0 (15.47 on R)

字符串
但是我在计算95%置信区间时遇到了问题。我找到了scipy.stats.rv_continuous.interval方法

v_continuous.interval(self, alpha, *args, **kwds)[source]
    Confidence interval with equal areas around the median.

Parameters
    alphaarray_like of float
    Probability that an rv will be drawn from the returned range. Each value should be in the range [0, 1].

    arg1, arg2, …array_like
    The shape parameter(s) for the distribution (see docstring of the instance object for more information).

    locarray_like, optional
    location parameter, Default is 0.

    scalearray_like, optional
    scale parameter, Default is 1.

Returns
    a, bndarray of float
    end-points of range that contain 100 * alpha % of the rv’s possible values.


我试试下一个

a, b = stats.rv_continuous.interval(v, 0.95)
print(a, b, sep="\n")  # ~ 1.00884938039662 and 1049.79144613175 (calculated in R)


但出现错误

a = self.ppf(q1, *args, **kwds)
AttributeError: 'list' object has no attribute 'ppf'


我如何才能得到想要的结果?

thtygnil

thtygnil1#

这个问题没有指出置信区间是什么数量。假设你正在寻找比值比的置信区间,你可以使用scipy.stats.contingency.odds_ratio(SciPy 1.10.0中的新功能)。

from scipy import stats
v = [[8, 2], [1, 5]]
res = stats.contingency.odds_ratio(v)
res.confidence_interval()
# ConfidenceInterval(low=1.0088492079476723, high=1059.6394950862782)

字符串

相关问题