rstatisticslinear-regressionnumerical-methodsquantile

Finding quantile numerically in R


I'm looking for an R code that would help me find the quantile when the pdf is given numerically. That is, say my data is

x = c(0.00,0.05,0.10,0.15,0.20,0.25,0.30,0.35,0.40,0.45,0.50,0.55,0.60,0.65,0.70,0.75,0.80,0.85,0.90,0.95,1.00)

and the corresponding pdf values are

pdf = c(0.000000000,1.221759375,1.968300000,2.349028125,2.457600000,2.373046875,2.160900000,1.874315625,1.555200000,1.235334375,0.937500000,0.676603125,0.460800000,0.292621875,0.170100000,0.087890625,0.038400000,0.012909375,0.002700000,0.000178125,0.000000000)

I'd like to find the 95th quantile. I found an example here, however, the pdf is considered to be a function in this example, which is not my case. Thank you ahead for any help.


Solution

  • You can calculate an approximate 95%-quantile according to the definition of quantile, e.g.,

    > max(x[cumsum(pdf) <= 0.95 * sum(pdf)])
    [1] 0.55
    

    Or, you can try approxfun like below

    > y <- cumsum(pdf) / sum(pdf)
    
    > k <- !duplicated(y)
    
    > approxfun(y[k],x[k])(0.95)
    [1] 0.5577952
    

    Below is the visualization

    plot(x[k], y[k], type = "b")
    abline(v = q)
    grid(nx = 19, ny = 19, lwd = 2)
    

    enter image description here