二項(xiàng)分布模型用來處理在一系列實(shí)驗(yàn)中只發(fā)現(xiàn)兩個可能結(jié)果的事件的成功概率。 例如,擲硬幣總是兩種結(jié)果:正面或反面。使用二項(xiàng)式分布估算在重復(fù)拋擲硬幣10次時正好準(zhǔn)確地找到3次是正面的概率。
R具有四個內(nèi)置函數(shù)來生成二項(xiàng)分布,它們在下面描述。
dbinom(x, size, prob)
pbinom(x, size, prob)
qbinom(p, size, prob)
rbinom(n, size, prob)
以下是使用的參數(shù)的描述 -
該函數(shù)給出了每個點(diǎn)的概率密度分布。參考以下代碼實(shí)現(xiàn) -
setwd("F:/worksp/R")
# Create a sample of 50 numbers which are incremented by 1.
x <- seq(0,50,by = 1)
# Create the binomial distribution.
y <- dbinom(x,50,0.5)
# Give the chart file a name.
png(file = "dbinom.png")
# Plot the graph for this sample.
plot(x,y)
# Save the file.
dev.off()
當(dāng)我們執(zhí)行上述代碼時,會產(chǎn)生以下結(jié)果 -

該函數(shù)給出事件的累積概率,它用于表示概率的單個值。參考以下代碼實(shí)現(xiàn) -
setwd("F:/worksp/R")
# Probability of getting 26 or less heads from a 51 tosses of a coin.
x <- pbinom(26,51,0.5)
print(x)
當(dāng)我們執(zhí)行上述代碼時,會產(chǎn)生以下結(jié)果 -
[1] 0.610116
該函數(shù)采用概率值,并給出其累積值與概率值匹配的數(shù)字。參考以下代碼實(shí)現(xiàn) -
setwd("F:/worksp/R")
# How many heads will have a probability of 0.25 will come out when a coin is tossed 51 times.
x <- qbinom(0.25,51,1/2)
print(x)
當(dāng)我們執(zhí)行上述代碼時,會產(chǎn)生以下結(jié)果 -
[1] 23
該函數(shù)從給定樣本生成所需數(shù)量的給定概率的隨機(jī)值。參考以下代碼實(shí)現(xiàn) -
# Find 8 random values from a sample of 150 with probability of 0.4.
x <- rbinom(8,150,.4)
print(x)
當(dāng)我們執(zhí)行上述代碼時,會產(chǎn)生以下結(jié)果 -
[1] 58 61 59 66 55 60 61 67