[R] "reverse" quantile function

Andras Farkas motyocska at yahoo.com
Fri Jun 16 01:56:28 CEST 2017

```David,

thanks for the response. In your response the quantile function (if I see correctly)  runs on the columns versus I need to run it on the rows, which is an easy fix, but that is not exactly what I had in mind... essentially we can remove t() from my original code to make "res" look like this:

res<-apply(z, 1, quantile, probs=c(0.3))

but after all maybe I did not explain myself clear enough so let me try again: the known variables to us in what I am trying to do are the data frame "z' :

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)

and the vector "res":

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)

and I need to find the probability (probs) , the unknown value, which would result in creating "res", ie: the probs=c(0.3), from:
res<-apply(z, 1, quantile, probs=c(0.3))...

a more simplified example assuming :

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,

On Thursday, June 15, 2017 6:46 PM, David Winsemius <dwinsemius at comcast.net> wrote:

> On Jun 15, 2017, at 12:37 PM, Andras Farkas via R-help <r-help at r-project.org> wrote:
>
> Dear All,
>
> we have:
>
> 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)))
>
>
>
> my goal is to do a 'reverse" of the function here that produces "res" on a data frame, ie: to get the answer 0.3 back for the percentile location when I have "res" available to me... For a single vector this would be done using ecdf something like this:
>
> x <- rnorm(100)
> #then I know this value:
> 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

```