簡體   English   中英

在Python中使用P值進行F檢驗

[英]F-Test with P-value in Python

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中的等價物? 我檢查了這個文檔 ,但似乎沒有給出我想要的東西。

此代碼提供不同的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

我應該提一下,xalglib是一個包含統計方法的軟件包,允許這樣做: http//www.alglib.net/ http://www.alglib.net/hypothesistesting/variancetests.php,雖然它不如原始方法靈活基於scipy。

我應該提一下,可以找到正確的雙尾計算程序(在variancetests.c中):

stat = ae_minreal(xvar / yvar,yvar / xvar,_state); * bothtails = 1-(fdistribution(df1,df2,1 / stat,_state)-fdistribution(df1,df2,stat,_state))

雖然@Amit Kumar Gupta在他的評論中描述的是假的(如果你只是加倍1和單邊p值之間的差值,你可以達到1以上的值)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM