二項分布模型處理在一系列實驗中僅發(fā)現(xiàn)兩個可能結(jié)果的事件的成功概率。 例如,擲硬幣總是給出頭或尾。 在二項分布期間估計在10次重復拋擲硬幣中精確找到3個頭的概率。
R語言有四個內(nèi)置函數(shù)來生成二項分布。 它們描述如下。
dbinom(x, size, prob) pbinom(x, size, prob) qbinom(p, size, prob) rbinom(n, size, prob)
以下是所使用的參數(shù)的描述 -
x是數(shù)字的向量。
p是概率向量。
n是觀察的數(shù)量。
size是試驗的數(shù)量。
prob是每個試驗成功的概率。
該函數(shù)給出每個點的概率密度分布。
# 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()
當我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果 -
此函數(shù)給出事件的累積概率。 它是表示概率的單個值。
# Probability of getting 26 or less heads from a 51 tosses of a coin. x <- pbinom(26,51,0.5) print(x)
當我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果 -
[1] 0.610116
該函數(shù)采用概率值,并給出累積值與概率值匹配的數(shù)字。
# 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)
當我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果 -
[1] 23
該函數(shù)從給定樣本產(chǎn)生給定概率的所需數(shù)量的隨機值。
# Find 8 random values from a sample of 150 with probability of 0.4. x <- rbinom(8,150,.4) print(x)
當我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果 -
[1] 58 61 59 66 55 60 61 67
更多建議: