Repository navigation
Conversation
…_test The variable addition score test used the number of columns of exog_extra as the number of constraints. Columns that are linear combinations of other columns of exog_extra or of the model's exog, for example the linear term that a spline basis contains, do not add a constraint, so the degrees of freedom were too large and the p-value too small, and exactly redundant columns made the covariance of the score singular and raised a LinAlgError. The number of constraints is now the rank of exog_extra after projecting out exog, with the column tolerance of R's lm, and redundant columns are replaced by an orthonormal basis of the independent part. The statistic is invariant to this change of basis, so it is the same as the one obtained by dropping the redundant columns by hand. Independent columns are used as given, and the separate z-tests are unchanged. This applies to GLM.score_test and to the score_test method of GLM and discrete model results. closes statsmodels#5364
|
The PR body and the commit message say "the p-value too small". It is the other way round: too many degrees of freedom make the p-value too large (the test was conservative). The docstring Notes already say "too large". Please fix the wording and add :pr: |
|
The helper can now return rank 0 for a non-empty exog_extra (every column in the span of exog, which test_all_redundant treats as supported). With cov_type="HC0" the generic branch builds np.eye(len(self.params) + k_constraints)[-k_constraints:] and [-0:] selects all rows, so the result has k_constraint equal to the number of parameters and a p-value of 1.0. With nonrobust the result is df 0 and a p-value of nan. Before this PR those inputs raised LinAlgError (GLM) or returned df 1 (Poisson). Please handle k_rank == 0 explicitly, by raising a |
If every column of exog_extra is a linear combination of the columns of exog, the rank of exog_extra given exog is 0 and there is no constraint to test. The helper returned rank 0, so the nonrobust test had df 0 and a p-value of nan, and with HC0 the constraint matrix selected all rows of the identity, which gave a p-value of 1. Raise a ValueError instead. Correct the release note: too many degrees of freedom made the p-value too large, not too small. Add the PR reference.
|
Thanks. |
The check of the rank of exog_extra added in #10680 made bad input fail with confusing errors: an exog_extra with the wrong number of rows raised LinAlgError "Incompatible dimensions" (ValueError from column_stack before), and a nan or inf in exog_extra raised LinAlgError "SVD did not converge" (a nan statistic and p-value before). Validate the shape and the finiteness at the top of the helper that computes the rank. Both cases now raise a ValueError that names the problem, for GLM.score_test, the score_test of the GLM and discrete results and the generic score_test. The hypothesis="separate" path does not use the helper and is unchanged. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
The Notes of score_test in #10680 said that a column of exog_extra is redundant if its norm after projecting out exog and the other columns is less than 1e-7 times its norm. That is the sequential rule of R's lm, but the helper uses the singular values of the column-normalised residuals of the projection on exog. For one column the two agree, for several columns they differ (checked against lm in R 4.6.1). State the rule that the code uses, that the tolerance is the one of R's lm while R's glm and anova(test="Rao") use min(1e-7, epsilon / 1000), and that the projection is unweighted so a column that differs from the span of exog only on observations with weights near zero is counted in the degrees of freedom. Add versionchanged 0.15.1 to the docstrings of both score_test functions. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Follow-up to #10680. The tests of the rank of exog_extra only had exactly redundant columns and clearly independent columns, so they did not pin the other side of the 1e-7 tolerance: a mutant with tol=1e-3 passed all of them. - Add a block with noise of relative size 1e-5: the columns are kept, df is 4 and the statistic equals the closed form of the Poisson score statistic. - Test that an exog_extra with nan or infinite values or with the wrong number of rows raises a ValueError for the GLM and Poisson results, the HC0 covariance, GLM.score_test and the generic score_test. - Test the documented limitation that the projection is unweighted: a column that differs from the span of exog only on observations with weight 1e-12 is kept, adds nothing to the statistic and is counted in the degrees of freedom. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
BUG: Follow up #10680 on score_test exog_extra, input checks, docs and tests
The variable addition score test (
GLM.score_test, and thescore_testmethod of GLM and discrete model results throughstatsmodels.base._parameter_inference.score_test) used the number of columns ofexog_extraas the number of constraints. As described in #5364, columns that are linear combinations of other columns ofexog_extraor of the model'sexog, for example the linear term that a spline basis contains, do not add a constraint, so the degrees of freedom were too large and the p-value too small. Exactly redundant columns made the covariance of the score singular and the test raisedLinAlgError: Singular matrix.On main, with a Poisson GLM with a constant and two regressors and an
exog_extrawith two new columns, a copy of a regressor and a linear combination of the two new columns,score_testraisesLinAlgError; with the last two columns perturbed by 1e-9 noise it returns df 4 and p-value 0.935 instead of df 2 and 0.663.The fix follows the reduction in the issue:
exog_extrais projected onexog, the residual columns are divided by the norms of the columns ofexog_extra, and the rank is the number of singular values of the scaled residuals above 1e-7, the column tolerance of R'slm(for one column this is exactly R's criterion). If the rank is smaller than the number of columns,exog_extrais replaced by the orthonormal basis of the residuals with that many columns. The score test statistic is invariant to a change of basis of the added block, so it is the same as the one obtained by dropping the redundant columns by hand, and the degrees of freedom are the rank. Independent columns are used exactly as before, so existing results do not change, and the separate z-tests (hypothesis="separate") still use the columns as given.k_constraintspassed by the user is added to the rank as before. The 2-tuple form ofexog_extrafor two-part models is not covered; its docstring says so.Tests in
test_score_test.py, for the GLM result method,GLM.score_testand thePoissonresult method, withcov_type="nonrobust"and"HC0": the exactly redundant block gives df 2 and the same statistic and p-value as the two independent columns to 1e-10 (on main:LinAlgError); the nearly redundant block (1e-10 noise) gives df 2 and the same statistic to 1e-6 (on main: df 4); the p-value equalschi2.sf(statistic, 2); a column ofexogasexog_extragives df 0 and a statistic of 0 instead of raising; independent columns, a one dimensionalexog_extraand a block with a 1e-12 scale are passed through unchanged; the rank agrees withmatrix_rankof thepartial_projectresidual used in the issue. A last test reproduces the issue with the B-spline basis of two variables (BSplines, df 6 each) added to a model with their linear terms: the rank is 10 for 12 columns, the statistic equals the one of the hand-reduced basis (principal components of the projected residual, as in the issue) to 1e-8, and the p-value uses 10 degrees of freedom.I could not compare with R here (it is not installed on the machine I used);
anova(glm_small, glm_large, test="Rao")uses the rank of the added block and would be the reference to add if you would like one in the test file.closes #5364
Also closes #7203.
NumPy's guide.
AI Disclosure
Tool(s):
Claude Code.Used for:
drafting the tests, running the reproductions and cross-checks.I have personally read, understood, and can explain every line of this diff, and
have verified the statistical/numerical correctness of the change.