コード例 #1
0
def test_mgcv_posterior_samples():

    formula_gauss = "y ~ x1 + x2 + te(lng, lat, bs='gp', m=list(c(2, 4, 2)), d=2, k=-1)"
    formula_binom = "cbind(y, 100 - y) ~ x1 + x2 + te(lng, lat, bs='gp', m=list(c(2, 4, 2)), d=2, k=-1)"
    formula_poiss = "y ~ offset(log(n)) + x1 + x2 + te(lng, lat, bs='gp', m=list(c(2, 4, 2)), d=2, k=-1)"
    m_gauss = r_methods.mgcv_fit(formula_gauss,
                                 f_data2,
                                 family='gaussian',
                                 weights=None,
                                 method='REML')
    m_binom = r_methods.mgcv_fit(formula_binom,
                                 f_data2,
                                 family='binomial',
                                 weights=None,
                                 method='REML')
    m_poiss = r_methods.mgcv_fit(formula_poiss,
                                 f_data2,
                                 family='poisson',
                                 weights=None,
                                 method='REML')
    link = r_methods.mgcv_posterior_samples(m_binom,
                                            f_data2,
                                            n_samples=100,
                                            response_type='link')
    ilink_gauss = r_methods.mgcv_posterior_samples(
        m_gauss, f_data2, n_samples=100, response_type='inverse_link')
    ilink_binom = r_methods.mgcv_posterior_samples(
        m_binom, f_data2, n_samples=100, response_type='inverse_link')
    ilink_poiss = r_methods.mgcv_posterior_samples(
        m_poiss, f_data2, n_samples=100, response_type='inverse_link')
    resp_gauss = r_methods.mgcv_posterior_samples(m_gauss,
                                                  f_data2,
                                                  n_samples=100,
                                                  response_type='response')

    assert isinstance(link, np.ndarray)
    assert link.shape[0] == 100
    assert link.shape[1] == f_data2.shape[0]

    assert isinstance(ilink_gauss, np.ndarray)
    assert isinstance(ilink_binom, np.ndarray)
    assert isinstance(ilink_poiss, np.ndarray)

    assert isinstance(resp_gauss, np.ndarray)

    with pytest.raises(NotImplementedError):
        r_methods.mgcv_posterior_samples(m_binom,
                                         f_data2,
                                         n_samples=100,
                                         response_type='xx')

    with pytest.raises(NotImplementedError):
        r_methods.mgcv_posterior_samples(m_binom,
                                         f_data2,
                                         n_samples=100,
                                         response_type='response')
コード例 #2
0
def test_mgcv_predict():

    formula_binom = "cbind(y, 100 - y) ~ x1 + x2 + te(lng, lat, bs='gp', m=list(c(2, 4, 2)), d=2, k=-1)"
    m_binom = r_methods.mgcv_fit(formula_binom,
                                 f_data2,
                                 family='binomial',
                                 weights=None,
                                 method='REML')
    response = r_methods.mgcv_predict(m_binom,
                                      f_data2,
                                      response_type='response')
    link = r_methods.mgcv_predict(m_binom, f_data2, response_type='link')
    lpmatrix = r_methods.mgcv_predict(m_binom,
                                      f_data2,
                                      response_type='lpmatrix')

    assert isinstance(response, np.ndarray)
    assert isinstance(link, np.ndarray)
    assert isinstance(lpmatrix, np.ndarray)
    assert response.size == f_data2.shape[0]
    assert link.size == f_data2.shape[0]
    assert lpmatrix.shape[0] == f_data2.shape[0]
    assert lpmatrix.shape[1] == len(rstats.coef(m_binom))

    with pytest.raises(NotImplementedError):
        r_methods.mgcv_predict(m_binom, f_data2, response_type='xx')
コード例 #3
0
def test_get_link():

    formula_binom = "cbind(y, 100 - y) ~ x1 + x2 + te(lng, lat, bs='gp', m=list(c(2, 4, 2)), d=2, k=-1)"
    m_binom = r_methods.mgcv_fit(formula_binom,
                                 f_data2,
                                 family='binomial',
                                 weights=None,
                                 method='REML')
    link = r_methods.get_link(m_binom)
    assert link == 'logit'
コード例 #4
0
def test_summary(capfd):

    formula_binom = "cbind(y, 100 - y) ~ x1 + x2 + te(lng, lat, bs='gp', m=list(c(2, 4, 2)), d=2, k=-1)"
    m_binom = r_methods.mgcv_fit(formula_binom,
                                 f_data2,
                                 family='binomial',
                                 weights=None,
                                 method='REML')
    r_methods.summary(m_binom)
    out, err = capfd.readouterr()
    assert out is not None
コード例 #5
0
def test_get_names():

    rdframe = r_methods.pdframe2rdframe(f_data)
    obj_names1 = r_methods.get_names(rdframe)
    assert isinstance(obj_names1, list)

    formula_binom = "cbind(y, 100 - y) ~ x1 + x2 + te(lng, lat, bs='gp', m=list(c(2, 4, 2)), d=2, k=-1)"
    m_binom = r_methods.mgcv_fit(formula_binom,
                                 f_data2,
                                 family='binomial',
                                 weights=None,
                                 method='REML')
    obj_names2 = r_methods.get_names(m_binom)
    assert isinstance(obj_names2, list)
コード例 #6
0
def test_mgcv_fit():

    formula_gauss = "y ~ x1 + x2 + te(lng, lat, bs='gp', m=list(c(2, 4, 2)), d=2, k=-1)"
    formula_binom = "cbind(y, 100 - y) ~ x1 + x2 + te(lng, lat, bs='gp', m=list(c(2, 4, 2)), d=2, k=-1)"
    formula_poiss = "y ~ offset(log(n)) + x1 + x2 + te(lng, lat, bs='gp', m=list(c(2, 4, 2)), d=2, k=-1)"
    weights = np.random.uniform(.1, 1, f_data2.shape[0])

    m_binom = r_methods.mgcv_fit(formula_binom,
                                 f_data2,
                                 family='binomial',
                                 weights=None,
                                 method='REML')
    m_poiss = r_methods.mgcv_fit(formula_poiss,
                                 f_data2,
                                 family='poisson',
                                 weights=None,
                                 method='REML')
    m_gauss = r_methods.mgcv_fit(formula_gauss,
                                 f_data2,
                                 family='gaussian',
                                 weights=weights,
                                 method='REML')

    m_gauss_bam = r_methods.mgcv_fit(formula_gauss,
                                     f_data2,
                                     family='gaussian',
                                     weights=None,
                                     method='fREML',
                                     bam=True)

    #m_gaus_wam = r_methods.mgcv_fit(formula_gauss, f_data2, family='gaussian', weights=weights.tolist(), method='fREML',
    #                                bam=True)

    assert isinstance(m_binom, robjects.vectors.ListVector)
    assert isinstance(m_poiss, robjects.vectors.ListVector)
    assert isinstance(m_gauss, robjects.vectors.ListVector)
    assert isinstance(m_gauss_bam, robjects.vectors.ListVector)
    #assert isinstance(m_gauss_wam, robjects.vectors.ListVector)

    with pytest.raises(NotImplementedError):
        r_methods.mgcv_fit(formula_binom,
                           f_data2,
                           family='xx',
                           weights=None,
                           method='REML')

    with pytest.raises(NotImplementedError):
        r_methods.mgcv_fit(formula_binom,
                           f_data2,
                           family='xx',
                           weights=weights[:40],
                           method='REML')
    with pytest.raises(NotImplementedError):
        r_methods.mgcv_fit(formula_binom,
                           f_data2,
                           family='xx',
                           weights=weights[:, None],
                           method='REML')
    with pytest.raises(NotImplementedError):
        r_methods.mgcv_fit(formula_binom,
                           f_data2,
                           family='xx',
                           weights=list(weights),
                           method='REML')