[R] density() integrates to 1?

(Ted Harding) Ted.Harding at nessie.mcc.ac.uk
Thu Sep 25 00:15:15 CEST 2003


On 24-Sep-03 Ross Boylan wrote:
> Visual inspection of the plot of a density() function vs a normal with
> the same mean and variance suggests the area under the density curve is
> bigger than under the normal curve.  The two curves are very close over
> most of the domain.  Assuming the normal curve does integrate to 1,
> this
> implies the area under density() is > 1.
> 
> Is there any assurance that the density kernel smoother produces
> something that integrates to 1?  Or am I seeing things?
> 
> I suppose an additional complexity is that density() produces discrete
> output, but then I'm looking at the continuous curve plot produced.

It should integrate to 1 (see help for density), and sum to something
very close to 1 depending on the number of points ("n=...") at which
density is evaluated.

Example:
> X<-rnorm(1000)
> Y<-density(X)   # n = 512 (default)
> x<-Y$x; y<-Y$y;
> k<-length(x);d<-min(x[2:k]-x[1:k-1]);
> sum(y*d)
[1] 1.000975
> Y<-density(X,n=2000)
> x<-Y$x; y<-Y$y;
> k<-length(x);d<-min(x[2:k]-x[1:k-1]);
> sum(y*d)
[1] 1.000240
> Y<-density(X,n=100000)
> x<-Y$x; y<-Y$y;
> k<-length(x);d<-min(x[2:k]-x[1:k-1]);
> sum(y*d)
[1] 0.9999996

Ted.


--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 167 1972
Date: 24-Sep-03                                       Time: 23:15:15
------------------------------ XFMail ------------------------------




More information about the R-help mailing list