2017-04-17 12 views
1

このおもちゃの例を考えてみましょう:値のサンプリング確率が異なる場合、メディアンを計算する最も速い方法は何ですか?

教師は、そのクラスの生徒の中央値の高さを計算したいと考えています。しかし、すべての生徒が毎日クラスに出演するとは限らないので、任意の日に計算された中央高さは異なる可能性があります。授業中の可能性とその高さは下の表の通りです。この情報が与えられれば、彼は期待される中央値を推定することができた。

>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 

このような分布の中央値(または任意の分位数)を推定する最も早い方法は何ですか?

私の実際の計算では、何百もの点(および関連する確率)を持つ何百もの異なるベクトルのメジアンと任意のクォンタイルを推定する必要があります。私はこれを見てきましたsuggestion確率密度関数は台形法を使って推定されますが、これが最良の方法であるかどうかはわかりません。

アドバイスをいただければ幸いです。ありがとう!このような

+0

いいえ、私が思うに、この(加重分位)の下に示すように、重みベクトルに注意しなければなら結構です。 Googling "weighted quantile r"、https://artax.karlin.mff.cuni.cz/r-help/library/reldist/html/wtd.quantile.htmlまたはhttp://artax.karlin.mff.cuniについて.cz/r-help/library/PSCBS/html/weightedQuantile.htmlまたはhttps://github.com/hadley/bigvis/blob/master/R/weighted-stats.r?あなたはこれらのソリューションのいくつかをベンチマークすることができます... –

+0

正しい方向@BenBolkerで私を指してくれてありがとう。 'PSCBS :: weightedQuantile'と' reldis :: wtd.quantile'のように見えるので、 'Hmisc :: wtd.quantile'を使うだけですので、元のままにしておきます。 – Ricola

答えて

1

何かが動作しますが、

#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 
関連する問題