Skip to content

BUG: Do not let the order of the clusters change the PanelOLS degrees… - #795

Merged
bashtage merged 2 commits into
mainfrom
lm71-fix-panel-clusters
Oct 13, 2026
Merged

bashtage merged 2 commits into
mainfrom
lm71-fix-panel-clusters

Conversation

@bashtage

Copy link
Copy Markdown
Owner

… of freedom

With one effect and a clustered covariance, PanelOLS does not count the degrees of
freedom of the effect in the debiased covariance if the effect is nested in the clusters
(entity effects and clusters by entity, as in Stata). With two cluster variables the
function that tests the nesting overwrote its result for each cluster variable, so that
only the last variable counted. The standard errors of the same model with the same two
cluster variables depended on their order. In the example of the new tests, entity effects
with the clusters (entity, time) count the effects and have a standard error of 0.12412
for the first coefficient, and with (time, entity) they do not count them and have 0.11603.

The effect is now nested if each of its groups is in one group of every cluster variable,
which is the rule of AbsorbingLS, so the result does not depend on the order. An effect
that is nested in only one of the two variables is counted. The other way to remove the
dependence on the order, to skip an effect that is nested in any variable, would also
change entity effects with the clusters (entity, time), the usual two-way clustering.

The standard errors change if the effect is nested in the last of two cluster variables
but not in the first: entity effects with (time, entity) or (time, industry), and time
effects with (entity, time) or (industry, time), where the effects were not counted. They
change by the factor sqrt((n - k) / (n - k - N)), which is 7% in the example. One cluster
variable, the models with two effects, and the other orders do not change.

The tests compare the covariance with the covariance of the regression on the data from
which the effect is removed, which is written out in NumPy, and check the order of the
variables for entity, time and other effects.

… of freedom

With one effect and a clustered covariance, PanelOLS does not count the degrees of
freedom of the effect in the debiased covariance if the effect is nested in the clusters
(entity effects and clusters by entity, as in Stata). With two cluster variables the
function that tests the nesting overwrote its result for each cluster variable, so that
only the last variable counted. The standard errors of the same model with the same two
cluster variables depended on their order. In the example of the new tests, entity effects
with the clusters (entity, time) count the effects and have a standard error of 0.12412
for the first coefficient, and with (time, entity) they do not count them and have 0.11603.

The effect is now nested if each of its groups is in one group of every cluster variable,
which is the rule of AbsorbingLS, so the result does not depend on the order. An effect
that is nested in only one of the two variables is counted. The other way to remove the
dependence on the order, to skip an effect that is nested in any variable, would also
change entity effects with the clusters (entity, time), the usual two-way clustering.

The standard errors change if the effect is nested in the last of two cluster variables
but not in the first: entity effects with (time, entity) or (time, industry), and time
effects with (entity, time) or (industry, time), where the effects were not counted. They
change by the factor sqrt((n - k) / (n - k - N)), which is 7% in the example. One cluster
variable, the models with two effects, and the other orders do not change.

The tests compare the covariance with the covariance of the regression on the data from
which the effect is removed, which is written out in NumPy, and check the order of the
variables for entity, time and other effects.
… singletons

PanelOLS(..., singletons=False) drops the observations that are alone in a group of an
effect, and the clustered covariance with cluster_entity or cluster_time (without the
clusters option) raised "IndexError: Boolean index has wrong length: 165 instead of 162".
The clusters of the entities and of the periods are made from the data of the model,
which no longer has the singletons, and the code then removed the singletons from them
using the mask of the singletons, which has a value for each observation before they
were dropped. The mask is only needed for the clusters that the user gives, which have a
value for each observation that is not missing. It is now applied to those clusters
right after they are formatted, so the clusters of the model and the clusters of the
user have the same observations. The models without singletons, and the clusters that
the user gives with cluster_entity or cluster_time, did not raise and give the same
results.

The tests compare the results with those of the model estimated from the data from which
the singletons were dropped, for entity effects and for entity and time effects, with and
without weights and missing values, and with each combination of the clusters.
@codecov

codecov Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.62%. Comparing base (1782b62) to head (3a937a7).
⚠️ Report is 107 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #795      +/-   ##
==========================================
+ Coverage   99.59%   99.62%   +0.02%     
==========================================
  Files         128      141      +13     
  Lines       24435    28166    +3731     
  Branches     1820     1981     +161     
==========================================
+ Hits        24336    28059    +3723     
- Misses         39       41       +2     
- Partials       60       66       +6     
Flag Coverage Δ
adder 99.60% <100.00%> (+0.03%) ⬆️
subtractor 99.60% <100.00%> (+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.

@bashtage
bashtage merged commit c43d7ad into main Oct 13, 2026
24 checks passed
@bashtage
bashtage deleted the lm71-fix-panel-clusters branch October 13, 2026 10:33
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.

1 participant