📜  R正态分布

📅  最后修改于: 2021-01-08 10:06:08             🧑  作者: Mango

R正态分布

在从独立来源随机收集数据时,通常可以看到数据分布是正常的。这意味着,如果在水平轴上绘制变量值并在垂直轴上计数值的图形,则将得到钟形曲线。曲线中心代表数据集的平均值。在图中,百分之五十的值位于平均值的左侧。其余50%位于图表的右侧。这称为正态分布。

R允许我们通过提供以下函数来生成正态分布:

这些函数可以具有以下参数:

S.No Parameter Description
1. x It is a vector of numbers.
2. p It is a vector of probabilities.
3. n It is a vector of observations.
4. mean It is the mean value of the sample data whose default value is zero.
5. sd It is the standard deviation whose default value is 1.

让我们开始在示例的帮助下了解如何使用这些功能。

dnorm():密度

R的dnorm()函数针对给定的均值和标准差计算每个点处概率分布的高度。正态分布的概率密度为:

# Creating a sequence of numbers between -1 and 20 incrementing by 0.2.
x <- seq(-1, 20, by = .2)
# Choosing the mean as 2.0 and standard deviation as 0.5.
y <- dnorm(x, mean = 2.0, sd = 0.5)
# Giving a name to the chart file.
png(file = "dnorm.png")
#Plotting the graph
plot(x,y)
# Saving the file.
dev.off()

输出:

pnorm():直接查找

dnorm()函数也称为“累积分布函数”。此函数计算正态分布随机数的概率,该概率小于给定数的值。累积分布如下:

f(x)= P(X≤x)

# Creating a sequence of numbers between -1 and 20 incrementing by 0.2.
x <- seq(-1, 20, by = .1)
# Choosing the mean as 2.0 and standard deviation as 0.5.
y <- pnorm(x, mean = 2.0, sd = 0.5)
# Giving a name to the chart file.
png(file = "pnorm.png")
#Plotting the graph
plot(x,y)
# Saving the file.
dev.off()

输出:

qnorm():反向查找

qnorm()函数将概率值作为输入,并计算其累积值与概率值匹配的数字。累积分布函数和逆累积分布函数之间的关系如下

p = f(x)
x = f -1 (p)

# Creating a sequence of numbers between -1 and 20 incrementing by 0.2.
x <- seq(0, 1, by = .01)
# Choosing the mean as 2.0 and standard deviation as 0.5.
y <- qnorm(x, mean = 2.0, sd = 0.5)
# Giving a name to the chart file.
png(file = "qnorm.png")
#Plotting the graph
plot(y,x)
# Saving the file.
dev.off()

输出:

rnorm():随机变量

rnorm()函数用于生成正态分布的随机数。该函数通过将样本量作为输入来生成随机数。让我们看一个示例,在其中绘制直方图以显示生成的数字的分布。

# Creating a sequence of numbers between -1 and 20 incrementing by 0.2.
x <- rnorm(1500, mean=80,  sd=15 )
# Giving a name to the chart file.
png(file = "rnorm.png")
#Creating histogram
hist(x,probability =TRUE,col="red",border="black")
# Saving the file.
dev.off()

输出: