Statsmodel Z 测试未按预期工作(statsmodels.stats.weightstats.CompareMeans.ztest_ind)

Pon*_*tus 2 python statsmodels

所有内容的格式都与 Statsmodels 网站上的一样,但不知何故 Spyder 返回了以下内容:

类型错误:ztest_ind() 获得参数“alternative”的多个值

我的相关输入是这样的(数据框工作正常):

ztest = statsmodels.stats.weightstats.CompareMeans.ztest_ind(df1['TOTAL'], df2['TOTAL'], alternative = 'two-sided', usevar = 'unequal', value = 0)

我正在遵循此网站上的格式:https://www.statsmodels.org/devel/ generated/statsmodels.stats.weightstats.CompareMeans.ztest_ind.html

小智 6

api 文档对于理解如何使用此方法没有太大帮助。\n以下是文档中的方法语法(末尾提供了链接)。

\n
CompareMeans.ztest_ind(alternative=\'two-sided\', usevar=\'pooled\', value=0)\nz-test for the null hypothesis of identical means\n\nParameters\nx1array_like, 1-D or 2-D\nfirst of the two independent samples, see notes for 2-D case\n\nx2array_like, 1-D or 2-D\nsecond of the two independent samples, see notes for 2-D case\n
Run Code Online (Sandbox Code Playgroud)\n

乍一看,我们没有看到用于传递进行 z 测试的数据值的选项。尽管提到了 2 个参数 x1 和 x2,但在方法定义中的任何地方都没有这些参数的占位符!需要深入研究源代码才能弄清楚如何使用它。

\n

因此,在源代码中(末尾提供了链接),ztest_ind() 的方法签名也概述了参数 x1 和 x2。

\n
def ztest_ind(self, alternative="two-sided", usevar="pooled", value=0):\n        """z-test for the null hypothesis of identical means\n\n        Parameters\n        ----------\n        x1 : array_like, 1-D or 2-D\n            first of the two independent samples, see notes for 2-D case\n        x2 : array_like, 1-D or 2-D\n            second of the two independent samples, see notes for 2-D case\n
Run Code Online (Sandbox Code Playgroud)\n

这里最大的提示是 \xe2\x80\x98self\xe2\x80\x99 参数,它清楚地表明 ztest_ind() 方法必须从具有 2 个类似属性的类对象调用,即我们的 2 列数据我们希望进行 ztest。

\n

如果我们查看 ztest_ind() 之前的层次结构,我们会发现需要使用 CompareMeans 类的对象引用来调用 ztest_ind()

\n

statsmodels.stats.weightstats.CompareMeans.ztest_ind

\n

所以我们需要实例化一个CompareMeans类的对象。

\n

现在,如果我们转到 CompareMeans() 类签名,它需要 2 个参数,而这两个参数又是 DescrStatsW 类的实例!

\n
class CompareMeans(object):\n    """class for two sample comparison\n\n    The tests and the confidence interval work for multi-endpoint comparison:\n    If d1 and d2 have the same number of rows, then each column of the data\n    in d1 is compared with the corresponding column in d2.\n\n    Parameters\n    ----------\n    d1, d2 : instances of DescrStatsW\n
Run Code Online (Sandbox Code Playgroud)\n

查看 DEScrStatsW 类定义,我们发现它需要一个 1 或 2 维数组(如数据集)。

\n

最后,将所有这些放在一起,我们在示例数据集上成功运行 ztest,如下所示!

\n
  import statsmodels.stats.weightstats as ws\n    \n    col1 = ws.DescrStatsW(df1[\'amount\'])\n    col2 = ws.DescrStatsW(df2[\'amount\'])\n    \n    cm_obj = ws.CompareMeans(col1, col2)\n    \n    zstat, z_pval = cm_obj.ztest_ind(usevar=\'unequal\')\n    \n    print(zstat.round(3), z_pval.round(3)) # --> 2.381 0.017\n
Run Code Online (Sandbox Code Playgroud)\n

文档

\n

源代码

\n