hist()中的密度/频率和概率 [英] density/frequency and probability in hist()

查看:454
本文介绍了hist()中的密度/频率和概率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用了代码

hist(x, probability=TRUE)

这给了我一个从0到2的y轴,名称为density.我不明白这是什么意思.它是否积分为1,总和为1,或者y值等于多少?文档说"freq = NULL,概率=!freq",但这对我来说没有意义.如果我不使用概率= TRUE,则会在y轴上获得频率,但是曲线图的形状是相同的.

which gives me a y-axis from 0 to 2 with the name density. I dont get what this means. Does it integrate to 1, sum to 1, or what is the y-value equal to? The documentation says "freq = NULL, probability = !freq" but that does not make sense to me. If I dont use probability=TRUE I get Frequency on the y-axis, but the shape of the plot is the same.

推荐答案

您可以将直方图保存到变量中并查看它.

You can save your histogram to a variable and take a look at it.

x=rnorm(1000)
h<-hist(x)

h

$breaks
 [1] -3.5 -3.0 -2.5 -2.0 -1.5 -1.0 -0.5  0.0  0.5  1.0  1.5  2.0  2.5  3.0  3.5  4.0

$counts
 [1]   2   8  24  42  87 169 188 189 146  78  38  23   5   0   1

$density
 [1] 0.004 0.016 0.048 0.084 0.174 0.338 0.376 0.378 0.292 0.156 0.076 0.046 0.010 0.000 0.002

$mids
 [1] -3.25 -2.75 -2.25 -1.75 -1.25 -0.75 -0.25  0.25  0.75  1.25  1.75  2.25  2.75  3.25  3.75

$xname
[1] "x"

$equidist
[1] TRUE

attr(,"class")
[1] "histogram"

默认情况下,它绘制频率(可以通过h $ counts访问),它只是每个间隔内获得的点数.总点数等于向量的长度,您可以使用

By default it plots frequency (can be accessed via h$counts), which is just the number of points that get within each interval. Total amount of points is equal to the length of the vector, which you can check with

sum(h$counts)
[1] 1000

如果指定probability=TRUE,它将绘制每个点在每个间隔内的概率.概率总和乘以条形宽度应等于1.在我们的示例中,条形宽度为0.5,因此我们得到

If you specify probability=TRUE, it will plot the probability of each point getting within each interval. Total sum of probabilities times the width of the bar should be equal to 1. In our case, bar width is 0.5, so we get

sum(h$density*0.5)
[1] 1

这篇关于hist()中的密度/频率和概率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆