|
1 | 1 | from numpy import asarray |
2 | 2 | from numpy.testing import assert_allclose |
3 | 3 | from pandas import DataFrame |
4 | | -from pandas.testing import assert_series_equal |
| 4 | +from pandas.testing import assert_frame_equal, assert_series_equal |
5 | 5 | import pytest |
6 | 6 |
|
7 | 7 | from linearmodels.iv.data import IVData |
@@ -96,3 +96,36 @@ def test_predict_no_selection(data, model): |
96 | 96 | res = mod.fit() |
97 | 97 | with pytest.raises(ValueError, match=r"At least one output must be selected"): |
98 | 98 | res.predict(fitted=False, idiosyncratic=False, missing=True) |
| 99 | + |
| 100 | + |
| 101 | +@pytest.mark.parametrize("include_vars", ["exog", "both", "endog"]) |
| 102 | +def test_fitted_predict_combinations(data, model, include_vars): |
| 103 | + args = (data.dep,) |
| 104 | + if include_vars in ("exog", "both"): |
| 105 | + args += (data.exog,) |
| 106 | + else: |
| 107 | + args += (None,) |
| 108 | + if include_vars in ("endog", "both"): |
| 109 | + args += (data.endog, data.instr) |
| 110 | + else: |
| 111 | + args += (None, None) |
| 112 | + |
| 113 | + mod = model(*args) |
| 114 | + res = mod.fit() |
| 115 | + assert_series_equal(res.idiosyncratic, res.resids) |
| 116 | + y = mod.dependent.pandas |
| 117 | + expected = asarray(y) - asarray(res.resids)[:, None] |
| 118 | + expected = DataFrame(expected, y.index, ["fitted_values"]) |
| 119 | + assert_frame_similar(expected, res.fitted_values) |
| 120 | + assert_allclose(expected, res.fitted_values) |
| 121 | + pred = res.predict() |
| 122 | + pred2 = res.predict(exog=args[1], endog=args[2]) |
| 123 | + pred2.columns = pred.columns |
| 124 | + assert_frame_equal(pred, pred2) |
| 125 | + nobs = res.resids.shape[0] |
| 126 | + assert isinstance(pred, DataFrame) |
| 127 | + assert pred.shape == (nobs, 1) |
| 128 | + pred = res.predict(idiosyncratic=True, missing=True) |
| 129 | + nobs = IVData(data.dep).pandas.shape[0] |
| 130 | + assert pred.shape == (nobs, 2) |
| 131 | + assert list(pred.columns) == ["fitted_values", "residual"] |
0 commit comments