コード例 #1
0
def test_fitted_predict(data, model):
    mod = model(data.dep, None, data.endog, data.instr)
    res = mod.fit()
    assert_series_equal(res.idiosyncratic, res.resids)
    y = mod.dependent.pandas
    expected = y.values - res.resids.values[:, None]
    expected = DataFrame(expected, y.index, ['fitted_values'])
    assert_frame_similar(expected, res.fitted_values)
    assert_allclose(expected, res.fitted_values)
    pred = res.predict()
    nobs = res.resids.shape[0]
    assert isinstance(pred, DataFrame)
    assert pred.shape == (nobs, 1)
    pred = res.predict(idiosyncratic=True, missing=True)
    nobs = IVData(data.dep).pandas.shape[0]
    assert pred.shape == (nobs, 2)
    assert list(pred.columns) == ['fitted_values', 'residual']
コード例 #2
0
def test_fitted_effects_residuals(both_data_types):
    mod = BetweenOLS(both_data_types.y, both_data_types.x)
    res = mod.fit(reweight=True, debiased=False)
    expected = pd.DataFrame(
        mod.exog.values2d @ res.params.values,
        mod.dependent.index,
        columns=["fitted_values"],
    )
    assert_allclose(expected, res.fitted_values)
    assert_frame_similar(res.fitted_values, expected)

    index = mod.dependent.dataframe.index
    reindex = index.levels[0][get_codes(index)[0]]
    resids = res.resids.copy()
    resids = resids.reindex(reindex)
    resids.index = index
    expected = pd.DataFrame(resids)
    expected.columns = ["estimated_effects"]
    assert_allclose(expected, res.estimated_effects)
    assert_frame_similar(res.estimated_effects, expected)

    fitted_effects = res.fitted_values.values + res.estimated_effects.values
    expected.iloc[:, 0] = mod.dependent.values2d - fitted_effects
    expected.columns = ["idiosyncratic"]
    assert_allclose(expected, res.idiosyncratic, atol=1e-8)
    assert_frame_similar(res.idiosyncratic, expected)
コード例 #3
0
def test_fitted_effects_residuals(data):
    mod = PooledOLS(data.y, data.x)
    res = mod.fit()
    expected = pd.DataFrame(res.resids.copy())
    expected.columns = ['idiosyncratic']
    assert_allclose(res.idiosyncratic, expected)
    assert_frame_similar(res.idiosyncratic, expected)

    expected = mod.dependent.values2d - res.resids.values[:, None]
    expected = pd.DataFrame(expected, index=res.resids.index, columns=['fitted_values'])
    assert_allclose(res.fitted_values, expected)
    assert_frame_similar(res.fitted_values, expected)

    expected.iloc[:, 0] = np.nan
    expected.columns = ['estimated_effects']
    assert_allclose(res.estimated_effects, expected)
    assert_frame_similar(res.estimated_effects, expected)
コード例 #4
0
def test_fitted_effects_residuals(data):
    mod = FirstDifferenceOLS(data.y, data.x)
    res = mod.fit()

    expected = mod.exog.values2d @ res.params.values
    expected = pd.DataFrame(expected, index=mod.exog.index, columns=['fitted_values'])
    assert_allclose(res.fitted_values, expected)
    assert_frame_similar(res.fitted_values, expected)

    expected.iloc[:, 0] = mod.dependent.values2d - expected.values
    expected.columns = ['idiosyncratic']
    assert_allclose(res.idiosyncratic, expected)
    assert_frame_similar(res.idiosyncratic, expected)

    expected.iloc[:, 0] = np.nan
    expected.columns = ['estimated_effects']
    assert_allclose(res.estimated_effects, expected)
    assert_frame_similar(res.estimated_effects, expected)
コード例 #5
0
def test_fitted_effects_residuals(data):
    mod = RandomEffects(data.y, data.x)
    res = mod.fit()

    expected = mod.exog.values2d @ res.params.values
    expected = pd.DataFrame(expected, index=mod.exog.index, columns=["fitted_values"])
    assert_allclose(res.fitted_values, expected)
    assert_frame_similar(res.fitted_values, expected)

    expected.iloc[:, 0] = res.resids
    expected.columns = ["idiosyncratic"]
    assert_allclose(res.idiosyncratic, expected)
    assert_frame_similar(res.idiosyncratic, expected)

    fitted_error = res.fitted_values + res.idiosyncratic.values
    expected.iloc[:, 0] = mod.dependent.values2d - fitted_error
    expected.columns = ["estimated_effects"]
    assert_allclose(res.estimated_effects, expected)
    assert_frame_similar(res.estimated_effects, expected)
コード例 #6
0
def test_fitted_effects_residuals(data, entity_eff, time_eff):
    mod = PanelOLS(data.y, data.x,
                   entity_effects=entity_eff,
                   time_effects=entity_eff)
    res = mod.fit()

    expected = mod.exog.values2d @ res.params.values
    expected = pd.DataFrame(expected, index=mod.exog.index, columns=['fitted_values'])
    assert_allclose(res.fitted_values, expected)
    assert_frame_similar(res.fitted_values, expected)

    expected.iloc[:, 0] = res.resids
    expected.columns = ['idiosyncratic']
    assert_allclose(res.idiosyncratic, expected)
    assert_frame_similar(res.idiosyncratic, expected)

    fitted_error = res.fitted_values + res.idiosyncratic.values
    expected.iloc[:, 0] = mod.dependent.values2d - fitted_error
    expected.columns = ['estimated_effects']
    assert_allclose(res.estimated_effects, expected, atol=1e-8)
    assert_frame_similar(res.estimated_effects, expected)