如何计算 R 中给定分位数的概率?

发布于 2025-01-01 13:51:15 字数 557 浏览 0 评论 0原文

使用 R,计算采样分布中给定概率的分位数是微不足道的:

x <- rnorm(1000, mean=4, sd=2)
quantile(x, .9) # results in 6.705755

但是,我找不到一种简单的方法来进行逆运算 - 计算样本 x。我最接近的是使用 pnorm() ,其平均值和标准差与创建样本时使用的相同:

pnorm(5, mean=4, sd=2) # results in 0.6914625

但是,因为这是根据完整正态分布计算概率,而不是从示例x,它并不完全准确。

是否有一个函数本质上执行 quantile() 的逆操作?本质上可以让我做与 pnorm() 相同的事情,但有一个示例?像这样的事情:

backwards_quantile(x, 5)

我找到了 ecdf() 函数,但无法找到一种方法使其产生单个概率而不是完整的方程对象。

Using R, it is trivial to calculate the quantiles for given probabilities in a sampled distribution:

x <- rnorm(1000, mean=4, sd=2)
quantile(x, .9) # results in 6.705755

However, I can't find an easy way to do the inverse—calculate the probability for a given quantile in the sample x. The closest I've come is to use pnorm() with the same mean and standard deviation I used when creating the sample:

pnorm(5, mean=4, sd=2) # results in 0.6914625

However, because this is calculating the probability from the full normal distribution, and not the sample x, it's not entirely accurate.

Is there a function that essentially does the inverse of quantile()? Something that essentially lets me do the same thing as pnorm() but with a sample? Something like this:

backwards_quantile(x, 5)

I've found the ecdf() function, but can't figure out a way to make it result in a single probability instead of a full equation object.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦过后 2025-01-08 13:51:15

ecdf 返回一个函数:您需要应用它。

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

ecdf returns a function: you need to apply it.

f <- ecdf(x)
f( quantile(x,.91) )
# Equivalently:
ecdf(x)( quantile(x,.91) )
青芜 2025-01-08 13:51:15

你自己或多或少已经有了答案。当你想写时,

backwards_quantile(x, 5)

只需写

ecdf(x)(5)

这对应于 type=1 的 quantile() 的逆。然而,如果你想要其他类型(我赞成NIST标准,对应Excel的Percentile.exc,即type=6),你还有更多的工作要做。

在后一种情况下,请考虑您要将其用于哪种用途。例如,如果您想要的只是绘制它,那么请考虑

yVals<-seq(0,1,0.01)
plot(quantile(x,yVals,type=6))

但是,如果您想要单个值(例如 5)的倒数,那么您需要编写一个求解函数来找到使得的 P

quantile(x,P,type=6) = 5

例如,它使用二分搜索x 的极值之间:

inverse_quantile<-function(x,y,d=0.01,type=1) {
  A<-min(x)
  B<-max(x)
  k<-(log((B-A)/d)/log(2))+1
  P=0.5
  for (i in 1:k) {
    P=P+ifelse((quantile(x,P,type=type)<y),2^{-i-1},-2^{-i-1})
  }
  P
}

因此,如果您想要数字 5 的集合 x 的类型 4 分位数,精度为 0.00001,那么您可以写

inverse_quantile<-function(x,5,d=0.00001,type=4)

You more or less have the answer yourself. When you want to write

backwards_quantile(x, 5)

just write

ecdf(x)(5)

This corresponds to the inverse of quantile() with type=1. However, if you want other types (I favour the NIST standard, corresponding to Excel's Percentile.exc, which is type=6), you have more work to do.

In these latter cases, consider which use you are going to put it to. If all you want is to plot it, for instance, then consider

yVals<-seq(0,1,0.01)
plot(quantile(x,yVals,type=6))

But if you want the inverse for a single value, like 5, then you need to write a solving function to find the P that makes

quantile(x,P,type=6) = 5

For instance this, which uses binary search between the extreme values of x:

inverse_quantile<-function(x,y,d=0.01,type=1) {
  A<-min(x)
  B<-max(x)
  k<-(log((B-A)/d)/log(2))+1
  P=0.5
  for (i in 1:k) {
    P=P+ifelse((quantile(x,P,type=type)<y),2^{-i-1},-2^{-i-1})
  }
  P
}

So if you wanted the type 4 quantile of your set x for the number 5, with precision 0.00001, then you would write

inverse_quantile<-function(x,5,d=0.00001,type=4)
棒棒糖 2025-01-08 13:51:15

只是为了方便起见,此功能可以帮助:

quantInv <- function(distr, value) ecdf(distr)(value)
set.seed(1)
x <- rnorm(1000, mean=4, sd=2)
quantInv(x, c(4, 5, 6.705755))
[1] 0.518 0.685 0.904

Just for convenience, this function helps:

quantInv <- function(distr, value) ecdf(distr)(value)
set.seed(1)
x <- rnorm(1000, mean=4, sd=2)
quantInv(x, c(4, 5, 6.705755))
[1] 0.518 0.685 0.904
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文