How to Calculate the Probability for a Given Quantile in R

How do I calculate the probability for a given quantile in R?

ecdf returns a function: you need to apply it.

f <- ecdf(x)
f( quantile(x,.91) )
# Equivalently:
ecdf(x)( quantile(x,.91) )

Calculate probability from a given quantile

In ?quantile we can read

See Also

ecdf for empirical distributions of which quantile is an inverse

Thus:

x <- rnorm(100)

r <- quantile(x, 0.05)
# 5%
#-1.469996

Fn <- ecdf(x)
Fn(r)
#[1] 0.05

find the quantile percentage given a value

If x is the vector, and x0 is the value you want to check, we can use

match(x0, sort(x)) / (length(x) + 1)

For your example:

x <- c(1,2,3,4,5,6,3,2,21,22,23,2,23,4,4,4444)
match(3, sort(x)) / (length(x) + 1)
# [1] 0.2941176

how to determine the percentile that a given value has in a sample in R

To clarify the useful link reference of Taufi, you can assign a variable to the ecdf function and use that for single or vector calculations of the inverse value. E.g.

cdf <- ecdf(x)
cdf(35)
[1] 0.6
allx <- cdf(x)
allx
[1] 0.16 0.24 0.84 0.92 0.08 0.60 0.24 0.60 0.08 0.44 0.32 0.64 0.96 0.16 0.48 0.40 0.32 0.72 0.76
[20] 1.00 0.36 0.68 0.52 0.84 0.88
cbind(x, allx)
x allx
[1,] 2 0.16
[2,] 3 0.24
[3,] 68 0.84
[4,] 253 0.92
[5,] 1 0.08
[6,] 35 0.60
[7,] 3 0.24
[8,] 35 0.60
[9,] 1 0.08
[10,] 24 0.44
[11,] 4 0.32
[12,] 36 0.64
[13,] 254 0.96
[14,] 2 0.16
[15,] 28 0.48
[16,] 12 0.40
[17,] 4 0.32
[18,] 54 0.72
[19,] 66 0.76
[20,] 775 1.00
[21,] 6 0.36
[22,] 45 0.68
[23,] 33 0.52
[24,] 68 0.84
[25,] 71 0.88

How can I specify probability values when invoking quantiles within apply in R?

You can pass arbitrary probability values to the probs parameter, for example, if you have a random data frame:

input.dat
A B C D
1 78 12 43 12
2 23 12 42 13
3 14 42 11 99
4 49 94 27 72

apply(input.dat, 2, quantile, probs = c(0.2, 0.4, 0.6, 0.8))
A B C D
20% 19.4 12.0 20.6 12.6
40% 28.2 18.0 30.0 24.8
60% 43.8 36.0 39.0 60.2
80% 60.6 62.8 42.4 82.8

How to calculate probability that normal distribution is greater or equal to some value in R?

The normal distribution is a continuous distribution. Therefore the probability of being greater than x and the probability of being greater than or equal to x are the same (similarly the probability of being less than x and the probability of being less than or equal to x are the same)

Therefore

# P(X > x) = P(X >= x)
1 - pnorm(x)

# or
pnorm(x, lower.tail=FALSE)

Both of these are equivalent (however they may occasionally provide different answers due to the numerical solver).

Determine a normal distribution given its quantile information

A general procedure for Normal distribution

Suppose we have a Normal distribution X ~ N(mu, sigma), with unknown mean mu and unknown standard deviation sigma. And we aim to solve for mu and sigma, given two quantile equations:

Pr(X < q1) = alpha1
Pr(X < q2) = alpha2

We consider standardization: Z = (X - mu) / sigma, so that

Pr(Z < (q1 - mu) / sigma) = alpha1
Pr(Z < (q2 - mu) / sigma) = alpha2

In other words,

(q1 - mu) / sigma = qnorm(alpha1)
(q2 - mu) / sigma = qnorm(alpha2)

The RHS is explicitly known, and we define beta1 = qnorm(alpha1), beta2 = qnorm(alpha2). Now, the above simplifies to a system of 2 linear equations:

mu + beta1 * sigma = q1
mu + beta2 * sigma = q2

This system has coefficient matrix:

1  beta1
1 beta2

with determinant beta2 - beta1. The only situation for singularity is beta2 = beta1. As long as the system is non-singular, we can use solve to solve for mu and sigma.

Think about what the singularity situation means. qnorm is strictly monotone for Normal distribution. So beta1 = beta2 is as same as alpha1 = alpha2. But this can be easily avoided as it is under your specification, so in the following I will not check singularity.

Wrap up above into an estimation function:

est <- function(q, alpha) {
beta <- qnorm(alpha)
setNames(solve(cbind(1, beta), q), c("mu", "sigma"))
}

Let's have a test:

x <- est(c(158, 168), c(0.025, 0.975))
# mu sigma
#163.000000 2.551067

## verification
qnorm(c(0.025, 0.975), x[1], x[2])
# [1] 158 168

We can also do something arbitrary:

x <- est(c(1, 5), c(0.1, 0.4))
# mu sigma
#5.985590 3.890277

## verification
qnorm(c(0.1, 0.4), x[1], x[2])
# [1] 1 5

Is there a quantile function in R that includes both given x and given probs?

I think you just want to do a linear interpolation. For example,

dat <- structure(list(probs = c(0.06, 0.1, 0.2, 0.24, 0.3, 0.5, 0.7, 
0.89, 1), x = c(-120, -100, -97, -90, -80, -70, -60, -50, -40
)), class = "data.frame", row.names = c(NA, -9L))

fn <- approxfun(dat$probs, dat$x)
fn(c(0.1, 0.15, 0.2))
#> [1] -100.0 -98.5 -97.0

Created on 2022-02-15 by the reprex package (v2.0.1.9000)



Related Topics



Leave a reply



Submit