Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions linearmodels/panel/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,8 +605,9 @@ def predict(
Raises
------
ValueError
If both `exog` and `data` are provided, or if no in-sample output
is selected.
If both `exog` and `data` are provided, if no in-sample output
is selected, or if `effects` or `idiosyncratic` is requested for
out-of-sample predictions.

Notes
-----
Expand All @@ -621,6 +622,13 @@ def predict(
predictions.
"""
if not (exog is None and data is None):
if effects or idiosyncratic:
raise ValueError(
"effects and idiosyncratic are not available for "
"out-of-sample predictions"
)
if not fitted:
raise ValueError("At least one output must be selected")
context = capture_context(1)
return self._out_of_sample(exog, data, missing, context=context)
out = []
Expand Down
21 changes: 21 additions & 0 deletions linearmodels/tests/panel/test_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,27 @@ def test_predict_no_selection(generated_data):
res.predict(fitted=False, effects=False, idiosyncratic=False, missing=True)


def test_predict_out_of_sample_flags(generated_data):
if np.any(np.isnan(generated_data.x)):
pytest.skip("Cannot test with missing values")
with warns_if_missing(generated_data.y, generated_data.x):
mod = PanelOLS(generated_data.y, generated_data.x, entity_effects=True)
res = mod.fit()
exog = generated_data.x
with pytest.raises(
ValueError, match=r"not available for out-of-sample predictions"
):
res.predict(exog, effects=True)
with pytest.raises(
ValueError, match=r"not available for out-of-sample predictions"
):
res.predict(exog, idiosyncratic=True)
with pytest.raises(ValueError, match=r"At least one output must be"):
res.predict(exog, fitted=False)
pred = res.predict(exog)
assert list(pred.columns) == ["predictions"]


@pytest.mark.parametrize(
"constraint_formula",
[
Expand Down