R语言二项分布

二项分布模型用来处理在一系列实验中只发现两个可能结果的事件的成功概率。 例如,掷硬币总是两种结果:正面或反面。使用二项式分布估算在重复抛掷硬币10次时正好准确地找到3次是正面的概率。

R具有四个内置函数来生成二项分布,它们在下面描述。

dbinom(x, size, prob)
pbinom(x, size, prob)
qbinom(p, size, prob)
rbinom(n, size, prob)

以下是使用的参数的描述 –

  • x – 是数字的向量。
  • p – 是概率向量。
  • n – 是观察次数。
  • size – 是试验的次数。
  • prob – 是每次试验成功的概率。

1.dbinom()函数

该函数给出了每个点的概率密度分布。参考以下代码实现 –

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()

当我们执行上述代码时,会产生以下结果 –

《R语言二项分布》

2.pbinom()函数

该函数给出事件的累积概率,它用于表示概率的单个值。参考以下代码实现 –

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)

当我们执行上述代码时,会产生以下结果 –

[1] 0.610116

3.qbinom()函数

该函数采用概率值,并给出其累积值与概率值匹配的数字。参考以下代码实现 –

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)

当我们执行上述代码时,会产生以下结果 –

[1] 23

4.rbinom()函数

该函数从给定样本生成所需数量的给定概率的随机值。参考以下代码实现 –

# Find 8 random values from a sample of 150 with probability of 0.4.
x <- rbinom(8,150,.4)

print(x)

当我们执行上述代码时,会产生以下结果 –

[1] 58 61 59 66 55 60 61 67

        原文作者:R语言教程
        原文地址: https://www.yiibai.com/r/r_binomial_distribution.html
        本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
    点赞