Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for weights in objects.Est #3580

Merged
merged 2 commits into from
Dec 6, 2023
Merged

Add support for weights in objects.Est #3580

merged 2 commits into from
Dec 6, 2023

Conversation

mwaskom
Copy link
Owner

@mwaskom mwaskom commented Dec 4, 2023

This PR enhances seaborn.objects.Est to support a weighted mean, with confidence intervals:

diamonds = sns.load_dataset("diamonds")
(
    so.Plot(diamonds, x="color", y="price")
    .add(so.Dot(), so.Est(), weight="carat")
    .add(so.Range(), so.Est(), weight="carat")
)

(I don't know that this plot makes any particular sense, but it demonstrates how you'd use it...)

It is using np.average under the hood.

Currently support is limited to func="mean", errorbar=("ci", ...). There may be other estimators / error bar methods were it makes sense to compute a weighted version (e.g. weighted median/percentiles) although we would need to implement those in seaborn as I don't believe there's an implementation in our dependencies. I think this is fine and probably satisfies the majority of use cases; we can expand support later if there's enough interest.

(As a side note, we should probably error when func != "mean" and errorbar=("se", ...) since we don't generically compute the parameteric standard error of whatever estimator is passed).

See previous discussion in #3563, which is about the function interface. I actually think that it will be relatively straightforward to add first-class support in the function interface too on top of these changes. I might follow up here or maybe do that in another PR...

Copy link

codecov bot commented Dec 4, 2023

Codecov Report

Merging #3580 (5225101) into master (63d91bf) will increase coverage by 0.18%.
Report is 10 commits behind head on master.
The diff coverage is 100.00%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #3580      +/-   ##
==========================================
+ Coverage   98.34%   98.52%   +0.18%     
==========================================
  Files          75       75              
  Lines       24625    24622       -3     
==========================================
+ Hits        24217    24259      +42     
+ Misses        408      363      -45     
Files Coverage Δ
seaborn/_statistics.py 99.69% <100.00%> (+0.02%) ⬆️
seaborn/_stats/aggregation.py 97.61% <100.00%> (+0.11%) ⬆️
tests/_stats/test_aggregation.py 100.00% <100.00%> (ø)
tests/test_statistics.py 100.00% <100.00%> (ø)

... and 12 files with indirect coverage changes

@mwaskom
Copy link
Owner Author

mwaskom commented Dec 6, 2023

Going to merge this and then follow up on the function interface in a separate PR.

@mwaskom mwaskom merged commit 2bb945c into master Dec 6, 2023
13 checks passed
@mwaskom mwaskom deleted the enh/weighted_est branch December 6, 2023 02:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant