当值具有不同的抽样概率时,计算中位数的最快方法是什么?

When values have different sampling probabilities, what is the fastest way to calculate the median?

考虑这个玩具示例:

一位老师想计算他 class 中学生的平均身高。但并非所有学生每天都出现 class,因此在任何给定的一天,计算出的中位数身高可能会有所不同。他们在 class 中的概率和他们的身高在下面的 table 中给出。根据这些信息,他可以估计预期的中位数。

>set.seed(123)
>data1 <- data.frame(Student=c(LETTERS[1:10]), Height.cm=sort( rnorm(n=10, mean=140, sd=10)), Prob.in.class=c(1,.75,1,.5,1,1,1,.25,1,.5))

>data1

   Student Height.cm Prob.in.class
1        A  127.3494          1.00
2        B  133.1315          0.75
3        C  134.3952          1.00
4        D  135.5434          0.50
5        E  137.6982          1.00
6        F  140.7051          1.00
7        G  141.2929          1.00
8        H  144.6092          0.25
9        I  155.5871          1.00
10       J  157.1506          0.50

在 R 中估计这样的分布的中位数(或任意分位数)的最快方法是什么?

对于我的实际计算,我需要估计数百个不同向量的中位数和任意分位数,每个向量有数万个点(和相关概率)。我见过这个 suggestion,其中概率密度函数是使用梯形法估计的,但我不确定这是最好的方法。

如果您能提供任何建议,我们将不胜感激。谢谢!

类似这样的方法应该可行,但要注意如下所示的权重向量

#your data
set.seed(123)
data1 <- data.frame(Student=c(LETTERS[1:10]), Height.cm=sort( rnorm(n=10, mean=140, sd=10)), Prob.in.class=c(1,.75,1,.5,1,1,1,.25,1,.5))

#Test a known ...
data2 <- c(1,1,1,1,1,2,3,3,3,3,3) # median clearly 2
median(data2) #yields 2, yah... 

#using weights... median should be 2 if function working right
data3 <- data.frame(Student=c(LETTERS[1:3]), Height.cm=c(1,2,3), Prob.in.class=c(5/12,2/12,5/12))
reldist::wtd.quantile(data3$Height.cm, q = .5, 
                  weight = data3$Prob.in.class) # yields 3, not the right answer

#the wtd.quantile function does not like probabilities. 
#multiply the weights to something greater than 1 seems to work. 
reldist::wtd.quantile(data3$Height.cm, q = .5, weight = data3$Prob.in.class*100) # yields 2, the right answer