假设我多次抛掷 10 个骰子(比如说 10000 次),我想知道 10 个骰子的总和落入某个范围(10 到 20、20 到 30、...、50 到 60)的概率。如何将值划分为范围并计算每个范围的概率? 以下是我到目前为止所拥有的:
K=10
all_comb = rep(list(1:6),K) # set up the dice
dice <- expand.grid(all_comb) #list all possible combinations
dice.sums <- rowSums(dice) #calculate sum of each combination
all_prob = c() #calculate the probability of each possible sum (from 10 to 60)
for (i in 1:(60-10+1) ){
a = mean( dice.sums == c(10:60)[i] )
all_prob = c(all_prob,a)
}
print(all_prob)
我希望有人能告诉我该怎么做。非常感谢!
如果您创建
table
的 dice.sums
,那么您将获得每个可能和的频率。将此表除以总和即可得出每个总和的概率。剩下的只是数据整理以将其转换为正确的格式,对此 dplyr
将很有用:
library(dplyr)
table(dice.sums) %>%
as.data.frame() %>%
mutate(dice.sums = as.numeric(as.character(dice.sums)),
prob = Freq/sum(Freq),
floor = 10 * floor(dice.sums/10)) %>%
summarise(range = paste(min(dice.sums), max(dice.sums), sep = ' - '),
prob = round(sum(prob), 8), .by = floor) %>%
select(range, prob)
#> range prob
#> 1 10 - 19 0.00148046
#> 2 20 - 29 0.15502340
#> 3 30 - 39 0.63852791
#> 4 40 - 49 0.20207825
#> 5 50 - 59 0.00288996
#> 6 60 - 60 0.00000002