Skip to content

fix: OneWayLS pairwise f-tests use pair nobs for df_denom, not full model df - #9796

Closed
amethystani wants to merge 3 commits into
statsmodels:mainfrom
amethystani:fix/onewayls-pairwise-effective-df
Closed

amethystani wants to merge 3 commits into
statsmodels:mainfrom
amethystani:fix/onewayls-pairwise-effective-df

Conversation

@amethystani

Copy link
Copy Markdown
Contributor

Summary

Fixes gh-9777.

OneWayLS.ftest_summary() was computing pairwise group comparisons using the residual degrees of freedom from the full joint model (all G groups). This inflates df_denom and makes every pairwise F-test anticonservative — rejection rates are higher than the nominal size.

Root cause

The old code re-used self.lsjoint (fitted on all groups) to run f_test for each pair (i, j). The residual df of that model is sum(nobs_g) - G*k, far larger than the correct two-group value of nobs_i + nobs_j - 2*k.

Fix

For each pair (i, j), subset to only those two groups and fit a dedicated two-group model with interacted design matrix [X | D*X] where D is a dummy for the second group. The f_test contrast [0...0 | I_k] then tests equality of the group-specific coefficients with the correct df:

df_denom = nobs_i + nobs_j - 2*k

The all test (full H0: all groups equal) is unchanged — it still uses the full joint model.

The fix works for both het=False (OLS) and het=True (WLS).

Tests added

statsmodels/sandbox/regression/tests/test_onewaygls.py (new file):

  • test_pairwise_df_denom_is_pair_nobs_not_total — exact df check
  • test_pairwise_df_strictly_less_than_full_model_df — sanity check
  • test_overall_ftest_uses_full_model_df — regression guard for all test
  • test_pairwise_pvalue_is_finite_and_in_range — output validity
  • test_pairwise_df_correct_for_het — parametrized over het flag

@josef-pkt

…ls in GLM.fit

In the non-IRLS (gradient optimizer) path, GLM.fit previously assigned
cov_p from rslt.normalized_cov_params before the try/except block, but
that assignment was immediately overwritten inside the try block, making
it dead code. When LinAlgError was raised the except branch set cov_p=None,
silently discarding the optimizer's own Hinv estimate.

Move the normalized_cov_params fallback into the except branch so it is
actually used when np.linalg.inv raises LinAlgError.

Fixes statsmodels#9776

Co-authored-by: amethystani <[email protected]>
…n_diag parameter

cov_nearest works by converting to a correlation matrix, which requires
all diagonal elements to be strictly positive. When the input has zero or
negative diagonal entries, numpy divides by zero silently, producing NaN
throughout the result.

Fix by:
- Raising a descriptive ValueError when diagonal elements are <= 0 and
  min_diag is not supplied
- Adding a min_diag parameter that clips offending diagonal entries to a
  user-supplied minimum (with a UserWarning when clipping occurs)

Fixes statsmodels#9675

Co-authored-by: amethystani <[email protected]>
…odel

Pairwise comparisons in ftest_summary() were using the residual df from
the full joint model (all G groups), making df_denom too large and the
F-test anticonservative.  The correct df for testing group i vs group j
is  nobs_i + nobs_j - 2*k  (two-group model residual df), not the full
sample df.

Fix: fit a separate two-group OLS/WLS on the subset [group_i, group_j]
for each pair, using a dummy-interacted design [X | D*X] where D flags
the second group.  The overall 'all' test is unaffected and still uses
the full joint model.

Fixes statsmodelsgh-9777

Co-authored-by: amethystani <[email protected]>
@Aniketsy

Copy link
Copy Markdown
Contributor

@amethystani this PR also includes changes from previous two PR's, you need to remove the changes that does not belong here.. Thanks!

@Aniketsy

Copy link
Copy Markdown
Contributor

@amethystani gentle ping, are you planning to clean these PR's, maybe then i can help in review 😊

@bashtage

Copy link
Copy Markdown
Member

OneWayLS has been moved to the archive and is unnecessary.

Can do a one-way ANOVA using

import statsmodels.api as sm
import pandas as pd
from statsmodels.formula.api import ols

# 1. Fit the OLS model (statsmodels treats strings/categories as factors automatically)
model = ols("dependent_var ~ C(group_variable)", data=df).fit()

# 2. Perform the One-Way ANOVA
anova_table = sm.stats.anova_lm(model, typ=2)

# 3. View the results
print(anova_table)

@bashtage bashtage closed this Aug 25, 2026
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.

BUG/ENH (sandbox) OnewayLS pairwise comparison - effective degrees of freedom

3 participants