fix(stat_summary): compute mean_se from the sample variance - #1119
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1119 +/- ##
=======================================
Coverage 88.20% 88.20%
=======================================
Files 222 222
Lines 16253 16253
Branches 2080 2080
=======================================
Hits 14336 14336
Misses 1315 1315
Partials 602 602 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
has2k1
requested changes
Sep 4, 2026
| half_width = res2["ymax"].iloc[0] - res2["y"].iloc[0] | ||
| assert half_width == pytest.approx(2 * expected) | ||
|
|
||
|
|
Owner
There was a problem hiding this comment.
We can remove this test. The comment in the function and the visual test are sufficient.
dylanpulver
force-pushed
the
fix-mean-se-sample-variance
branch
from
September 4, 2026 13:35
acf798e to
a57b090
Compare
has2k1
force-pushed
the
fix-mean-se-sample-variance
branch
from
September 7, 2026 16:25
de31647 to
a57b090
Compare
`mean_se` used `np.var(series)`, whose default `ddof=0` is the population variance. The standard error of the mean is built from the sample variance, so the interval was too narrow by a factor of sqrt((n-1)/n) (29% at n=2, 5% at n=10). ggplot2's reference implementation is `se <- mult * sqrt(stats::var(x) / length(x))`, and R's `stats::var` is the sample variance. The two sibling summary functions in this module already use sample estimators: `mean_sdl` uses `Series.std()` and `mean_cl_normal` uses `scipy.stats.sem`, both `ddof=1`. `mean_se` is also the default `fun_data` for `stat_summary_bin`, so the baseline image for the affected test is regenerated.
has2k1
force-pushed
the
fix-mean-se-sample-variance
branch
from
September 7, 2026 16:27
a57b090 to
e3696f0
Compare
Owner
|
@dylanpulver, thank you. |
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.
mean_sebuilds the standard error fromnp.var(series), whose defaultddof=0is the population variance. The standard error of the mean uses the sample variance, so the interval is too narrow bysqrt((n-1)/n)— 29% at n=2, 5% at n=10.ggplot2's reference implementation (
R/stat-summary.R):R's
stats::varis the sample variance.The two sibling summary functions in this module already use sample estimators —
mean_sdlviaSeries.std()andmean_cl_normalviascipy.stats.sem, bothddof=1.mean_sewas the only one onddof=0.Measured half-width for
[2,4,4,4,5,5,7,9](n=8):s/sqrt(n)=scipy.stats.sem= ggplot2ratio 0.9354143467 =
sqrt(7/8)exactly.The fix keeps ggplot2's
sqrt(var/n)shape.series.std()/sqrt(n)also passes the suite, but is worse:ndarray.std()defaults toddof=0, so it would keep the bug for array input and raise on a list.mean_seis the defaultfun_dataforstat_summary_bin, somean_se.pngis regenerated (viatools/update_baseline_images.py, thenoxipng -o 4 --strip safe).Tests: 850 passed, 5 skipped, 1 xpassed, 0 failed. An earlier version of this description reported 6
test_geom_smoothfailures; those were missing dependencies in my checkout. The added test fails on the unpatched function.Found and drafted with Claude Opus 5 (
claude-opus-5); I reviewed and verified the change.