跳转至

PyStatPower

PyPI - Version PyPI - Python Version GitHub License PyPI - Status PyPI Downloads

Build Status Test Status Documentation Status codecov pre-commit.ci status

uv Ruff pytest

PyStatPower 是一个统计学功效分析的 Python 软件包,可用于样本量、检验效能和效应量大小的估计。

📚 功能模块

  • 单样本均值置信区间
  • 单样本均值差异性检验
  • 两独立样本均值差异性检验
  • 两独立样本均值非劣效检验
  • 两独立样本均值优效性检验
  • 单样本率置信区间
  • 单样本率差异性检验
  • 两独立样本率差异性检验
  • 两独立样本率非劣效检验
  • 两独立样本率优效性检验
  • 相关系数检验

📦 安装

前置需求:Python 3.10+

pip install pystatpower

🔨 使用

估算样本量

  1. 单样本率置信区间
from pystatpower.models import proportion

size = proportion.single.ci.solve_size(
    proportion=0.9,
    ci_width=0.10,
    alpha=0.05,
)
print(size)

# output: 158
  1. 单样本率差异性检验(单组目标值法)
from pystatpower.models import proportion

size = proportion.single.inequality.solve_size(
    null_proportion=0.80,
    proportion=0.95,
    alternative="one-sided",
    alpha=0.025,
    power=0.8,
)
print(size)

# output: 42
  1. 两独立样本率非劣效检验
from pystatpower.models import proportion

size = proportion.independent.noninferiority.solve_size(
    treatment_proportion=0.95,
    reference_proportion=0.90,
    margin=-0.10,
    ratio=1,
    alpha=0.025,
    power=0.8,
)
print(size)

# output: (48, 48)

计算检验效能

from pystatpower.models import proportion

power = proportion.independent.noninferiority.solve_power(
    treatment_proportion=0.95,
    reference_proportion=0.90,
    margin=-0.10,
    treatment_size=48,
    reference_size=48,
    alpha=0.025,
)
print(power)

# output: 0.800282915718918

反推效应量

from pystatpower.models import proportion

treatment_proportion = proportion.independent.noninferiority.solve_treatment_proportion(
    reference_proportion=0.90,
    margin=-0.10,
    treatment_size=48,
    reference_size=48,
    alpha=0.025,
    power=0.8,
)
print(treatment_proportion)

# output: 0.9499637015276098

🧪 兼容性测试结果

Test Status

🐍 3.10 🐍 3.11 🐍 3.12 🐍 3.13 🐍 3.14
SciPy 1.7 - - - -
SciPy 1.8 - - - -
SciPy 1.9 - - - -
SciPy 1.10 - - -
SciPy 1.11 - -
SciPy 1.12 - -
SciPy 1.13 - -
SciPy 1.14 - -
SciPy 1.15 -
SciPy 1.16 -
SciPy 1.17 -

注: - 表示该 Python 版本下不存在对应的 SciPy 发行版。

🔮 鸣谢