Weighted Median

hi all,

I am trying to get a weighted median and IQR (25th and 75th %iles) for my variable GCSTot_recode. It needs to be grouped by Cause_code.

Here is my weighted mean code:

weighted_df <- MOIweights_joined %>%
group_by(Cause_code) %>%
summarise(weighted_mean_PTADays_impute = weighted.mean(x = PTADays_impute, w = stab_weights, na.rm = TRUE))

However, I cannot figure out how to get the weighted median.

I have tried:
weighted_df <- MOIweights_joined |>
group_by(Cause_code) |>
summarise(weighted_median_GCSTot_recode = wtd.quantile(GCSTot_recode, w = stab_weights, na.rm = TRUE))

and get this error code: Warning: Returning more (or less) than 1 row per summarise() group was deprecated in dplyr 1.1.0.
Please use reframe() instead.
When switching from summarise() to reframe(), remember that reframe() always returns an ungrouped data frame and adjust accordingly.summarise() has grouped output by 'Cause_code'. You can override using the .groups argument.

I have also tried:
weighted_df <- MOIweights_joined %>%
group_by(Cause_code) %>%
summarise(weighted_median_GCSTot_recode = wtd.median(GCSTot_recode, w = stab_weights)

But i get this error code: Error in summarise(., weighted_median_GCSTot_recode = wtd.median(GCSTot_recode, :

:information_source: In group 1: Cause_code = 0.
Caused by error in wtd.median():
! could not find function "wtd.median"

Any help is greatly appreciated!

Have you put in a library statement for the wtd.median function? Maybe the package ergm?

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.