Survey.jl icon indicating copy to clipboard operation
Survey.jl copied to clipboard

Weighted `quantile` implementation

Open smishr opened this issue 2 years ago • 5 comments

Currently, Statistics has all the Hyndman & Fan rules, and Statsbase has some rudimentary rule for Weighted (which I have been unable to classify), but from what I could find, those formulae under the "Weighted" column on the right hand side of the table are not implemented in Julia yet. They are straightforward to implement. Later, we could look at encapsulating parts of the code into Statsbase quantile image

Except for simple random samples when qrule="hf7", our quantile function is incorrect. Weighting logic needs to be applied.

smishr avatar Dec 08 '22 07:12 smishr

qrule function in R, which applies the given qrule, eg "hf7" by default.

function (x, w, p) 
{
  if (any(zero <- w == 0)) {
    w <- w[!zero]
    x <- x[!zero]
  }
  n <- length(x)
  ii <- order(x)
  x <- x[ii]
  cumw <- cumsum(w[ii])
  pk <- c(0, cumw[-n])/cumw[n - 1]
  approx(pk, x, p, method = "linear", rule = 2)$y
}

smishr avatar Dec 08 '22 08:12 smishr

Related to #87

smishr avatar Dec 08 '22 13:12 smishr

See R survey implementation and logic of all the qrules. It should be relatively easy to translate the logic into Julia.

smishr avatar Feb 15 '23 08:02 smishr

See https://docs.julialang.org/en/v1/stdlib/Statistics/ quantile and quantile! functions for unweighted quantile definitions. Using parameters alpha and beta, can convert into weighted by multiplying by cdf and sum cdf

smishr avatar Feb 19 '23 05:02 smishr

@nadiaenh this is also a good PR to work on to build understanding and skills for the project. The R survey qrule.R is relatively easy to understand and implement in Julia

smishr avatar Feb 27 '23 07:02 smishr