From my reading of the documentation associated with ggpubr::add_summary()
, stats::IQR()
and stats::quantile()
I expected my use of median_iqr
and median_q1q3
to return the same intervals. The documentation for IQR()
says it’s using quantile()
… and yet, doesn’t return the values I expected. Can someone explain?
sapply(c("reprex", "dplyr", "ggpubr"), require, character=TRUE)
tg <- ToothGrowth
# first two same
tg |>
group_by(dose) |>
summarise(med = median(len),
iqr = median_q1q3(len))
#> # A tibble: 3 × 3
#> dose med iqr$y $ymin $ymax
#> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 0.5 9.85 9.85 7.22 12.2
#> 2 1 19.2 19.2 16.2 23.4
#> 3 2 26.0 26.0 23.5 27.8
tg |>
group_by(dose) |>
summarise(med = median(len),
iqr = median_hilow_(len, ci=0.5))
#> # A tibble: 3 × 3
#> dose med iqr$y $ymin $ymax
#> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 0.5 9.85 9.85 7.22 12.2
#> 2 1 19.2 19.2 16.2 23.4
#> 3 2 26.0 26.0 23.5 27.8
# this is how the above are calculated:
tg |>
group_by(dose) |>
summarise(med = median(len),
iqr = median_q1q3(len),
lo = stats::quantile(len, probs = c(.25)),
hi = stats::quantile(len, probs = c(.75)))
#> # A tibble: 3 × 5
#> dose med iqr$y $ymin $ymax lo hi
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 0.5 9.85 9.85 7.22 12.2 7.22 12.2
#> 2 1 19.2 19.2 16.2 23.4 16.2 23.4
#> 3 2 26.0 26.0 23.5 27.8 23.5 27.8
# 3rd one is using this derivation of +/- 1 ci
# this is how 3rd is calculated:
tg |>
group_by(dose) |>
summarise(med = median(len),
iqr = median_iqr(len),
ci = IQR(len, type = 7)) |>
mutate(lo = med - ci,
hi = med + ci)
#> # A tibble: 3 × 6
#> dose med iqr$y $ymin $ymax ci lo hi
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 0.5 9.85 9.85 4.82 14.9 5.03 4.82 14.9
#> 2 1 19.2 19.2 12.1 26.4 7.12 12.1 26.4
#> 3 2 26.0 26.0 21.6 30.2 4.3 21.6 30.2
Created on 2024-04-29 by the reprex package (v2.0.1)