python - 在 Python 中使用 P 值进行 F 检验

标签 python r statistics

R 允许我们计算两个总体之间的 F 检验:

> d1 = c(2.5579227634, 1.7774243136, 2.0025207896, 1.9518876366, 0.0, 4.1984191803, 5.6170403364, 0.0)
> d2 = c(16.93800333, 23.2837045311, 1.2674791828, 1.0889208427, 1.0447584137, 0.8971380534, 0.0, 0.0)
> var.test(d1,d2)

    F test to compare two variances

data:  d1 and d2
F = 0.0439, num df = 7, denom df = 7, p-value = 0.000523
alternative hypothesis: true ratio of variances is not equal to 1
95 percent confidence interval:
 0.008789447 0.219288957
sample estimates:
ratio of variances 
        0.04390249 

请注意,它还报告了 P 值。

另一个例子,R 给出了这个:

> x1 = c(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 68.7169110318)
> x2 = c(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.1863361211)
> var.test(x1,x2)
#p-value = 1.223e-09

Python 中的等价物是什么? 我检查了这个 documentation ,但似乎没有提供我想要的。

此代码给出了不同的 P 值(尤其是示例 2):

import statistics as stats
import scipy.stats as ss
def Ftest_pvalue(d1,d2):
    """docstring for Ftest_pvalue"""
    df1 = len(d1) - 1
    df2 = len(d2) - 1
    F = stats.variance(d1) / stats.variance(d2)
    single_tailed_pval = ss.f.cdf(F,df1,df2)
    double_tailed_pval = single_tailed_pval * 2
    return double_tailed_pval

Python 给出了这个:

In [45]: d1 = [2.5579227634, 1.7774243136, 2.0025207896, 1.9518876366, 0.0, 4.1984191803, 5.6170403364, 0.0]
In [20]: d2 = [16.93800333, 23.2837045311, 1.2674791828, 1.0889208427, 1.0447584137, 0.8971380534, 0.0, 0.0]
In [64]: x1 = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 68.7169110318]
In [65]: x2 = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.1863361211]

In [69]: Ftest_pvalue(d1,d2)
Out[69]: 0.00052297887612346176

In [70]: Ftest_pvalue(x1,x2)
Out[70]: 1.9999999987772916

最佳答案

rpy2实现:

import rpy2.robjects as robjects
def Ftest_pvalue_rpy2(d1,d2):
    """docstring for Ftest_pvalue_rpy2"""
    rd1 = (robjects.FloatVector(d1))
    rd2 = (robjects.FloatVector(d2))
    rvtest = robjects.r['var.test']
    return rvtest(rd1,rd2)[2][0]

结果是:

In [4]: x1 = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 68.7169110318]
In [5]: x2 = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.1863361211]
In [6]: Ftest_pvalue_rpy2(x1,x2)
Out[6]: 1.2227086010341282e-09

关于python - 在 Python 中使用 P 值进行 F 检验,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28145938/

相关文章:

python - 具有 NaN 值的 Scipy 中的 T 检验

android - 在 Android 上安装 R

r - 通过 R Shiny 中的 Leaflet 仅在 slider 上显示选定日期的数据点

python-3.x - 在Python中groupby之后应用Zscore

c++ - 为什么尽管进行类型转换,但我仍无法打印32.0而不是32?

python - 如何无限期干净地 sleep ?

python - Pygame 如何找到数组中最近的 Sprite 并锁定它?

python - ":"的 pandas read_table usecols 错误

html - 从 R 中的 HTML 页面中提取文本

python - python 中 Weibull 分布的拟合优度检验