Repository navigation
fix: OneWayLS pairwise f-tests use pair nobs for df_denom, not full model df - #9796
Closed
amethystani wants to merge 3 commits into
Closed
amethystani wants to merge 3 commits into
amethystani wants to merge 3 commits into
Conversation
…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]>
Contributor
|
@amethystani this PR also includes changes from previous two PR's, you need to remove the changes that does not belong here.. Thanks! |
Contributor
|
@amethystani gentle ping, are you planning to clean these PR's, maybe then i can help in review 😊 |
Member
|
Can do a one-way ANOVA using |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 inflatesdf_denomand 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 runf_testfor each pair(i, j). The residual df of that model issum(nobs_g) - G*k, far larger than the correct two-group value ofnobs_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]whereDis a dummy for the second group. Thef_testcontrast[0...0 | I_k]then tests equality of the group-specific coefficients with the correct df:The
alltest (full H0: all groups equal) is unchanged — it still uses the full joint model.The fix works for both
het=False(OLS) andhet=True(WLS).Tests added
statsmodels/sandbox/regression/tests/test_onewaygls.py(new file):test_pairwise_df_denom_is_pair_nobs_not_total— exact df checktest_pairwise_df_strictly_less_than_full_model_df— sanity checktest_overall_ftest_uses_full_model_df— regression guard foralltesttest_pairwise_pvalue_is_finite_and_in_range— output validitytest_pairwise_df_correct_for_het— parametrized over het flag@josef-pkt