[R] R hist density wrong?

Duncan Murdoch murdoch.duncan at gmail.com
Wed Nov 18 09:57:31 CET 2015


On 18/11/2015 3:49 AM, Luca Cerone wrote:
> Dear all,
> this is probably a very naive question but I can't understand what
> hist() means by density.
>
> A very simple example:
>
> h <- hist(c(1,1,2,3), plot=F)
>
> h$counts
> [1] 2 1 0 1
>
> h$density
> [1] 1.0 0.5 0.0 0.5
>
> The counts are as I expect, but density is quite puzzling for me.
>
> I would have expected to obtain the probability of that bin (i.e. 0.5,
> 0.25, 0, 0.25),
> but I can't understand how those numbers come out.

The bins are 0.5 wide (see h$breaks).  Density has the usual meaning for 
continuous distributions:  probability per unit.  So a density of 1 per 
unit over a distance of 0.5 gives a probability of 0.5.
>
> Sometimes sum(h$density) is equal to 1 as I would expect, though.

sum(h$density) would rarely make sense to calculate, any more than the 
sum of the normal density function at 4 points would.  You want to 
integrate a density.  The formula for that is 
sum(h$density*diff(h$breaks)).

Duncan Murdoch
>
> What am I misunderstanding here?
>
> Thanks a lot for the help!
>
> Cheers,
> Luca
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list