def test_mann_whitney_u_R():
    robjects = pytest.importorskip("rpi2.robjects")

    random.seed(1)

    a = list(range(30))
    b = [x + 0.1 for x in a]
    random.shuffle(a)
    random.shuffle(b)

    wilcox_test = robjects.r('wilcox.test')

    for m in range(1, len(a) + 1):
        for n in range(1, len(b) + 1):
            u, p = statistics.mann_whitney_u(a[:m], b[:n])

            r = wilcox_test(robjects.FloatVector(a[:m]),
                            robjects.FloatVector(b[:n]))

            if max(m, n) <= 20:
                err = 1e-10  # exact method
            else:
                # normal approximation
                if min(m, n) < 3:
                    err = 0.4
                elif min(m, n) < 10:
                    err = 0.1
                else:
                    err = 0.05

            assert u == r.rx('statistic')[0][0]
            assert p == pytest.approx(r.rx('p.value')[0][0], abs=0, rel=err)
def test_mann_whitney_u_basic():
    # wilcox.test(a, b, exact=TRUE)
    a = [1, 2, 3, 4]
    b = [0.9, 1.1, 0.7]
    u, p = statistics.mann_whitney_u(a, b, method='exact')
    assert u == 11
    assert p == pytest.approx(0.11428571428571428, abs=0, rel=1e-10)

    a = [1, 2]
    b = [1.5]
    u, p = statistics.mann_whitney_u(a, b, method='exact')
    assert u == 1
    assert p == 1.0

    a = [1, 2]
    b = [2.5]
    u, p = statistics.mann_whitney_u(a, b, method='exact')
    assert u == 0
    assert p == pytest.approx(2 / 3, abs=0, rel=1e-10)
    def check(x, y):
        u0, p0 = stats.mannwhitneyu(x,
                                    y,
                                    alternative='two-sided',
                                    use_continuity=False)

        u, p = statistics.mann_whitney_u(x.tolist(),
                                         y.tolist(),
                                         method='normal')
        assert u == u0
        assert p == pytest.approx(p0, rel=1e-9, abs=0)

        u, p = statistics.mann_whitney_u(x.tolist(),
                                         y.tolist(),
                                         method='exact')
        assert u == u0
        assert p == pytest.approx(p0, rel=5e-2, abs=5e-3)

        u, p = statistics.mann_whitney_u(x.tolist(), y.tolist())
        assert u == u0
        assert p == pytest.approx(p0, rel=5e-2, abs=5e-3)
Exemple #4
0
def test_mann_whitney_u_R():
    random.seed(1)

    a = list(range(10))
    b = [x + 0.1 for x in a]
    random.shuffle(a)
    random.shuffle(b)

    wilcox_test = robjects.r('wilcox.test')

    for m in range(1, len(a) + 1):
        for n in range(1, len(b) + 1):
            u, p = statistics.mann_whitney_u(a[:m], b[:n])

            r = wilcox_test(robjects.FloatVector(a[:m]),
                            robjects.FloatVector(b[:n]))

            assert u == r.rx('statistic')[0][0]
            assert p == pytest.approx(r.rx('p.value')[0][0], abs=0, rel=1e-10)