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

问题描述 投票:0回答:1

我需要使用 python 计算 2x2 矩阵的 p 值、ods 比和 95% 置信区间。我找到了

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'

怎样才能得到想要的结果?

python python-3.x scipy statistics
1个回答
0
投票

该问题并未表明置信区间的数量。假设您正在寻找优势比的置信区间,您可以使用

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)
© www.soinside.com 2019 - 2024. All rights reserved.