如何计算 R 中给定分位数的概率?
使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
ecdf
返回一个函数:您需要应用它。ecdf
returns a function: you need to apply it.你自己或多或少已经有了答案。当你想写时,
只需写
这对应于 type=1 的 quantile() 的逆。然而,如果你想要其他类型(我赞成NIST标准,对应Excel的Percentile.exc,即type=6),你还有更多的工作要做。
在后一种情况下,请考虑您要将其用于哪种用途。例如,如果您想要的只是绘制它,那么请考虑
但是,如果您想要单个值(例如 5)的倒数,那么您需要编写一个求解函数来找到使得的 P
例如,它使用二分搜索x 的极值之间:
因此,如果您想要数字 5 的集合 x 的类型 4 分位数,精度为 0.00001,那么您可以写
You more or less have the answer yourself. When you want to write
just write
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
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
For instance this, which uses binary search between the extreme values of x:
So if you wanted the type 4 quantile of your set x for the number 5, with precision 0.00001, then you would write
只是为了方便起见,此功能可以帮助:
Just for convenience, this function helps: