Skip to content

BUG: Drop observations with a missing categorical value in formulas - #893

Open
bycorekci wants to merge 1 commit into
bashtage:mainfrom
bycorekci:fix/formula-missing-categorical-891
Open

bycorekci wants to merge 1 commit into
bashtage:mainfrom
bycorekci:fix/formula-missing-categorical-891

Conversation

@bycorekci

Copy link
Copy Markdown

What

Formulas now treat a missing value of a categorical or string variable as missing, so the observation is dropped with a MissingValueWarning, as it already is for array input. Applies to the formula parsers of the IV (including AbsorbingLS), panel, system (SUR) and asset-pricing models.

Why

formulaic encodes a missing categorical value as a row of zeros in the dummy columns, and linearmodels builds the matrices with na_action="ignore". The observation was kept in the base category without a warning, so y ~ 1 + x + C(g) gave different estimates from the same data passed as arrays or after dropna().

How

  • New helper model_matrix_from_formula in linearmodels/shared/utility.py builds the matrix with na_action="ignore" and sets the dummy columns of a missing categorical value to NaN. model_matrix_from_spec does the same, so predict(data=...) gives NaN for such rows.
  • IVFormulaParser, PanelFormulaParser and the asset-pricing parser use it. SUR uses IVFormulaParser.
  • Note added under "Changes that alter results" in the 7.2 release notes.

How to test

pytest linearmodels/tests/iv/test_formula_missing.py linearmodels/tests/panel/test_formula.py -k categorical

The new tests fail without the change. The IV, panel, system, asset-pricing and shared formula and utility tests pass.

Closes #891

A missing value of a categorical or string variable was encoded as a row
of zeros in the dummy columns, which kept the observation in the base
category without a warning. It is now missing, so the observation is
dropped with a MissingValueWarning, as with arrays.

Closes bashtage#891
@codecov

codecov Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 87.25490% with 13 lines in your changes missing coverage. Please review.
✅ Project coverage is 99.66%. Comparing base (1e04074) to head (ce3fda7).

Files with missing lines Patch % Lines
linearmodels/shared/utility.py 78.33% 7 Missing and 6 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #893      +/-   ##
==========================================
- Coverage   99.69%   99.66%   -0.03%     
==========================================
  Files         206      206              
  Lines       39413    39487      +74     
  Branches     2537     2549      +12     
==========================================
+ Hits        39291    39355      +64     
- Misses         52       58       +6     
- Partials       70       74       +4     
Flag Coverage Δ
adder 99.65% <87.25%> (-0.03%) ⬇️
subtractor 99.65% <87.25%> (-0.03%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Formulas keep observations with a missing categorical value as the base level

1 participant