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)
```

- Installing R on Linux: configure: error: libcurl >= 7.28.0 library and headers are required with support for https
- How to do ensembles with time series using AICc?
- planes3d expands and draws the area based on the sphere's radius
- How to extract tag code itself using R, rvest
- How to Display or Print Contents of Environment in R
- How to use Windows user credentials for proxy authentication in R/RStudio
- R reticulate specifying python executable to use
- Replace multiple Instances of a variable name in an R function and save the modified function
- Standardizing address formatting in R
- How to fix "failed to load cairo DLL" in R?
- Using grepl to filter columns names in specific range of columns
- changing the legends in ggplot2 to have groups of similar labels
- How to keep only unique rows but ignore a column?
- convert string date to R Date FAST for all dates
- Add subgroup text to plotly pie chart
- R Shiny : adjust height of DT datatable when fillContainer=TRUE,
- Why do R external pointers' "unusual copying semantics" mean they should not be used stand-alone?
- How to extract somo character after a string with a number of word which can change in R
- What does `se` stand for in geom_smooth(..., se = FALSE)?
- How to find number of rows greater than any values in R
- Align text and reduce space between text and parentheses in plotly hover info box
- Remove outer box of geom_bar plot with broken y-axis
- How to use lag/lead in mutate with an initial value?
- Is it possible to have a Shiny ConditionalPanel whose condition is a global variable?
- counting elements in one list in another list
- How to vectorize nested loops in R?
- Replace NA values with an incrementing sequence starting from the previous non-NA value
- How can I calculate the number of uniques in a row within a species matrix?
- How to perform operations on pairs of rows, based on a "distinguishing" column's values
- Mutate variable based on previous observations