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

fquantile bug #523

Closed
zander-prinsloo opened this issue Jan 9, 2024 · 2 comments
Closed

fquantile bug #523

zander-prinsloo opened this issue Jan 9, 2024 · 2 comments

Comments

@zander-prinsloo
Copy link

Hi Sebastian,

I picked up an issue with fquantile and compare it to Hmisc::wtd.quantile() in the example below. I realize that the output from these two functions should not be exactly the same because they are not using the same approach to calculating the quantiles, so the small differences from most of the quantiles are not what I am referring to.

The issue I am pointing at is the 60% quantile from collapse::fquantile(), which decreases and then jumps back up for 70%. For the example below, this issue persists even when using different types (i.e. type = 5, etc.). Interestingly, the final example below where fquantile( x = c(1:100), w = rep(1, 100), probs = seq(from = 0.1, to = 1, by = 0.1)) the issue seems to disappear.

Thank you very much! As always, I am really enjoying the collapse package and appreciate all your work on it greatly.

Zander

library(collapse, Hmisc)

x <- c(0.3031276, 0.4571868, 0.5611382, 0.6589519, 0.7138266, 0.7599011,
             0.7859537, 0.8227337, 0.8861251, 0.9227733, 0.9741309, 1.0043999,
             1.0527802, 1.1004243, 1.1344353, 1.1543153, 1.1986660, 1.2225957,
             1.2467686, 1.2881969, 1.3103023, 1.3470067, 1.3717622, 1.4171236,
             1.4481890, 1.4767816, 1.5113053, 1.5520433, 1.5766412, 1.6206641,
             1.6614114, 1.6988976, 1.7187559, 1.7445536, 1.7655008, 1.7951064,
             1.8154464, 1.8412325, 1.8843781, 1.9022643, 1.9349266, 1.9775958,
             2.0209325, 2.0538855, 2.0880485, 2.1341179, 2.1662982, 2.2073586,
             2.2297966, 2.2696783, 2.3054806, 2.3522053, 2.4107709, 2.4608277,
             2.4788487, 2.5041681, 2.5652320, 2.6004631, 2.6632136, 2.7169775,
             2.7553121, 2.8365738, 2.9027606, 2.9793340, 3.0432024, 3.1047007,
             3.1667340, 3.2568974, 3.3373767, 3.4326348, 3.5228778, 3.6003411,
             3.6967467, 3.8003151, 3.9020071, 3.9976514, 4.1647284, 4.2741119,
             4.3598892, 4.4362847, 4.5396775, 4.6069366, 4.7372277, 4.9295803,
             5.0362082, 5.1420157, 5.2902402, 5.5672972, 5.7774503, 6.2099230,
             6.7065727, 6.9770491, 7.2619571, 7.8597135, 8.3030427, 8.8661065,
             9.9074310, 10.7280315, 12.6459594, 18.0195166)

w <- c(148.832031, 861.869185, 753.803590, 787.644447, 680.662491,
            889.665800, 835.005236, 744.138014, 631.031320, 59.672307,
            211.059592, 180.225505, 481.189884, 428.716380, 963.777711,
            768.157068, 620.819895, 55.915707, 744.457704, 80.550037,
            596.757105, 293.466754, 845.799925, 127.239379, 65.016717,
            982.369334, 538.933227, 304.913201, 1000.549589, 229.999551,
            900.345681, 715.797532, 926.045942, 445.912266, 57.284686,
            200.823635, 311.681956, 288.483657, 715.706993, 181.051739,
            988.774738, 558.520419, 146.126589, 706.082199, 443.189061,
            194.593867, 623.515651, 281.936126, 4.341623, 606.789155,
            929.317259, 220.582461, 356.963444, 69.051047, 267.023953,
            870.515426, 145.374869, 285.938089, 343.880965, 227.054974,
            874.782554, 899.026458, 551.543717, 838.777356, 28.366997,
            668.319839, 791.231414, 616.596092, 211.014267, 754.605834,
            956.967726, 566.716436, 935.859162, 64.795456, 619.012640,
            205.367614, 979.031488, 942.429357, 65.017801, 222.496672,
            465.333957, 305.959686, 595.160696, 361.537173, 359.102206,
            842.988743, 649.262715, 385.603889, 416.167539, 512.941754,
            911.603964, 424.459892, 823.561780, 856.990370, 534.810471,
            870.347438, 392.741960, 965.569166, 663.887435, 338.334480)

collapse::fquantile(
  x = x, 
  w = w, 
  probs = seq(from = 0.1, to = 1, by = 0.1)
)
#>        10%        20%        30%        40%        50%        60%        70% 
#>  0.8381894  1.0836237  1.6189591  1.9205753  2.3487439  1.3716575  3.6852311 
#>        80%        90%       100% 
#>  4.7764544  7.3361690 18.0195166
Hmisc::wtd.quantile(
  x = x, 
  w = w, 
  probs = seq(from = 0.1, to = 1, by = 0.1)
)
#>        10%        20%        30%        40%        50%        60%        70% 
#>  0.8227337  1.2467686  1.6206641  1.9349266  2.3522053  2.9793340  3.6003411 
#>        80%        90%       100% 
#>  4.7372277  7.2619571 18.0195166
collapse::fquantile(
  x = c(1:100), 
  w = rep(1, 100), 
  probs = seq(from = 0.1, to = 1, by = 0.1)
)
#>   10%   20%   30%   40%   50%   60%   70%   80%   90%  100% 
#>  10.9  20.8  30.7  40.6  50.5  60.4  70.3  80.2  90.1 100.0
@SebKrantz
Copy link
Owner

Thanks, yeah this is strange. Looks like something is rounded off somewhere where it shouldn't. In general, the algorithm is a bit different from Hmisc, so the results are not identical by design, but this is of course a bug.

SebKrantz added a commit that referenced this issue Jan 11, 2024
@SebKrantz
Copy link
Owner

This has been fixed now in 2.0.9. Thanks for flagging. Will send it to CRAN today.

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