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

Better weighted Median? #72

Closed
Waschoi opened this issue Oct 19, 2020 · 3 comments
Closed

Better weighted Median? #72

Waschoi opened this issue Oct 19, 2020 · 3 comments

Comments

@Waschoi
Copy link

Waschoi commented Oct 19, 2020

In today's analysis of a colleague who uses SPSS, I found that the weighted median does not always correspond. The current implementation using matrixStats::weightedMedian matches the grouped median, but this is most likely not intended. I would rather recommend sjstats::weighted_median() or MetricsWeighted::weighted_median(), especially since these also allow the calculation of quantiles. I would be very pleased, if my suggestion would be implemented. Thanks for the great job you did with the package.

@gdemin
Copy link
Owner

gdemin commented Oct 19, 2020

Hi,
Yes, I will change w_median to make it is corresponding to the SPSS.
But could you provide example when these implementations are different? I need it for tests.
Also link with description of the difference between the algos will be helpful.

@Waschoi
Copy link
Author

Waschoi commented Oct 20, 2020

I found the answer, no need to change the package: line 70 of https://github.com/gdemin/expss/blob/master/R/weighted_stats.R
Needs to be changed to: interpolate = F

here an reprod example:
testdata <- tribble(~id, ~grum, ~wph, ~group,
1:1000, sample(seq(300, 1500, .15), 1000, replace = T),
sample(seq(.3, 3, .001), 1000, replace = T),
sample(seq(10, 30, 1), 1000, replace = T))%>%
unnest()

testdata%>%
group_by(group)%>%
summarise_at(vars(grum), list(expss = ~expss::w_median(., weight = wph),
matrix = ~matrixStats::weightedMedian(., wph, interpolate = F, ties = "weighted"),
sj = ~sjstats::weighted_median(., wph),
metric = ~MetricsWeighted::weighted_median(., wph)))

which gives the same median like SPSS or Stata 13. Also the help-file of matrixStats says, that interpolate Needs to be F, so that ties is working.

Greetings!

@gdemin
Copy link
Owner

gdemin commented Nov 16, 2020

Fixed in 0.10.7

@gdemin gdemin closed this as completed Nov 16, 2020
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

No branches or pull requests

2 participants