课程表

R语言 基础教程

R语言 图表

R语言 数据接口

R语言 统计示例

工具箱
速查手册

R语言 标准分布

当前位置:免费教程 » 程序设计 » R语言

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

R语言有四个内置函数来产生正态分布。 它们描述如下。

  1. dnorm(x, mean, sd)
  2. pnorm(x, mean, sd)
  3. qnorm(p, mean, sd)
  4. rnorm(n, mean, sd)

以下是在上述功能中使用的参数的描述 - 

  • x是数字的向量。

  • p是概率的向量。

  • n是观察的数量(样本大小)。

  • mean是样本数据的平均值。 它的默认值为零。

  • sd是标准偏差。 它的默认值为1。

dnorm()

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

  1. # Create a sequence of numbers between -10 and 10 incrementing by 0.1.
  2. x <- seq(-10, 10, by = .1)
  3.  
  4. # Choose the mean as 2.5 and standard deviation as 0.5.
  5. y <- dnorm(x, mean = 2.5, sd = 0.5)
  6.  
  7. # Give the chart file a name.
  8. png(file = "dnorm.png")
  9.  
  10. plot(x,y)
  11.  
  12. # Save the file.
  13. dev.off()

当我们执行上面的代码,它产生以下结果 -

dnorm()图

pnorm()

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

  1. # Create a sequence of numbers between -10 and 10 incrementing by 0.2.
  2. x <- seq(-10,10,by = .2)
  3. # Choose the mean as 2.5 and standard deviation as 2.
  4. y <- pnorm(x, mean = 2.5, sd = 2)
  5.  
  6. # Give the chart file a name.
  7. png(file = "pnorm.png")
  8.  
  9. # Plot the graph.
  10. plot(x,y)
  11.  
  12. # Save the file.
  13. dev.off()

当我们执行上面的代码,它产生以下结果 -

pnorm()图

qnorm()

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

  1. # Create a sequence of probability values incrementing by 0.02.
  2. x <- seq(0, 1, by = 0.02)
  3.  
  4. # Choose the mean as 2 and standard deviation as 3.
  5. y <- qnorm(x, mean = 2, sd = 1)
  6.  
  7. # Give the chart file a name.
  8. png(file = "qnorm.png")
  9.  
  10. # Plot the graph.
  11. plot(x,y)
  12.  
  13. # Save the file.
  14. dev.off()

当我们执行上面的代码,它产生以下结果 -

qnorm()图

RNORM()

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

  1. # Create a sample of 50 numbers which are normally distributed.
  2. y <- rnorm(50)
  3.  
  4. # Give the chart file a name.
  5. png(file = "rnorm.png")
  6.  
  7. # Plot the histogram for this sample.
  8. hist(y, main = "Normal DIstribution")
  9.  
  10. # Save the file.
  11. dev.off()

当我们执行上面的代码,它产生以下结果 -

RNORM()图
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号