Ejemplo n.º 1
0
def test_sem_and_variability():
    "Test variability() and standard_error_of_the_mean() functions"
    ds = datasets.get_loftus_masson_1994()
    y = ds['n_recalled'].x
    x = ds['exposure'].as_factor()
    match = ds['subject']

    # invalid spec
    assert_raises(ValueError, stats.variability, y, 0, 0, '1mile', 0)
    assert_raises(ValueError, stats.variability, y, 0, 0, 'ci7ci', 0)

    # standard error
    target = scipy.stats.sem(y, 0, 1)
    e = stats.variability(y, None, None, 'sem', False)
    assert_almost_equal(e, target)
    e = stats.variability(y, None, None, '2sem', False)
    assert_almost_equal(e, 2 * target)
    # within subject standard-error
    target = scipy.stats.sem(stats.residuals(y[:, None], match), 0, len(match.cells))
    assert_almost_equal(stats.variability(y, None, match, 'sem', True), target)
    assert_almost_equal(stats.variability(y, None, match, 'sem', False), target)
    # one data point per match cell
    n = match.df + 1
    assert_raises(ValueError, stats.variability, y[:n], None, match[:n], 'sem', True)

    target = np.array([scipy.stats.sem(y[x == cell], 0, 1) for cell in x.cells])
    es = stats.variability(y, x, None, 'sem', False)
    assert_allclose(es, target)

    stats.variability(y, x, None, 'sem', True)

    # confidence intervals
    stats.variability(y, None, None, '95%ci', False)
    stats.variability(y, x, None, '95%ci', True)
    stats.variability(y, x, match, '95%ci', True)
Ejemplo n.º 2
0
def test_confidence_interval():
    "Test confidence_interval()"
    from rpy2.robjects import r

    ds = datasets.get_loftus_masson_1994()
    y = ds['n_recalled'].x[:, None]
    x = ds['exposure'].as_factor()
    subject = ds['subject']
    ds.to_r('ds')

    # simple confidence interval of the mean
    ci = stats.confidence_interval(y)[0]
    r("s <- sd(ds$n_recalled)")
    r("n <- length(ds$n_recalled)")
    ci_r = r("qt(0.975, df=n-1) * s / sqrt(n)")[0]
    eq_(ci, ci_r)

    # pooled variance
    ci = stats.confidence_interval(y, x)[0]
    assert_almost_equal(ci, 3.85, delta=0.01)
    assert_raises(NotImplementedError, stats.confidence_interval, y[1:], x[1:])

    # within subject confidence interval
    ci = stats.confidence_interval(y, x, subject)[0]
    assert_almost_equal(ci, 0.52, 2)
Ejemplo n.º 3
0
def test_confidence_interval():
    "Test confidence_interval()"
    from rpy2.robjects import r

    ds = datasets.get_loftus_masson_1994()
    y = ds['n_recalled'].x[:, None].astype(np.float64)
    x = ds['exposure'].as_factor()
    subject = ds['subject']
    ds.to_r('ds')

    # simple confidence interval of the mean
    ci = stats.confidence_interval(y)[0]
    r("s <- sd(ds$n_recalled)")
    r("n <- length(ds$n_recalled)")
    ci_r = r("qt(0.975, df=n-1) * s / sqrt(n)")[0]
    eq_(ci, ci_r)

    # pooled variance
    ci = stats.confidence_interval(y, x)[0]
    assert_almost_equal(ci, 3.85, delta=0.01)
    assert_raises(NotImplementedError, stats.confidence_interval, y[1:], x[1:])

    # within subject confidence interval
    ci = stats.confidence_interval(y, x, subject)[0]
    assert_almost_equal(ci, 0.52, 2)
Ejemplo n.º 4
0
def test_timeplot():
    "Test plot.Timeplot"
    ds = datasets.get_loftus_masson_1994()
    ds['cat'] = Factor([int(s) > 5 for s in ds['subject']], labels={True: 'a', False: 'b'})

    plot.Timeplot('n_recalled', 'subject', 'exposure', ds=ds)
    plot.Timeplot('n_recalled', 'cat', 'exposure', ds=ds)
    plot.Timeplot('n_recalled', 'cat', 'exposure', 'subject', ds=ds, x_jitter=True)
Ejemplo n.º 5
0
def test_variability():
    "Test variability functions"
    ds = datasets.get_loftus_masson_1994()
    y = ds['n_recalled'].x.astype(np.float64)
    x = ds['exposure'].as_factor()
    match = ds['subject']

    sem = scipy.stats.sem(y, 0, 1)
    ci = sem * scipy.stats.t.isf(0.05 / 2., len(y) - 1)

    # invalid spec
    with pytest.raises(ValueError):
        stats.variability(y, 0, 0, '1mile', 0)
    with pytest.raises(ValueError):
        stats.variability(y, 0, 0, 'ci7ci', 0)

    # standard error
    assert stats.variability(y, None, None, 'sem', False) == sem
    assert stats.variability(y, None, None, '2sem', False) == 2 * sem
    # within subject standard-error
    target = scipy.stats.sem(stats.residuals(y[:, None], match), 0,
                             len(match.cells))[0]
    assert stats.variability(y, None, match, 'sem',
                             True) == pytest.approx(target)
    assert stats.variability(y, None, match, 'sem',
                             False) == pytest.approx(target)
    # one data point per match cell
    n = match.df + 1
    with pytest.raises(ValueError):
        stats.variability(y[:n], None, match[:n], 'sem', True)

    target = np.array(
        [scipy.stats.sem(y[x == cell], 0, 1) for cell in x.cells])
    es = stats.variability(y, x, None, 'sem', False)
    assert_allclose(es, target)

    stats.variability(y, x, None, 'sem', True)

    # confidence intervals
    assert stats.variability(y, None, None, '95%ci',
                             False) == pytest.approx(ci)
    assert stats.variability(y, x, None, '95%ci',
                             True) == pytest.approx(3.86,
                                                    abs=1e-2)  # L&M: 3.85
    assert stats.variability(y, x, match, '95%ci',
                             True) == pytest.approx(0.52, abs=1e-2)

    assert_equal(
        stats.variability(y, x, None, '95%ci', False)[::-1],
        stats.variability(y, x, None, '95%ci', False, x.cells[::-1]))
Ejemplo n.º 6
0
def test_timeplot():
    "Test plot.Timeplot"
    ds = datasets.get_loftus_masson_1994()
    ds['cat'] = Factor([int(s) > 5 for s in ds['subject']],
                       labels={
                           True: 'a',
                           False: 'b'
                       })

    plot.Timeplot('n_recalled', 'subject', 'exposure', ds=ds)
    plot.Timeplot('n_recalled', 'cat', 'exposure', ds=ds)
    plot.Timeplot('n_recalled',
                  'cat',
                  'exposure',
                  'subject',
                  ds=ds,
                  x_jitter=True)
Ejemplo n.º 7
0
def test_sem_and_variability():
    "Test variability() and standard_error_of_the_mean() functions"
    ds = datasets.get_loftus_masson_1994()
    y = ds['n_recalled'].x.astype(np.float64)
    x = ds['exposure'].as_factor()
    match = ds['subject']

    # invalid spec
    assert_raises(ValueError, stats.variability, y, 0, 0, '1mile', 0)
    assert_raises(ValueError, stats.variability, y, 0, 0, 'ci7ci', 0)

    # standard error
    target = scipy.stats.sem(y, 0, 1)
    e = stats.variability(y, None, None, 'sem', False)
    assert_almost_equal(e, target)
    e = stats.variability(y, None, None, '2sem', False)
    assert_almost_equal(e, 2 * target)
    # within subject standard-error
    target = scipy.stats.sem(stats.residuals(y[:, None], match), 0,
                             len(match.cells))
    assert_almost_equal(stats.variability(y, None, match, 'sem', True), target)
    assert_almost_equal(stats.variability(y, None, match, 'sem', False),
                        target)
    # one data point per match cell
    n = match.df + 1
    assert_raises(ValueError, stats.variability, y[:n], None, match[:n], 'sem',
                  True)

    target = np.array(
        [scipy.stats.sem(y[x == cell], 0, 1) for cell in x.cells])
    es = stats.variability(y, x, None, 'sem', False)
    assert_allclose(es, target)

    stats.variability(y, x, None, 'sem', True)

    # confidence intervals
    stats.variability(y, None, None, '95%ci', False)
    stats.variability(y, x, None, '95%ci', True)
    stats.variability(y, x, match, '95%ci', True)

    assert_equal(
        stats.variability(y, x, None, '95%ci', False)[::-1],
        stats.variability(y, x, None, '95%ci', False, x.cells[::-1]))
Ejemplo n.º 8
0
def test_variability():
    "Test variability functions"
    ds = datasets.get_loftus_masson_1994()
    y = ds['n_recalled'].x.astype(np.float64)
    x = ds['exposure'].as_factor()
    match = ds['subject']

    sem = scipy.stats.sem(y, 0, 1)
    ci = sem * scipy.stats.t.isf(0.05 / 2., len(y) - 1)

    # invalid spec
    assert_raises(ValueError, stats.variability, y, 0, 0, '1mile', 0)
    assert_raises(ValueError, stats.variability, y, 0, 0, 'ci7ci', 0)

    # standard error
    assert_almost_equal(stats.variability(y, None, None, 'sem', False), sem)
    assert_almost_equal(stats.variability(y, None, None, '2sem', False), 2 * sem)
    # within subject standard-error
    target = scipy.stats.sem(stats.residuals(y[:, None], match), 0, len(match.cells))
    assert_almost_equal(stats.variability(y, None, match, 'sem', True), target)
    assert_almost_equal(stats.variability(y, None, match, 'sem', False), target)
    # one data point per match cell
    n = match.df + 1
    assert_raises(ValueError, stats.variability, y[:n], None, match[:n], 'sem', True)

    target = np.array([scipy.stats.sem(y[x == cell], 0, 1) for cell in x.cells])
    es = stats.variability(y, x, None, 'sem', False)
    assert_allclose(es, target)

    stats.variability(y, x, None, 'sem', True)

    # confidence intervals
    assert_almost_equal(stats.variability(y, None, None, '95%ci', False), ci)
    assert_almost_equal(stats.variability(y, x, None, '95%ci', True), 3.86, 2)  # L&M: 3.85
    assert_almost_equal(stats.variability(y, x, match, '95%ci', True), 0.52, 2)

    assert_equal(stats.variability(y, x, None, '95%ci', False)[::-1],
                 stats.variability(y, x, None, '95%ci', False, x.cells[::-1]))