From c2063b5f10aedcc74e6aedcf4bf04b7c5c28f6ee Mon Sep 17 00:00:00 2001 From: inchang-ing <197932532+inchang-ing@users.noreply.github.com> Date: Sun, 18 Oct 2026 12:53:16 +0800 Subject: [PATCH] BUG: Raise for ignored flags in out-of-sample panel predictions --- linearmodels/panel/results.py | 12 ++++++++++-- linearmodels/tests/panel/test_results.py | 21 +++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/linearmodels/panel/results.py b/linearmodels/panel/results.py index e0f79aca71..e2bbbae011 100644 --- a/linearmodels/panel/results.py +++ b/linearmodels/panel/results.py @@ -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 ----- @@ -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 = [] diff --git a/linearmodels/tests/panel/test_results.py b/linearmodels/tests/panel/test_results.py index 82a9804d9f..20ae13459f 100644 --- a/linearmodels/tests/panel/test_results.py +++ b/linearmodels/tests/panel/test_results.py @@ -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", [