R 简明教程

R - Normal Distribution

在来自独立来源的随机数据集合中,通常观察到数据的分布是正态的。这意味着,通过绘制一个图形,其中水平轴为变量值,垂直轴为值计数,我们可以得到一个钟形曲线。曲线的中心表示数据集的均值。在图形中,50% 的值位于均值的左侧,而其他 50% 位于图形右侧。在统计中,这称为正态分布。

R 具有四个内置函数用于生成正态分布。它们在下方进行了描述。

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

以下是上面函数中使用的参数的描述 -

  1. x 是数字向量。

  2. p 是概率向量。

  3. n 是观测数(样本量)。

  4. mean 是样本数据的均值。其默认值为 0。

  5. sd 是标准差。其默认值为 1。

dnorm()

此函数针对给定的均值和标准差提供每个点的概率分布高度。

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

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

dnorm

pnorm()

此函数给出了正态分布随机数小于给定数字值的概率。它也称为 “累积分布函数”。

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

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

pnorm

qnorm()

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

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

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

qnorm

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

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

rnorm