Skip to content

BUG: Center CompareMeans summary CI on the null - #11260

Open
xihaian251 wants to merge 2 commits into
statsmodels:mainfrom
xihaian251:sprint2-final/E
Open

xihaian251 wants to merge 2 commits into
statsmodels:mainfrom
xihaian251:sprint2-final/E

Conversation

@xihaian251

Copy link
Copy Markdown
Contributor
  • tests added / passed.
  • code/documentation is well formatted.
  • properly formatted commit message. See
    NumPy's guide.

Problem

With a nonzero value, CompareMeans.summary reports the coefficient on the scale mean1 - mean2 - value, but reports the confidence interval on the unadjusted mean-difference scale. Consequently, the reported coefficient and confidence interval describe different quantities.

import numpy as np
from statsmodels.stats.weightstats import CompareMeans, DescrStatsW

x1 = np.arange(15.0)
x2 = x1 - 1
cm = CompareMeans(DescrStatsW(x1), DescrStatsW(x2))
print(cm.summary(value=2))

For the default two-sample t test, the reported coefficient is -1.0000, but the pre-fix confidence interval is [-2.345, 4.345], centered at +1. On the reported coefficient scale, the interval should be [-4.345, 2.345].

Changes

Subtract value from both confidence-interval endpoints in summary, so that the coefficient and its interval both describe the restriction mean1 - mean2 - value.

This changes the summary presentation only. The underlying t and z tests, their statistics and p-values, and the confidence-interval methods for the raw mean difference are unchanged. The default value=0 summary is also unchanged.

Tests

The regression tests compare summary rows with independent closed-form calculations across t and z tests, pooled and unequal variances, one- and two-dimensional inputs, and zero and nonzero null values. Additional tests verify the underlying inference methods and the default summary output.

Local validation: 107 tests passed.

Affected test module: statsmodels/stats/tests/test_weightstats.py.

AI Disclosure

Contributions must comply with the statsmodels AI Policy.

Tick exactly one of the following. If AI tools were used, replace the <...>
placeholders in the second option with the tool name(s) and how the tool's output was
used.

  • No AI tools were used to develop this pull request.
  • AI tools were used.
    Tool(s): Qoder, ChatGPT.
    Used for: Qoder: patch implementation, regression tests, debugging and local verification; ChatGPT: source investigation, defect analysis and technical review.
    I have personally read, understood, and can explain every line of this diff, and
    have verified the statistical/numerical correctness of the change.

statistic, pvalue = cm.ztest_ind(usevar=usevar, value=2)
lower, upper = cm.zconfint_diff(usevar=usevar)

assert_allclose(statistic, expected[:, 2], rtol=1e-13)
lower, upper = cm.zconfint_diff(usevar=usevar)

assert_allclose(statistic, expected[:, 2], rtol=1e-13)
assert_allclose(pvalue, expected[:, 3], rtol=1e-13)
# Confidence-interval methods remain intervals for the raw mean
# difference; only the summary presentation is null-centered.
raw_expected = _comparemeans_direct(x1, x2, use_t, usevar, value=0)
assert_allclose(lower, raw_expected[:, 4], rtol=1e-13)
# difference; only the summary presentation is null-centered.
raw_expected = _comparemeans_direct(x1, x2, use_t, usevar, value=0)
assert_allclose(lower, raw_expected[:, 4], rtol=1e-13)
assert_allclose(upper, raw_expected[:, 5], rtol=1e-13)
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.

2 participants