个性化阅读
专注于IT技术分析

R正态分布示例图解

点击下载

本文概述

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

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

R正态分布示例图解

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

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

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

dnorm():密度

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

R正态分布示例图解

例子

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

输出

R正态分布示例图解

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

输出

R正态分布示例图解

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

输出

R正态分布示例图解

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

输出

R正态分布示例图解

赞(0)
未经允许不得转载:srcmini » R正态分布示例图解

评论 抢沙发

评论前必须登录!