簡體   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