Skip to content
Prev 369826 / 398506 Next

"reverse" quantile function

It would depend on which one of the 9 quantile definitions you are using. The discontinuous ones aren't invertible, and the continuous ones won't be either, if there are ties in the data. 

This said, it should just be a matter of setting up the inverse of a piecewise linear function. To set ideas, try 

x <- rnorm(5)
curve(quantile(x,p), xname="p")

The breakpoints for the default quantiles are n points evenly spread on [0,1], including the endpoints; i.e., for n=5, (0, .25, .5, .75, 1) 

So:

x <- rnorm(5)
br <- seq(0, 1, ,5)
qq <- quantile(x, br) ## actually == sort(x)

pfun <- approxfun(qq, br)
(q <- quantile(x, .1234))
pfun(q)


There are variations, e.g. the one-liner

approx(sort(x), seq(0,1,,length(x)), q)$y

-pd