R 简明教程

R - Binomial Distribution

二项分布模型用于找出某个事件在某系列实验中仅有两项可能结果的成功概率。例如,抛掷一枚硬币总是会得到正面或反面。估计二项分布期间重复抛掷一枚硬币 10 次中正好得到 3 次正面的概率。

The binomial distribution model deals with finding the probability of success of an event which has only two possible outcomes in a series of experiments. For example, tossing of a coin always gives a head or a tail. The probability of finding exactly 3 heads in tossing a coin repeatedly for 10 times is estimated during the binomial distribution.

R 有四个生成二项分布的内置函数。其描述如下。

R has four in-built functions to generate binomial distribution. They are described below.

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

以下是所用参数的描述 -

Following is the description of the parameters used −

  1. x is a vector of numbers.

  2. p is a vector of probabilities.

  3. n is number of observations.

  4. size is the number of trials.

  5. prob is the probability of success of each trial.

dbinom()

此函数给出了每个点的概率密度分布。

This function gives the probability density distribution at each point.

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

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

When we execute the above code, it produces the following result −

dbinom

pbinom()

此函数给出了事件的累积概率。它是一个表示概率的单一值。

This function gives the cumulative probability of an event. It is a single value representing the probability.

# Probability of getting 26 or less heads from a 51 tosses of a coin.
x <- pbinom(26,51,0.5)

print(x)

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

When we execute the above code, it produces the following result −

[1] 0.610116

qbinom()

此函数获取概率值并给出一个其累积值与概率值匹配的数字。

This function takes the probability value and gives a number whose cumulative value matches the probability value.

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

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

When we execute the above code, it produces the following result −

[1] 23

rbinom()

此函数从给定样本中生成给定概率所需数量的随机值。

This function generates required number of random values of given probability from a given sample.

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

print(x)

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

When we execute the above code, it produces the following result −

[1] 58 61 59 66 55 60 61 67