簡體   English   中英

在R中繪制一個二項分布表

[英]Plot a table of binomial distributions in R

對於游戲設計問題,我需要更好地檢查二項式分布。 使用R ,我需要構建一個二維表-給定固定參數'pool'(擲出的骰子數量),'sides'(骰子的側面數)具有:

  • 行->成功的最小值(從0到兩邊,是離散分布)
  • 在列中->成功次數(從0到池)

我知道如何將其作為單個任務進行計算,但是我不確定如何迭代來填充整個表

編輯:我忘了說我想計算至少獲得成功次數的概率p。

這使您成功了一半。
您還沒有真正展示過完成的工作,但是如果您不熟悉R ,那么您可能會錯失一個事實,即一個非常強大的功能是可以將值的向量用作另一個向量的索引。 這使問題的一部分變得輕而易舉。

pool <- 3
sides <- 20  # <cough>D&D<cough>


# you need to strore the values somewhere, use a vector
NumberOfRollsPerSide <- rep(0, sides)
names(NumberOfRollsPerSide) <- 1:sides # this will be useful in table

## Repeast so long as there are still zeros
##     ie, so long as there is a side that has not come up yet
while (any(NumberOfRollsPerSide == 0)) {
  # roll once
  oneRoll <- sample(1:sides, pool, TRUE)  

  # add (+1) to each sides' total rolls
  #  note that you can use the roll outcome to index the vector. R is great. 
  NumberOfRollsPerSide[oneRoll] <- NumberOfRollsPerSide[oneRoll] + 1
}

# These are your results: 
  NumberOfRollsPerSide

我希望這不是您的功課;)現在,您剩下要做的就是計算每一面首先出現的卷號。 您認為最好的做法是什么?

好的,我認為這可能是一個簡單的解決方案。 它在行上具有成功率,在列上具有骰子滾動(p)的成功閾值。

poolDistribution <- function(n, sides=10, digits=2, roll.Under=FALSE){
  m <- 1:sides
  names(m) <- paste(m,ifelse(roll.Under,"-", "+"),sep="")
  s <- 1:n
  names(s) <- paste(s,n,sep="/")
  sapply(m, function(m.value) round((if(roll.Under) (1 - pbinom(s - 1, n, (m.value)/sides))*100 else (1 - pbinom(s - 1, n, (sides - m.value + 1)/sides))*100), digits=digits))  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM