Discussion:
[R] "reverse" quantile function
David Winsemius
2017-06-15 22:46:36 UTC
Permalink
Dear All,
t<-seq(0,24,1)
a<-10*exp(-0.05*t)
b<-10*exp(-0.07*t)
c<-10*exp(-0.1*t)
d<-10*exp(-0.03*t)
z<-data.frame(a,b,c,d)
res<-t(apply(z, 1, quantile, probs=c(0.3)))
x <- rnorm(100)
quantile(x,0.33)
#so do this step
ecdf(x)(quantile(x,0.33))
#to get 0.33 back...
any suggestions on how I could to that for a data frame?
Can't you just used ecdf and quantile ecdf?

# See ?ecdf page for both functions
lapply( lapply(z, ecdf), quantile, 0.33)
$a
33%
4.475758

$b
33%
3.245151

$c
33%
2.003595

$d
33%
6.173204
--
David Winsemius
Alameda, CA, USA

______________________________________________
R-***@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
peter dalgaard
2017-06-16 08:58:10 UTC
Permalink
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
David,
res<-apply(z, 1, quantile, probs=c(0.3))
t<-seq(0,24,1)
a<-10*exp(-0.05*t)
b<-10*exp(-0.07*t)
c<-10*exp(-0.1*t)
d<-10*exp(-0.03*t)
z<-data.frame(a,b,c,d)
res<-c(10.000000, 9.296382, 8.642955, 8.036076 ,7.472374, 6.948723, 6.462233, 6.010223 ,5.590211
,5.199896 ,4.837147, 4.499989 ,4.186589, 3.895250 ,3.624397, 3.372570, 3.138415, 2.920675
, 2.718185 ,2.529864 ,2.354708, 2.191786, 2.040233, 1.899247, 1.768084)
res<-apply(z, 1, quantile, probs=c(0.3))...
k<-c(1:100)
f<-30
ecdf(k)(f)
would give us the value of 0.3... so same idea as this, but instead of "k" we have data frame "z", and instead of "f" we have "res", and need to find the value of 0.3... Does that make sense?
much appreciate the help...
Andras Farkas,
Dear All,
t<-seq(0,24,1)
a<-10*exp(-0.05*t)
b<-10*exp(-0.07*t)
c<-10*exp(-0.1*t)
d<-10*exp(-0.03*t)
z<-data.frame(a,b,c,d)
res<-t(apply(z, 1, quantile, probs=c(0.3)))
x <- rnorm(100)
quantile(x,0.33)
#so do this step
ecdf(x)(quantile(x,0.33))
#to get 0.33 back...
any suggestions on how I could to that for a data frame?
Can't you just used ecdf and quantile ecdf?
# See ?ecdf page for both functions
lapply( lapply(z, ecdf), quantile, 0.33)
$a
33%
4.475758
$b
33%
3.245151
$c
33%
2.003595
$d
33%
6.173204
--
David Winsemius
Alameda, CA, USA
______________________________________________
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: ***@cbs.dk Priv: ***@gmail.com

______________________________________________
R-***@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Loading...