# R正态分布示例图解

## 本文概述

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

S.No Parameter Description
1. x 它是数字的向量。
2. p 它是概率的向量。
3. n 它是观察的载体。
4. mean 它是样本数据的平均值, 其默认值为零。
5. sd 这是标准偏差, 其默认值为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()``````

• 回顶