[R] "reverse" quantile function
Andras Farkas
motyocska at yahoo.com
Fri Jun 16 11:29:46 CEST 2017
Peter,
thanks, very nice, this will work for me... could you also help with setting up the code to run the on liner "approx(sort(x), seq(0,1,,length(x)), q)$y" on the rows of a data frame using my example above? So if I cbind z and res,
df<-cbind(z,res)
the "x" in your one liner would be the first 4 column values of each row and "q" is the last (5fth) column value of each row..
thanks again for all the help, Andras Farkas
On Friday, June 16, 2017 4:58 AM, peter dalgaard <pdalgd at gmail.com> wrote:
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
> On 16 Jun 2017, at 01:56 , Andras Farkas via R-help <r-help at r-project.org> wrote:
>
> 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
>
> ______________________________________________
> R-help at 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, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
[[alternative HTML version deleted]]
More information about the R-help
mailing list