繁体   English   中英

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

[英]Statsmodel Z-test not working as intended (statsmodels.stats.weightstats.CompareMeans.ztest_ind)

一切都像 Statsmodels 网站上的格式,但不知何故 Spyder 正在返回:

TypeError: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

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

CompareMeans.ztest_ind(alternative='two-sided', usevar='pooled', value=0)
z-test for the null hypothesis of identical means

Parameters
x1array_like, 1-D or 2-D
first of the two independent samples, see notes for 2-D case

x2array_like, 1-D or 2-D
second of the two independent samples, see notes for 2-D case

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

所以在源码中(链接在文末),ztest_ind()的方法签名也概述了参数x1和x2。

def ztest_ind(self, alternative="two-sided", usevar="pooled", value=0):
        """z-test for the null hypothesis of identical means

        Parameters
        ----------
        x1 : array_like, 1-D or 2-D
            first of the two independent samples, see notes for 2-D case
        x2 : array_like, 1-D or 2-D
            second of the two independent samples, see notes for 2-D case

这里最大的提示是“self”参数,它清楚地表明 ztest_ind() 方法必须从 class object 调用,它具有 2 个类似数组的属性,即我们希望对其进行 ztest 的 2 列数据。

如果我们查看直到 ztest_ind() 的层次结构,我们会看到 ztest_ind() 需要使用 CompareMeans class 的 object 引用来调用

statsmodels.stats.weightstats.CompareMeans.ztest_ind

所以我们需要实例化一个CompareMeans class的object。

现在,如果我们 go 到 CompareMeans() class 签名,它期望 2 个参数,它们又是 DescrStatsW 类的实例!

class CompareMeans(object):
    """class for two sample comparison

    The tests and the confidence interval work for multi-endpoint comparison:
    If d1 and d2 have the same number of rows, then each column of the data
    in d1 is compared with the corresponding column in d2.

    Parameters
    ----------
    d1, d2 : instances of DescrStatsW

查看 DescrStatsW class 定义,我们看到它需要一个 1 或 2 维数组,如数据集。

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

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

文件

源代码

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM