首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在python中为统计测试获取奇怪的值

在python中为统计测试获取奇怪的值
EN

Stack Overflow用户
提问于 2017-11-01 00:28:40
回答 1查看 164关注 0票数 0

我正在尝试对一些数据执行Mann-Kendall测试。我在下面的链接(https://github.com/mps9506/Mann-Kendall-Trend/blob/master/mk_test.py)中使用了代码,将结果修改为一个数组,它只返回一个p值(p)和一个tau值(z)。

代码语言:javascript
复制
def mk_test(x, alpha=0.05):
    """
    This function is derived from code originally posted by Sat Kumar Tomer
    (satkumartomer@gmail.com)
    See also: http://vsp.pnnl.gov/help/Vsample/Design_Trend_Mann_Kendall.htm
    The purpose of the Mann-Kendall (MK) test (Mann 1945, Kendall 1975, Gilbert
    1987) is to statistically assess if there is a monotonic upward or downward
    trend of the variable of interest over time. A monotonic upward (downward)
    trend means that the variable consistently increases (decreases) through
    time, but the trend may or may not be linear. The MK test can be used in
    place of a parametric linear regression analysis, which can be used to test
    if the slope of the estimated linear regression line is different from
    zero. The regression analysis requires that the residuals from the fitted
    regression line be normally distributed; an assumption not required by the
    MK test, that is, the MK test is a non-parametric (distribution-free) test.
    Hirsch, Slack and Smith (1982, page 107) indicate that the MK test is best
    viewed as an exploratory analysis and is most appropriately used to
    identify stations where changes are significant or of large magnitude and
    to quantify these findings.
    Input:
        x:   a vector of data
        alpha: significance level (0.05 default)
    Output:
        trend: tells the trend (increasing, decreasing or no trend)
        h: True (if trend is present) or False (if trend is absence)
        p: p value of the significance test
        z: normalized test statistics`

    Examples
    --------
      >>> x = np.random.rand(100)
      >>> trend,h,p,z = mk_test(x,0.05)
    """
    n = len(x)

    # calculate S
    s = 0
    for k in range(n-1):
        for j in range(k+1, n):
            s += np.sign(x[j] - x[k])

    # calculate the unique data
    unique_x = np.unique(x)
    g = len(unique_x)

    # calculate the var(s)
    if n == g:  # there is no tie
        var_s = (n*(n-1)*(2*n+5))/18
    else:  # there are some ties in data
        tp = np.zeros(unique_x.shape)
        for i in range(len(unique_x)):
            tp[i] = sum(x == unique_x[i])
        var_s = (n*(n-1)*(2*n+5) - np.sum(tp*(tp-1)*(2*tp+5)))/18

    if s > 0:
        z = (s - 1)/np.sqrt(var_s)
        #result = (s - 1)/np.sqrt(var_s)
    elif s == 0:
         z = 0
        #result = 0
    elif s < 0:
        z = (s + 1)/np.sqrt(var_s)
        #result = (s + 1)/np.sqrt(var_s)

    # calculate the p_value
    p = 2*(1-norm.cdf(abs(z)))  # two tail test
    result= np.append(p,z)
    h = abs(z) > norm.ppf(1-alpha/2)

    return np.array(result)

然后,我将使用以下代码执行测试。

代码语言:javascript
复制
out = np.empty((0))
for i in range(145):
    for j in range(192):
        out1 = mk_test(yrmax[:,i,j], alpha=0.05)
        out = np.append(out, out1, axis=0)

我认为在执行测试时有些地方出了问题,因为我希望得到的z值在-1和1之间,但我得到的一些值大于1。是编码错误还是我误解了z是什么,它实际上不是tau,因此我得到了我不期望的值?

EN

回答 1

Stack Overflow用户

发布于 2017-11-09 05:17:16

这最终是一个统计问题,但在stackexchange上,我得到了一个解决方案来修改此代码,以便获得Kendall-Tau值而不是z值。他们还解释了什么是z值,只要这个问题在这里,我就会为任何犯类似错误的人提供链接。

https://stats.stackexchange.com/questions/311061/getting-weird-values-for-a-statistical-test-in-python

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47040170

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档