[英]How to calculate PMF of sample median for discrete random variable in R?
一个袋子里有 5 个台球,编号分别为 1、2、3、4、5。从袋子中随机抽取大小为 n = 3 的样本,不放回原样。 样本中位数的概率质量 function 是多少?
这是我所拥有的:
库(listviewer)sampleSpaceAndMedian = list()
# the random samples (1,2,3), (1,3,2), (2,1,3),
# (2,3,1), (3,1,2), and (3,2,1) have the same mean
# therefore, belong to the same equivalence class
for (a in 1:3){
for (b in 2:4){
for (c in 3:5){
# a unique random sample of size 3 (ignores the order)
if (b > a && c > b){
tString = paste(toString(a), toString(b), toString(c), toString(median(c(a,b,c))), sep = " ")
sampleSpaceAndMedian <- append(sampleSpaceAndMedian, tString)
}
}
}
}
# the random sample is in the first three columns
# median is the fourth column
jsonedit( sampleSpaceAndMedian )
```
你能帮我拿到PMF吗? 谢谢。
您可以使用combn
获取向量的所有组合,并将 function 应用于它:
combn(1:5, 3)
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#[1,] 1 1 1 1 1 1 2 2 2 3
#[2,] 2 2 2 3 3 4 3 3 4 4
#[3,] 3 4 5 4 5 5 4 5 5 5
要获得median
的分布,您可以使用以下命令:
prop.table(table(combn(1:5, 3, median)))
#> 2 3 4
#>0.3 0.4 0.3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.