[Rd] hist.default()$density

Martin Becker martin.becker at mx.uni-saarland.de
Tue Mar 30 17:17:36 CEST 2010

Dear developers,

the current implementation of hist.default() calculates 'density' (and 
'intensities') as
  dens <- counts/(n*h)
where h has been calculated before as
  h <- diff(fuzzybreaks)
which results in 'fuzzy' values for the density, see e.g.

 > tmp <- hist(1:10,breaks=c(-2.5,2.5,7.5,12.5),plot=FALSE)
 > print(tmp$density,digits=15)
[1] 0.0399999920000016 0.1000000000000000 0.0600000000000000

Since hist.default()$breaks are not the fuzzy breaks used for the 
calculation of dens, the sum of the bins' area is significantly 
different from 1 in many cases, see e.g.

 > print(sum(tmp$density*diff(tmp$breaks)),digits=15)
[1] 0.999999960000008

Is this intended, or should the calculation of dens read
  dens <- counts/(n*diff(breaks))
instead (or should hist.default()$breaks return the fuzzy breaks)?

Best wishes

Dr. Martin Becker
Statistics and Econometrics
Saarland University
Campus C3 1, Room 206
66123 Saarbruecken

More information about the R-devel mailing list