Repository navigation
ENH: Add a formula interface to AbsorbingLS - #890
Conversation
Add AbsorbingLS.from_formula, which takes the dependent variable and the non-absorbed regressors from a formula while the effects are still passed to absorb. Store the formula in AbsorbingLS.formula. Closes bashtage#371
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #890 +/- ##
========================================
Coverage 99.69% 99.69%
========================================
Files 203 204 +1
Lines 38941 39054 +113
Branches 2503 2507 +4
========================================
+ Hits 38822 38935 +113
Misses 51 51
Partials 68 68
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
bashtage
left a comment
There was a problem hiding this comment.
Thanks, this is the hybrid I suggested in #371, and it is consistent with IV2SLS.from_formula. It agrees with OLS and WLS on the explicit design (with missing values, weights and transformed regressors) and with AbsorbingLS built by hand, to about 1e-14.
-
Please add
.. versionadded:: 7.2to the docstrings offrom_formulaandformula. -
test_formula_missing_valuesleaks twoMissingValueWarnings. Please wrap both constructions:from linearmodels.shared.exceptions import MissingValueWarning with pytest.warns(MissingValueWarning, match="missing values"): formula_res = AbsorbingLS.from_formula(formula, data, absorb=absorb).fit() with pytest.warns(MissingValueWarning, match="missing values"): direct_res = AbsorbingLS(parser.dependent, parser.exog, absorb=absorb).fit()
-
Release note: please add the PR number and mention
formula:- ``AbsorbingLS.from_formula`` creates a model from a formula for the dependent variable and the regressors that are not absorbed. The effects and interactions are passed to ``absorb`` and ``interactions``, not written in the formula, so that they stay sparse. ``AbsorbingLS.formula`` holds the formula (:issue:`371`, :issue:`890`).
Optional: "y ~ 1 + x1 | firm", the syntax mentioned in #371, fails with NumPy's "inhomogeneous shape" error (as IV2SLS.from_formula does). A ValueError saying that effects go in absorb would help users who try it.
Add versionadded 7.2 to from_formula and formula, expect the two MissingValueWarnings in test_formula_missing_values, and use the suggested release note. A formula that writes effects after '|' now raises a ValueError that points to absorb and interactions; '|' inside a term such as I(a | b) still works. Note in the guide that absorbed effects must be categorical.
|
Thanks @bashtage. All three points and the optional one are in d3f59256 (review):
Checks: |
|
Thanks. |
Thank you @bashtage |
This PR fixes issue #371.
Closes #371
As suggested in the issue, this adds a hybrid formula interface to
AbsorbingLS: the formula gives the dependent variable and the non-absorbed regressors, and the effects are still passed toabsorb(andinteractions), so they stay sparse.AbsorbingLS.from_formula(formula, data, *, absorb=None, interactions=None, weights=None, drop_absorbed=False)parses the formula with the parser the IV models use (IVFormulaParser), so the same transforms andC()terms work, and rows with a missing formula variable are dropped by the model as usual. A formula with an[endog ~ instruments]block raisesValueError, and so does one that writes effects after|(such asy ~ x | firm), with a message saying that effects go inabsorband interactions ininteractions;|inside a term, as inI(a | b), still works.AbsorbingLS.formulaholds the formula (""for models built from arrays, as in the IV models). Both are marked.. versionadded:: 7.2.doc/source/changes/7.0.rst(7.2, New features) and a short example in theAbsorbingLSsection ofdoc/source/iv/introduction.rst.Tests (
linearmodels/tests/iv/test_absorbing_formula.py): the formula model matchesAbsorbingLSbuilt from arrays, both from the parser output (absorb as a DataFrame and as anInteraction, with interactions, weights anddrop_absorbed) and from a design matrix built by hand (constant, transforms,C()dummies), plus the intercept rule,y ~ 0, missing values (with the expectedMissingValueWarnings), the IV-block error, the|error andI(a | b).Not changed: a missing value in a categorical used through
C()becomes a row of zero dummies inIVFormulaParser(withna_action="ignore"), so that row is kept in the base level.IV2SLS.from_formulabehaves the same way today; I left the shared parser alone here.Verification (macOS, Python 3.12):
mainand pass here;test_absorbing_formula.pypasses with-W error::linearmodels.shared.exceptions.MissingValueWarningpytest linearmodels/tests/iv: 9939 passed, 2400 skippedflake8,black --check,isort --checkandruff check linearmodelspass;mypyreports the same errors asmain; the new docstring example runs under--doctest-modules