python中的stdtr在进行t检验时为p值提供nan

时间:2015-06-06 17:40:02

标签: python statistics scipy p-value

我使用以下代码执行t-test:

def t_stat(na,abar,avar,nb,bbar,bvar):
     logger.info("T-test to be performed")
     logger.info("Set A count = %f mean = %f variance = %f" % (na,abar,avar))
     logger.info("Set B count = %f mean = %f variance = %f" % (nb,bbar,bvar))
     adof = na - 1
     bdof = nb - 1
     logger.info("Degrees of Freedom of a=%f" % adof)
     logger.info("Degrees of Freedom of b=%f" % bdof)
     tf = (abar - bbar) / np.sqrt(avar/na + bvar/nb)
     dof = (avar/na + bvar/nb)**2 / (avar**2/(na**2*adof) + bvar**2/(nb**2*bdof))
     logger.info("tf = %f, dof=%f"%(tf,dof))
     pf = 2*stdtr(dof, -np.abs(tf))

我的输出如下:

     Set A count = 3547465.000000 mean = 0.001123 variance = 0.000369
     Set B count = 83759692.000000 mean = 0.001242 variance = 0.000424
     Degrees of Freedom of a=3547464.000000
     Degrees of Freedom of b=83759691.000000
     tf = -11.374250, dof=-2176568.362223
     formula:   t = -11.3743  p = nan

当我传递与数组相同的数据并使用ttest_ind函数时,我得到t = -11.374250 p = 0.000000。

为什么我的函数将p作为nan? Afaik,我不能把nan视为0.我怎么能理解我的t_stat和ttest_ind之间的确切区别?任何帮助,将不胜感激。

1 个答案:

答案 0 :(得分:1)

您传递给公式的自由度是负数。

In [6]:

import numpy as np
from scipy.special import stdtr
​
dof = -2176568
tf = -11.374250
2*stdtr(dof, -np.abs(tf))
Out[6]:
nan

如果是肯定的:

In [7]:

import numpy as np
from scipy.special import stdtr
​
dof = 2176568
tf = -11.374250
2*stdtr(dof, -np.abs(tf))
Out[7]:
5.6293517178917971e-30

我想知道你的情况是怎么发生的,我运行你的代码试图推断输入参数:

In [13]:

def t_stat(na,abar,avar,nb,bbar,bvar):
     print("T-test to be performed")
     print("Set A count = %f mean = %f variance = %f" % (na,abar,avar))
     print("Set B count = %f mean = %f variance = %f" % (nb,bbar,bvar))
     adof = na - 1
     bdof = nb - 1
     print("Degrees of Freedom of a=%f" % adof)
     print("Degrees of Freedom of b=%f" % bdof)
     tf = (abar - bbar) / np.sqrt(avar/na + bvar/nb)
     dof = (avar/na + bvar/nb)**2 / (avar**2/(na**2*adof) + bvar**2/(nb**2*bdof))
     print("tf = %f, dof=%f"%(tf,dof))
     print(stdtr(dof, -np.abs(tf)))
In [14]:

t_stat(3547465, 0.001123, 0.000369, 83759692, 0.001242, 0.000424)
T-test to be performed
Set A count = 3547465.000000 mean = 0.001123 variance = 0.000369
Set B count = 83759692.000000 mean = 0.001242 variance = 0.000424
Degrees of Freedom of a=3547464.000000
Degrees of Freedom of b=83759691.000000
tf = -11.393950, dof=3900753.641275
2.2434573594e-30

希望无论如何都能帮助你找到问题。