R语言正态分布

在随机收集来自独立来源的数据中,通常观察到数据的分布是正常的。 这意味着,在绘制水平轴上的变量的值和垂直轴中的值的计数时,我们得到一个钟形曲线。 曲线的中心代表数据集的平均值。 在图中,百分之五十的值位于平均值的左侧,另外五十分之一位于图的右侧。 统称为正态分布。

R有四个内置函数来生成正态分布。它们在下面描述 –

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

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

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

1.dnorm()函数

该函数给出给定平均值和标准偏差在每个点的概率分布的高度。

setwd("F:/worksp/R")
# 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()

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

《R语言正态分布》

2.pnorm()函数

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

setwd("F:/worksp/R")
# 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()

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

《R语言正态分布》

3.qnorm()函数

该函数采用概率值,并给出其累积值与概率值匹配的数字值。

setwd("F:/worksp/R")
# 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()

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

《R语言正态分布》

4.rnorm()函数

该函数用于生成分布正常的随机数,它将样本大小作为输入,并生成许多随机数。我们绘制直方图以显示生成数字的分布。

setwd("F:/worksp/R")
# 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 = "正态分布")

# Save the file.
dev.off()

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

《R语言正态分布》

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