R语言二项分布
二项式分布模型涉及寻找具有在一系列实验中只有两种可能的结果的事件的成功的概率。对于一个硬币的例子折腾总是给出一个正面或反面。发现正是3个正面,在反复掷硬币10次的概率是二项分布估计的期间。
dbinom(x, size, prob)
pbinom(x, size, prob)
qbinom(p, size, prob)
rbinom(n, size, prob)
以下是所使用的参数的说明:
x 是数字向量。
p 是概率的向量。
n 是观测次数。
size 是试验次数。
prob 是每次试验的成功概率。
dbinom()
此函数可以让每个点显示的概率密度分布。
# 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()
当我们上面的代码执行时,它产生以下结果:
pbinom()
此函数给出了一个事件的累积概率。它是表示单个值的概率。
# 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
qbinom()
这个函数的概率值,并给出了一个数字,其累加值相匹配概率值。
# 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
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
数据分析咨询请扫描二维码