Repository navigation
fix(stat_density): use the standard deviation when nrd0 sees a zero IQR - #1149
Open
nk-batman21 wants to merge 1 commit into
Open
nk-batman21 wants to merge 1 commit into
nk-batman21 wants to merge 1 commit into
Conversation
R's bw.nrd0 falls back to the standard deviation when the IQR is 0. nrd0 fell back to IQR/1.349, which is always 0 at that point, so the bandwidth came from the first value, or 1. The same values in another row order then gave a different density, violin, sina or beeswarm.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1149 +/- ##
=======================================
Coverage 88.92% 88.92%
=======================================
Files 229 229
Lines 17320 17320
Branches 2204 2204
=======================================
Hits 15402 15402
Misses 1289 1289
Partials 629 629 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This branch has not been deployed
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.
When the IQR of a group is 0,
nrd0(the defaultbwofgeom_density,geom_violin,geom_sinaandgeom_beeswarm) falls back toIQR/1.349, which is 0 at that point, and then toabs(x[0])or 1. So the bandwidth ignores the spread of the data and depends on which row comes first. This happens whenever the middle half of a group is a single value, e.g. zero-inflated data such as daily rainfall, or ratings where one answer dominates.R's
bw.nrd0uses the standard deviation in this case:For these values that gives 1.9164; plotnine gave 0.5679, or 5.6786 with the rows reversed. This change uses
stdinstead ofstd_estimatein the fallback, so it matches R. When all the values are equal the result is unchanged. The fallback was added for #317.Ran:
pytest tests/test_geom_density.py tests/test_geom_violin.py tests/test_geom_sina.py tests/test_geom_beeswarm.py tests/test_stat_calculate_methods.pyon Python 3.14 and 3.11 (lowest-direct)Ran:
ruff check .,ruff format --check .,pyright