R语言----(标准正态&二项)分布

标准分布也即是:正态分布。 在来自独立源的数据的随机集合中,通常观察到数据的分布是正态的。
在图中,曲线的中心表示数据集的平均值。 50%的值位于平均值的左侧,另外50%位于图表的右侧。

正态分布

开始前先附一篇下述四个函数的统计学讲解,个人感觉较为详细http://www.360doc.com/content/18/0913/18/19913717_786412696.shtml

R语言有四个内置函数来产生正态分布:

dnorm(x, mean, sd)
pnorm(x, mean, sd)
qnorm(p, mean, sd)
rnorm(n, mean, sd)

以下是在上述功能中使用的参数的描述:

  • x是数字的向量。
  • p是概率的向量。
  • n是观察的数量(样本大小)。
  • mean是样本数据的平均值。 它的默认值为零。
  • sd是标准偏差。 它的默认值为1。

dnorm()

该函数给出给定平均值和标准偏差在每个点的概率分布的高度。
翻译一下就是:返回值是正态分布概率密度函数值,比如dnorm(z)则表示:标准正态分布密度函数f(x)在x=z处的函数值。

输入:

# Create a sequence of numbers between -10 and 10 incrementing by 0.1.
x <- seq(-10, 10, by = .1)

# Choose the mean as 2.5 and standard deviation as 0.5.
y <- dnorm(x, mean = 2.5, sd = 0.5)

# Give the chart file a name.
png(file = "dnorm.png")

plot(x,y)

# Save the file.
dev.off()

输出:
在这里插入图片描述

pnorm()

该函数给出正态分布随机数的概率小于给定数的值。 它也被称为“累积分布函数”。
翻译一下就是:返回值是正态分布的分布函数值,比如pnorm(z)等价于P[X ≤ z]

输入:

# Create a sequence of numbers between -10 and 10 incrementing by 0.2.
x <- seq(-10,10,by = .2)
 
# Choose the mean as 2.5 and standard deviation as 2. 
y <- pnorm(x, mean = 2.5, sd = 2)

# Give the chart file a name.
png(file = "pnorm.png")

# Plot the graph.
plot(x,y)

# Save the file.
dev.off()

输出:
在这里插入图片描述

qnorm()

该函数采用概率值,并给出累积值与概率值匹配的数字。
翻译一下就是:qnorm是正态分布累积分布函数的反函数,也就是说它可以视为pnorm的反函数,这里的q指的是quantile,即分位数。
使用qnorm这个函数可以回答这个问题:正态分布中的第p个分位数的Z-score是多少?

输入:

# Create a sequence of probability values incrementing by 0.02.
x <- seq(0, 1, by = 0.02)

# Choose the mean as 2 and standard deviation as 3.
y <- qnorm(x, mean = 2, sd = 1)

# Give the chart file a name.
png(file = "qnorm.png")

# Plot the graph.
plot(x,y)

# Save the file.
dev.off()

输出:
在这里插入图片描述

rnorm()

此函数用于生成分布正常的随机数。 它将样本大小作为输入,并生成许多随机数。

输入:

# Create a sample of 50 numbers which are normally distributed.
y <- rnorm(50)

# Give the chart file a name.
png(file = "rnorm.png")

# Plot the histogram for this sample.
hist(y, main = "Normal DIstribution")

# Save the file.
dev.off()

输出:

在这里插入图片描述

二项分布

二项分布模型处理在一系列实验中仅发现两个可能结果的事件的成功概率。 例如,掷硬币总是给出正面或反面(两个结果)。

R语言有四个内置函数来生成二项分布:

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

以下是所使用的参数的描述:

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

dbinom()

该函数给出每个点的概率密度分布。翻译一下就是:返回值是二项分布概率密度函数值,比如dbinom(z)则表示:二项分布密度函数f(x)在x=z处的函数值。

输入:

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

此函数给出事件的累积概率。 它是表示概率的单个值。
翻译一下就是:返回值是二项分布的分布函数值,比如pbinom(z)等价于P[X ≤ z]

例:从一枚硬币的51次投掷中,得到26个或更少的正面朝上的概率

输入:

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

该函数采用概率值,并给出累积值与概率值匹配的数字。
翻译一下就是:qbinom可以视为pbinom的反函数,这里的q指的是quantile,即分位数。
使用qbinom这个函数可以回答这个问题:二项分布中的第p个分位数的Z-score是多少?

例:当一枚硬币被掷51次时,有多少个硬币正面朝上的概率为0.25

输入:

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

输出:

58 61 59 66 55 60 61 67
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章