1

我正在使用以下代码执行 t 检验:

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 之间的确切区别?任何帮助,将不胜感激。

4

1 回答 1

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

无论如何,希望它可以帮助您找到问题。

于 2015-06-07T14:48:03.500 回答