Hi all, A friend send me a question on why does this: x<-rpois(100,1) sum( hist(x)$density ) Gives out "2" I tried this: sum( hist(x, freq =T)$density ) It didn't help. Then he came back with the following insight: # with breaks b<-c(0,0.9,1:8) sum(hist(x,breaks=b)$density) # Much more then 2 # but if we add weights according to the interval length sum(hist(x,breaks=b)$density * diff(b)) # it works What do you think ? Tal ----------------Contact Details:------------------------------------------------------- Contact me: Tal.Galili@gmail.com | 972-52-7275845 Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | www.r-statistics.com (English) ---------------------------------------------------------------------------------------------- [[alternative HTML version deleted]]
David Winsemius
2010-Mar-13 20:59 UTC
[R] Is this a bug (or a feature) in hist(x)$density ??
On Mar 13, 2010, at 3:39 PM, Tal Galili wrote:> Hi all, > > A friend send me a question on why does this: > > x<-rpois(100,1) > sum( hist(x)$density ) > > Gives out "2"Yes. And... > hist(x)$breaks [1] 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0> > I tried this: > > sum( hist(x, freq =T)$density ) > > It didn't help. > > Then he came back with the following insight: > > # with breaks > b<-c(0,0.9,1:8) > sum(hist(x,breaks=b)$density) # Much more then 2 > # but if we add weights according to the interval length > sum(hist(x,breaks=b)$density * diff(b)) > # it works > > > > What do you think ? >I do think that neither you nor your friend understand densities. -- David. David Winsemius, MD West Hartford, CT
Stephan Kolassa
2010-Mar-13 21:04 UTC
[R] Is this a bug (or a feature) in hist(x)$density ??
Hi Tal, basically, by summing over the (pointwise) density, you are "approximating" the integral over the density (which should be around 1) - but to really do a rectangular approximation, you will of course need to multiply each function value by the width of the corresponding rectangle. I'd recommend reading through Wikipedia's article on the Riemann integral, you may be enlightened. http://en.wikipedia.org/wiki/Riemann_integral HTH, Stephan Tal Galili schrieb:> Hi all, > > A friend send me a question on why does this: > > x<-rpois(100,1) > sum( hist(x)$density ) > > Gives out "2" > > I tried this: > > sum( hist(x, freq =T)$density ) > > It didn't help. > > Then he came back with the following insight: > > # with breaks > b<-c(0,0.9,1:8) > sum(hist(x,breaks=b)$density) # Much more then 2 > # but if we add weights according to the interval length > sum(hist(x,breaks=b)$density * diff(b)) > # it works > > > > What do you think ? > > > Tal > > > > ----------------Contact > Details:------------------------------------------------------- > Contact me: Tal.Galili at gmail.com | 972-52-7275845 > Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | > www.r-statistics.com (English) > ---------------------------------------------------------------------------------------------- > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > 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. >