r - 生成给定百分位数的分布

我想在给定以下 score and percentile ranks 的情况下在 R 中生成一个分布.

x <- 1:10
PercRank <- c(1, 7, 12, 23, 41, 62, 73, 80, 92, 99)

PercRank = 1例如告诉 1% 的数据有 value/score <= 1 (x 的第一个值)。同样,PercRank = 7表明 7% 的数据具有 value/score <= 2等等。

我不知道如何找到底层分布。如果我能得到一些关于如何获得 pdf 的指导,我会很高兴。仅从这么多信息中得出的基础分布。

最佳答案

来自 Wikipedia :

The percentile rank of a score is the percentage of scores in its frequency distribution that are the same or lower than it.

为了说明这一点,让我们创建一个分布,比如说,正态分布,其中 mean=2sd=2,以便我们稍后可以测试(我们的代码)。

# 1000 samples from normal(2,2)
x1 <- rnorm(1000, mean=2, sd=2)

现在,让我们采用您在帖子中提到的相同 percentile rank。让我们将它除以 100,以便它们代表累积概率。

cum.p <- c(1, 7, 12, 23, 41, 62, 73, 80, 92, 99)/100

这些百分位数对应的值(scores)是多少?

# generating values similar to your x.
x <- c(t(quantile(x1, cum.p)))
> x
 [1] -2.1870396 -1.4707273 -1.1535935 -0.8265444 -0.2888791  
         0.2781699  0.5893503  0.8396868  1.4222489  2.1519328

这意味着 1% 的数据小于 -2.18。 7% 的数据小于 -1.47 等等...现在,我们有 xcum.p(相当于您的 PercRank ).让我们忘记 x1 以及这应该是正态分布的事实。为了找出它可能是什么分布,让我们通过使用 diff 从累积概率中获取实际概率,它采用第 n 个和第 (n-1) 个元素之间的差异。

prob <- c( cum.p[1], diff(cum.p), .01)
> prob
# [1] 0.01 0.06 0.05 0.11 0.18 0.21 0.11 0.07 0.12 0.07 0.01

现在,我们所要做的就是为 x (x[1]:x[2], x[2 ]:x[3] ...) 然后最终从这个庞大的数据中抽取所需数量的点(比如 10000),概率如上所述。

这可以通过以下方式完成:

freq <- 10000 # final output size that we want

# Extreme values beyond x (to sample)
init <- -(abs(min(x)) + 5) 
fin  <- abs(max(x)) + 5

ival <- c(init, x, fin) # generate the sequence to take pairs from
len <- 100 # sequence of each pair

s <- sapply(2:length(ival), function(i) {
    seq(ival[i-1], ival[i], length.out=len)
})
# sample from s, total of 10000 values with probabilities calculated above
out <- sample(s, freq, prob=rep(prob, each=len), replace = T)

现在,我们从分布中获得了 10000 个样本。让我们看看它是怎样的。它应该类似于均值 = 2 和 sd = 2 的正态分布。

> hist(out)

> c(mean(out), sd(out))
# [1] 1.954834 2.170683

这是一个正态分布(来自直方图),mean = 1.95sd = 2.17 (~ 2)

注意:我所解释的一些事情可能是迂回的和/或代码“可能/可能不”适用于其他一些发行版。这篇文章的目的只是用一个简单的例子来解释这个概念。

编辑:为了阐明@Dwin 的 点,我尝试使用与 OP 的问题对应的 x = 1:10 相同的代码, 用相同的代码替换 x 的值。

cum.p <- c(1, 7, 12, 23, 41, 62, 73, 80, 92, 99)/100
prob <- c( cum.p[1], diff(cum.p), .01)
x <- 1:10

freq <- 10000 # final output size that we want

# Extreme values beyond x (to sample)
init <- -(abs(min(x)) + 1) 
fin  <- abs(max(x)) + 1

ival <- c(init, x, fin) # generate the sequence to take pairs from
len <- 100 # sequence of each pair

s <- sapply(2:length(ival), function(i) {
    seq(ival[i-1], ival[i], length.out=len)
})
# sample from s, total of 10000 values with probabilities calculated above
out <- sample(s, freq, prob=rep(prob, each=len), replace = T)

> quantile(out, cum.p) # ~ => x = 1:10
# 1%     7%    12%    23%    41%    62%    73%    80%    92%    99% 
# 0.878  1.989  2.989  4.020  5.010  6.030  7.030  8.020  9.050 10.010 

> hist(out)

https://stackoverflow.com/questions/14547364/

相关文章:

google-apps-script - 在电子表格的 App 脚本中选择连续范围

regex - 如何在 Perl 中的正则表达式匹配后打印行?

r - 如何按位置排除向量中的多个元素?

sql - Oracle 在 DBA_ROLES 上的选择权限

memory - 了解mmap

CSS - 向内联文本的第二行添加填充

jsf - 如何通过 primefaces 中的 beans 在 selectOneRadio/se

svg - 如何在 HTML 中呈现不同大小的 SVG?

jquery - 谷歌地图,基于 Zoom 的比例圆

ruby-on-rails - 即使表中有用户名,也没有用户名字段