Skip to content

Commit 26064e7

Browse files
authored
Merge pull request #546 from bashtage/fix-wights-absorbed
BUG: Fix weights absorbed
2 parents 1ad6b8c + 77f164c commit 26064e7

File tree

6 files changed

+60
-8
lines changed

6 files changed

+60
-8
lines changed

linearmodels/panel/model.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1830,7 +1830,7 @@ def fit(
18301830
if not weighted:
18311831
y, x, ybar = self._fast_path(low_memory=low_memory)
18321832
y_effects = np.array([0.0])
1833-
x_effects = np.zeros(x.shape[1])
1833+
x_effects = np.zeros(x.shape)
18341834
else:
18351835
y, x, ybar, y_effects, x_effects = self._weighted_fast_path(
18361836
low_memory=low_memory
@@ -1883,7 +1883,7 @@ def fit(
18831883

18841884
# Adjust exog
18851885
self.exog = PanelData(self.exog.dataframe.iloc[:, retain])
1886-
x_effects = x_effects[retain]
1886+
x_effects = x_effects[:, retain]
18871887

18881888
params = _lstsq(x, y, rcond=None)[0]
18891889
nobs = self.dependent.dataframe.shape[0]

linearmodels/tests/panel/test_panel_ols.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import pandas as pd
77
import pytest
88

9+
from linearmodels.datasets import wage_panel
910
from linearmodels.iv.model import IV2SLS
1011
from linearmodels.panel.data import PanelData
1112
from linearmodels.panel.model import PanelOLS, PooledOLS
@@ -1343,7 +1344,7 @@ def test_singleton_removal_other_effects(data):
13431344
def test_singleton_removal_mixed(singleton_data, other_effects):
13441345
if other_effects == 1:
13451346
other_effects = PanelData(singleton_data.c).dataframe.iloc[:, [0]]
1346-
elif other_effects == 2:
1347+
else:
13471348
other_effects = singleton_data.c
13481349
mod = PanelOLS(singleton_data.y, singleton_data.x, other_effects=other_effects)
13491350
res_keep = mod.fit(use_lsmr=True)
@@ -1531,3 +1532,26 @@ def test_entity_into():
15311532
ti = res.time_info
15321533
assert ti["total"] == 4
15331534
assert ti["min"] == 16
1535+
1536+
1537+
@pytest.mark.parametrize("path", ["use_lsdv", "low_memory", ""])
1538+
def test_absorbed_with_weights(path):
1539+
data = wage_panel.load().copy()
1540+
year = pd.Categorical(data.year)
1541+
data = data.set_index(["nr", "year"])
1542+
data["year"] = year
1543+
# and random number between 0 and 1 for weights
1544+
data["rand"] = np.random.rand(data.shape[0])
1545+
data["absorbe"] = data.groupby("nr")["union"].transform("mean")
1546+
1547+
fit_options = {}
1548+
if path:
1549+
fit_options[path] = True
1550+
1551+
with pytest.warns(AbsorbingEffectWarning, match="Variables have been"):
1552+
PanelOLS.from_formula(
1553+
"lwage ~1+absorbe + married + EntityEffects",
1554+
data=data,
1555+
weights=data["rand"],
1556+
drop_absorbed=True,
1557+
).fit(**fit_options)

linearmodels/tests/system/test_formulas.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@
5050
key += " : " + str(m[0].__name__)
5151
ids.append(key)
5252

53+
escape_fmlas = fmlas[:2] + fmlas[3:]
54+
escape_params = list(product(escape_fmlas, models))
55+
56+
escape_ids = []
57+
for f, m in escape_params:
58+
key = "--".join([value for value in f.values()])
59+
key += " : " + str(m[0].__name__)
60+
escape_ids.append(key)
61+
5362

5463
def sigmoid(v):
5564
return np.exp(v) / (1 + np.exp(v))
@@ -62,6 +71,13 @@ def config(request):
6271
return fmla, model, interface
6372

6473

74+
@pytest.fixture(scope="module", params=escape_params, ids=escape_ids)
75+
def escape_config(request):
76+
fmla, model_interace = request.param
77+
model, interface = model_interace
78+
return fmla, model, interface
79+
80+
6581
def test_formula(config):
6682
fmla, model, interface = config
6783
for key in fmla:
@@ -182,8 +198,8 @@ def test_parser(config):
182198
assert_frame_equal(eq1[key], eq2[key])
183199

184200

185-
def test_formula_escaped(config):
186-
fmla, model, interface = config
201+
def test_formula_escaped(escape_config):
202+
fmla, model, interface = escape_config
187203
for key in fmla:
188204
if "[" in fmla[key] and model not in (IVSystemGMM, IV3SLS):
189205
return
@@ -205,6 +221,7 @@ def fix_formula(fmla):
205221
escaped_fmla = fix_formula(fmla)
206222
data = joined.copy()
207223
data.columns = cols
224+
208225
mod = model.from_formula(escaped_fmla, data)
209226
pmod = pickle.loads(pickle.dumps(mod))
210227
mod_fmla = interface(escaped_fmla, data)

pyproject.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,13 @@ exclude = '''
2626

2727
[tool.setuptools_scm]
2828
write_to = "linearmodels/_version.py"
29+
30+
[tool.pyright]
31+
exclude = [
32+
"**/tests/**",
33+
]
34+
35+
[tool.mypy]
36+
exclude = [
37+
"tests",
38+
]

requirements-dev.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
xarray>=0.16
22
mypy>=1.3
3-
black[jupyter]==23.3.0
3+
black[jupyter]==23.7.0
44
pytest>=7.3.0
55
isort>=5.12
66
ipython
@@ -10,6 +10,7 @@ nbconvert
1010
nbformat
1111
nbsphinx
1212
numpydoc
13+
pandas-stubs
1314
pytest-xdist
1415
pytest-cov
1516
seaborn

requirements-test.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
black[jupyter]==23.3.0
1+
black[jupyter]==23.7.0
22
coverage
33
flake8
44
isort
55
matplotlib
6-
pytest>=7.0,<7.1
6+
pytest>=7.3.0
77
pytest-xdist
88
pytest-cov
99
seaborn

0 commit comments

Comments
 (0)