Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a quantile function given a discrete CDF in R that can handle

Tags:

r

cdf

So for example, I have a discrete function with a CDF as follows:

cdf <- c(0.00, 0.35, 0.71, 0.92, 1.00, 1.00, 1.00, 1.00)

I can create a sort of quantile function with the following line . . .

result <- which(cdf == min(cdf[cdf > x]))

. . . where x is the cumulative probability. So for example, qfunction(0.9) = 4 and qfunction(0.99) = 5.

This solution appears fine (albeit inelegant) until I want to handle vectors. So if x = c(0.9, 0.99) my function falls over. This seems like something that people would do a lot of in R and yet I haven't found a solution. R is not my primary language.

Any help would be appreciated.

like image 692
timbo Avatar asked Mar 16 '23 05:03

timbo


1 Answers

You probably want the findInterval() function. See the ?findInerval help page for details about the function. But something like

findInterval(c(.9, .99), cdf)+1

should work for your sample data/input.

like image 149
MrFlick Avatar answered Apr 30 '23 18:04

MrFlick