Re: [R] Question about density()

From: Gabor Grothendieck <ggrothendieck_at_myway.com>
Date: Fri 04 Mar 2005 - 13:11:46 EST

Dan Bolser <dmb <at> mrc-dunn.cam.ac.uk> writes:

:
: If I integrate over the result of the density() funcion, is the result 1?
:
: For example
:
: x <- rnorm(1000)
: plot(density(x))
:
: Does the area under the curve I see sum to 1?
:
: What I really want to know is if I can directly compare two particular
: curves, generated like this
:
: x <- rnorm(1000)
: plot(density(x))
:
: a.seq <- seq(min(x),max(x),0.01)
: lines(a.seq,dnorm(x=a.seq,mean=mean(x),sd=sd(x)))
:
: I guess they are directly comparable if the area under density sums to
: one.
:
: Is this correct?

We can integrate it to find out:

R> set.seed(1)
R> z <- density(rnorm(10000))
R> f <- approxfun(z$x, z$y, yleft = 0, yright = 0)
R> integrate(f, -Inf, Inf)

1.000978 with absolute error < 6.6e-05

R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Fri Mar 04 13:22:34 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:30:40 EST