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

So, for example, I have a discrete function with CDF like this:

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

      

I can create a 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 seems fine (albeit inelegant) until I handle vectors. So if x = c (0.9, 0.99) my function falls down. It looks like people would be doing a lot in R and yet I haven't found a solution. R is not my primary language.

Any help would be appreciated.

+3


source to share


1 answer


You probably want a function findInterval()

. For details on the function, see the help page ?findInerval

. But something like

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

      



should work for your sample data / input.

+6


source







All Articles