0

I was using the package HistogramTools in R to create Histogram.

> H<-hist(1:100)
> sum(H$density)
[1] 0.1

Somehow the sum of the density is not 1, regardless of the data I use (I have tried several generated random data as well, and it is also not 1)

but then,

> sum(H$counts)
[1] 100

the total counts match the total number of data.

Am I wrong in understanding "density" or "relative frequency"??

mah
  • 1
  • 3
  • You would find it informative to plot `H`. – whuber Sep 23 '19 at 19:54
  • Pretty sure this is essentially a duplicate. Its sort of covered in Silverfish's answer [here](https://stats.stackexchange.com/a/133370/805) but I think there's an answer (maybe from whuber) elsewhere that's more direct. I just don't seem to be turning it up right now. – Glen_b Sep 24 '19 at 03:21

1 Answers1

2

$density property of an histogram object gives you the y-axis values of each bin. In order to get an area of $1$, you'll have to multiply it with the bin width. In your case, number of bins is 10 by default, which gives you a bin width of 10. Multiplying it with H$density gives $1$. Try the following, which will give 0.2 when executed (here bin width will be 5):

H<-hist(1:100, 20)
sum(H$density)
gunes
  • 49,700
  • 3
  • 39
  • 75