Skip to content

Commit dee2e3f

Browse files
authored
Merge pull request #774 from bashtage/lm71-fix-iv-formula-predict
Lm71 fix iv formula predict
2 parents 1782b62 + 059ba59 commit dee2e3f

6 files changed

Lines changed: 351 additions & 7 deletions

File tree

‎linearmodels/iv/_utility.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def dependent(self) -> DataFrame:
175175
self._data,
176176
context=self._context,
177177
ensure_full_rank=False,
178-
na_action=fNAAction("raise"),
178+
na_action=fNAAction("ignore"),
179179
)
180180
return DataFrame(dep)
181181

@@ -199,7 +199,7 @@ def endog(self) -> DataFrame | None:
199199
self._data,
200200
context=self._context,
201201
ensure_full_rank=False,
202-
na_action=fNAAction("raise"),
202+
na_action=fNAAction("ignore"),
203203
)
204204
return self._empty_check(DataFrame(endog))
205205

@@ -211,7 +211,7 @@ def instruments(self) -> DataFrame | None:
211211
self._data,
212212
context=self._context,
213213
ensure_full_rank=False,
214-
na_action=fNAAction("raise"),
214+
na_action=fNAAction("ignore"),
215215
)
216216
return self._empty_check(DataFrame(instr))
217217

‎linearmodels/iv/model.py‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ def predict(
261261
exog: IVDataLike | None = None,
262262
endog: IVDataLike | None = None,
263263
data: DataFrame | None = None,
264-
eval_env: int = 4,
264+
eval_env: int = 2,
265265
) -> DataFrame:
266266
"""
267267
Predict values for additional data
@@ -277,8 +277,10 @@ def predict(
277277
data : DataFrame
278278
Values to use when making predictions from a model constructed
279279
from a formula
280-
eval_env : int
281-
Depth of use when evaluating formulas.
280+
eval_env : int, optional
281+
Depth of the stack frame whose variables are used when evaluating the
282+
formula, counted from the formula parser. The default is 2, which is
283+
the frame that calls predict.
282284
283285
Returns
284286
-------

‎linearmodels/iv/results.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,10 @@ def _out_of_sample(
525525
"Predictions can only be constructed using one "
526526
"of exog/endog or data, but not both."
527527
)
528-
pred = self.model.predict(self.params, exog=exog, endog=endog, data=data)
528+
# Variables of the formula are those of the frame that called OLSResults.predict
529+
pred = self.model.predict(
530+
self.params, exog=exog, endog=endog, data=data, eval_env=4
531+
)
529532
if not missing:
530533
pred = pred.loc[pred.notnull().all(axis=1)]
531534
return pred
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
"""
2+
Missing values in a formula are dropped, whatever block of the formula has them.
3+
4+
The dependent variable, the exogenous regressors, the endogenous regressors and the
5+
instruments of a model made from a formula can have missing values, as the arrays that
6+
the models use. The observations are dropped with a MissingValueWarning, and the results
7+
are those of the model fit to the data after the rows with missing values were dropped.
8+
"""
9+
10+
import numpy as np
11+
from numpy.testing import assert_allclose, assert_array_equal
12+
import pandas as pd
13+
from pandas.testing import assert_index_equal
14+
import pytest
15+
16+
from linearmodels.iv import IV2SLS, IVGMM, IVGMMCUE, IVJIVE, IVLIML
17+
from linearmodels.shared.exceptions import MissingValueWarning
18+
19+
MODELS = [IV2SLS, IVLIML, IVGMM, IVGMMCUE, IVJIVE]
20+
FORMULA = "y ~ 1 + x + w + [e ~ z1 + z2]"
21+
NOBS = 150
22+
ROWS = [3, 40]
23+
BLOCKS = {"dependent": "y", "exog": "x", "endog": "e", "instruments": "z1"}
24+
25+
26+
@pytest.fixture(scope="module")
27+
def frame():
28+
rng = np.random.default_rng(0)
29+
df = pd.DataFrame(
30+
rng.standard_normal((NOBS, 6)),
31+
columns=["y", "x", "w", "e", "z1", "z2"],
32+
index=pd.Index([f"obs{i}" for i in range(NOBS)]),
33+
)
34+
df["e"] = df["e"] + df["z1"] + df["z2"]
35+
df["y"] = df["y"] + df["e"] + df["x"]
36+
df["weight"] = np.exp(0.3 * rng.standard_normal(NOBS))
37+
return df
38+
39+
40+
def assert_same_fit(res, expected, cls):
41+
# The estimates of the continuously updated GMM come from an optimizer, and the
42+
# optimizer sees data in another memory layout
43+
rtol = 1e-5 if cls is IVGMMCUE else 1e-10
44+
assert res.nobs == expected.nobs
45+
assert_index_equal(res.resids.index, expected.resids.index)
46+
assert_allclose(res.params, expected.params, rtol=rtol)
47+
assert_allclose(res.cov, expected.cov, rtol=rtol * 100)
48+
assert_allclose(res.resids, expected.resids, rtol=rtol * 100, atol=1e-12)
49+
50+
51+
@pytest.mark.parametrize("cls", MODELS)
52+
@pytest.mark.parametrize("block", list(BLOCKS))
53+
def test_missing_value_in_each_block(frame, cls, block):
54+
data = frame.copy()
55+
data.iloc[ROWS, data.columns.get_loc(BLOCKS[block])] = np.nan
56+
with pytest.warns(MissingValueWarning, match="missing values"):
57+
mod = cls.from_formula(FORMULA, data)
58+
assert mod.notnull.sum() == NOBS - 2
59+
assert not mod.notnull[ROWS].any()
60+
expected = cls.from_formula(FORMULA, frame.drop(index=frame.index[ROWS])).fit()
61+
assert_same_fit(mod.fit(), expected, cls)
62+
63+
64+
@pytest.mark.parametrize("cls", MODELS)
65+
def test_missing_values_in_every_block_in_different_rows(frame, cls):
66+
data = frame.copy()
67+
rows = {"y": 3, "x": 10, "e": 20, "z2": 30, "w": 31}
68+
for col, row in rows.items():
69+
data.iloc[row, data.columns.get_loc(col)] = np.nan
70+
with pytest.warns(MissingValueWarning, match="missing values"):
71+
mod = cls.from_formula(FORMULA, data)
72+
keep = np.ones(NOBS, dtype=bool)
73+
keep[list(rows.values())] = False
74+
assert_array_equal(mod.notnull, keep)
75+
expected = cls.from_formula(FORMULA, frame[keep]).fit()
76+
assert_same_fit(mod.fit(), expected, cls)
77+
78+
79+
@pytest.mark.parametrize("cls", [IV2SLS, IVLIML, IVGMM])
80+
@pytest.mark.parametrize("block", list(BLOCKS))
81+
def test_missing_values_with_weights(frame, cls, block):
82+
data = frame.copy()
83+
data.iloc[ROWS, data.columns.get_loc(BLOCKS[block])] = np.nan
84+
with pytest.warns(MissingValueWarning, match="missing values"):
85+
mod = cls.from_formula(FORMULA, data, weights=data["weight"])
86+
clean = frame.drop(index=frame.index[ROWS])
87+
expected = cls.from_formula(FORMULA, clean, weights=clean["weight"]).fit()
88+
res = mod.fit()
89+
assert res.nobs == NOBS - 2
90+
assert_allclose(res.params, expected.params, rtol=1e-10)
91+
assert_allclose(res.cov, expected.cov, rtol=1e-8)
92+
93+
94+
def test_no_missing_values_do_not_warn(frame, recwarn):
95+
IV2SLS.from_formula(FORMULA, frame).fit()
96+
assert not [w for w in recwarn if issubclass(w.category, MissingValueWarning)]
97+
98+
99+
def test_all_values_missing(frame):
100+
data = frame.copy()
101+
data["y"] = np.nan
102+
with pytest.raises(ValueError, match="All observations contain missing data"):
103+
IV2SLS.from_formula(FORMULA, data)
104+
105+
106+
@pytest.mark.parametrize("block", ["x", "e"])
107+
def test_predict_with_missing_values(frame, block):
108+
res = IV2SLS.from_formula(FORMULA, frame).fit()
109+
new = frame.iloc[:7].copy()
110+
new.iloc[2, new.columns.get_loc(block)] = np.nan
111+
pred = res.model.predict(res.params, data=new)
112+
assert pred.shape == (7, 1)
113+
assert np.isnan(pred.iloc[2, 0])
114+
assert pred.drop(index=new.index[2]).notnull().all().all()
115+
# The results drop the predictions that are missing, unless missing is True
116+
assert res.predict(data=new).shape == (6, 1)
117+
kept = res.predict(data=new, missing=True)
118+
assert kept.shape == (7, 1)
119+
assert_allclose(kept.drop(index=new.index[2]), pred.drop(index=new.index[2]))
120+
complete = res.predict(data=frame.iloc[:7])
121+
assert_allclose(
122+
complete.drop(index=new.index[2]), pred.drop(index=new.index[2]), rtol=1e-12
123+
)
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
"""
2+
predict with data evaluates the formula in the frame that calls it.
3+
4+
The default depth of IV model.predict was 4, which is the depth of results.predict, so
5+
calling model.predict from the top level of a script raised "call stack is not deep
6+
enough", and calling it from a nested function used the variables of another frame.
7+
"""
8+
9+
import json
10+
import subprocess
11+
import sys
12+
import textwrap
13+
14+
import numpy as np
15+
from numpy.testing import assert_allclose
16+
import pandas as pd
17+
import pytest
18+
19+
from linearmodels.iv import IV2SLS, IVGMM, IVGMMCUE, IVJIVE, IVLIML
20+
21+
MODELS = [IV2SLS, IVLIML, IVGMM, IVGMMCUE, IVJIVE]
22+
FORMULA = "y ~ 1 + I(x * mult) + [e ~ z]"
23+
MULT = 3.0
24+
25+
26+
@pytest.fixture(scope="module")
27+
def frame():
28+
rng = np.random.default_rng(0)
29+
df = pd.DataFrame(rng.standard_normal((200, 4)), columns=["y", "x", "e", "z"])
30+
df["e"] = df["e"] + df["z"]
31+
return df
32+
33+
34+
def expected_prediction(params, new):
35+
"""The prediction from the parameters, using the formula by hand"""
36+
return (params.iloc[0] + params.iloc[1] * MULT * new["x"].to_numpy()) + params.iloc[
37+
2
38+
] * new["e"].to_numpy()
39+
40+
41+
def predict_in_this_frame(cls, df, new):
42+
"""Fit a model whose formula uses a variable of this function, and predict"""
43+
mult = MULT # noqa: F841 Used by the formula
44+
mod = cls.from_formula(FORMULA, df)
45+
res = mod.fit()
46+
return res.params, mod.predict(res.params, data=new), res.predict(data=new)
47+
48+
49+
@pytest.mark.parametrize("cls", MODELS)
50+
def test_predict_in_a_module_level_function(frame, cls):
51+
new = frame.iloc[:7]
52+
params, by_model, by_results = predict_in_this_frame(cls, frame, new)
53+
expected = expected_prediction(params, new)
54+
assert_allclose(by_model.to_numpy().squeeze(), expected, rtol=1e-12)
55+
assert_allclose(by_results.to_numpy().squeeze(), expected, rtol=1e-12)
56+
57+
58+
@pytest.mark.parametrize("cls", MODELS)
59+
def test_predict_in_nested_functions(frame, cls):
60+
new = frame.iloc[:7]
61+
62+
def outer():
63+
def inner():
64+
return predict_in_this_frame(cls, frame, new)
65+
66+
return inner()
67+
68+
params, by_model, by_results = outer()
69+
expected = expected_prediction(params, new)
70+
assert_allclose(by_model.to_numpy().squeeze(), expected, rtol=1e-12)
71+
assert_allclose(by_results.to_numpy().squeeze(), expected, rtol=1e-12)
72+
73+
74+
def test_predict_in_the_test_function(frame):
75+
# The variables of this function are used, and not those of a frame above it
76+
mult = MULT # noqa: F841 Used by the formula
77+
new = frame.iloc[:7]
78+
mod = IV2SLS.from_formula(FORMULA, frame)
79+
res = mod.fit()
80+
expected = expected_prediction(res.params, new)
81+
assert_allclose(
82+
mod.predict(res.params, data=new).to_numpy().squeeze(), expected, rtol=1e-12
83+
)
84+
assert_allclose(res.predict(data=new).to_numpy().squeeze(), expected, rtol=1e-12)
85+
86+
87+
def test_explicit_depth_counts_the_frames_of_the_parser(frame):
88+
# eval_env is counted from the formula parser, as it always was: 2 is the frame
89+
# that calls predict, and each wrapper function needs one more
90+
mult = MULT # noqa: F841 Used by the formula
91+
new = frame.iloc[:7]
92+
mod = IV2SLS.from_formula(FORMULA, frame)
93+
res = mod.fit()
94+
expected = expected_prediction(res.params, new)
95+
96+
def wrapper():
97+
return mod.predict(res.params, data=new, eval_env=3)
98+
99+
assert_allclose(wrapper().to_numpy().squeeze(), expected, rtol=1e-12)
100+
same = mod.predict(res.params, data=new, eval_env=2)
101+
assert_allclose(same.to_numpy().squeeze(), expected, rtol=1e-12)
102+
103+
104+
SCRIPT = """
105+
import json
106+
107+
import numpy as np
108+
import pandas as pd
109+
110+
from linearmodels.iv import IV2SLS
111+
112+
rng = np.random.default_rng(0)
113+
df = pd.DataFrame(rng.standard_normal((200, 4)), columns=["y", "x", "e", "z"])
114+
df["e"] = df["e"] + df["z"]
115+
new = df.iloc[:7]
116+
mult = 3.0
117+
mod = IV2SLS.from_formula("y ~ 1 + I(x * mult) + [e ~ z]", df)
118+
res = mod.fit()
119+
out = {"params": res.params.tolist()}
120+
out["model"] = mod.predict(res.params, data=new).to_numpy().squeeze().tolist()
121+
out["results"] = res.predict(data=new).to_numpy().squeeze().tolist()
122+
123+
124+
def nested():
125+
def deeper():
126+
mult = 3.0
127+
m = IV2SLS.from_formula("y ~ 1 + I(x * mult) + [e ~ z]", df)
128+
r = m.fit()
129+
return m.predict(r.params, data=new), r.predict(data=new)
130+
131+
return deeper()
132+
133+
134+
by_model, by_results = nested()
135+
out["nested_model"] = by_model.to_numpy().squeeze().tolist()
136+
out["nested_results"] = by_results.to_numpy().squeeze().tolist()
137+
print(json.dumps(out))
138+
"""
139+
140+
141+
def test_predict_at_the_top_level_of_a_script(tmp_path):
142+
script = tmp_path / "predict_script.py"
143+
script.write_text(textwrap.dedent(SCRIPT))
144+
done = subprocess.run(
145+
[sys.executable, str(script)],
146+
capture_output=True,
147+
text=True,
148+
timeout=300,
149+
check=False,
150+
)
151+
assert done.returncode == 0, done.stderr
152+
out = json.loads(done.stdout.strip().splitlines()[-1])
153+
rng = np.random.default_rng(0)
154+
df = pd.DataFrame(rng.standard_normal((200, 4)), columns=["y", "x", "e", "z"])
155+
df["e"] = df["e"] + df["z"]
156+
expected = expected_prediction(pd.Series(out["params"]), df.iloc[:7])
157+
for key in ("model", "results", "nested_model", "nested_results"):
158+
assert_allclose(out[key], expected, rtol=1e-12, err_msg=key)

0 commit comments

Comments
 (0)