Re: [R] two densities with same stepsize

From: Duncan Murdoch <>
Date: Tue, 05 Feb 2008 06:39:14 -0500

On 05/02/2008 3:22 AM, Antje wrote:
> Hi there,
> I have two series of data. plotting the density function of both gives me an
> idea about the difference of the data. But I would like to quantify the
> difference I see.
> a <- rnorm(100)
> b <- rnorm(100)
> da <- density(a)
> db <- density(b)
> The problem is that da$x and db$x are different and so I have difficulties to
> compare them... Is there any way to force the density funtion to produce the
> values for the same x-steps?

If you specify n= and from= and to= to be the same, I'd expect that you'd get the same steps.

da <- density(a, n=512, from=min(c(a,b)), to=max(c(a,b))) db <- density(b, n=512, from=min(c(a,b)), to=max(c(a,b)))

You could also use approxfun or splinefun to convert the density() output into a function that could be evaluated anywhere.

> Or is there any other statistical approach I should use for comparing two
> density functions? I need to quantify how much the data-series differ (density
> function is rather complex in my case with skew and several maximas, so not
> easily to describe as a mathematical function.
> Sorry, I'm not that deep into statistics. Any comments / keywords on this is
> welcome...

There are a lot of measures of the difference between two distributions.   Which one to use depends a lot on the intended purpose.

Duncan Murdoch mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Tue 05 Feb 2008 - 11:42:27 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 05 Feb 2008 - 12:30:12 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive