Skip to content
Prev 69166 / 398503 Next

Questions about the intersection area under two kernel densities

Hi,

A "density" object's x and y values can be accessed using $x and $y
(this is in ?density, under "Value:")

obj <- density(rnorm(100, 0, 1))
obj$x
obj$y

You may find it useful to force the "from" and "to" arguments to be
the same for the two calls to density(), if you want to work directly
from the x and y values.

x <- rnorm(100, 0, 1)
y <- rnorm(100, .2, 1)
dx <- density(x)
dy <- density(y)

## Re-do the densities so they have the same x-values:
lim <- range(dx$x, dy$x)
dx <- density(x, from=lim[1], to=lim[2])
dy <- density(y, from=lim[1], to=lim[2])

plot(dx, col="blue", ylim=range(dx$y, dy$y))
lines(dy, col="red")
polygon(dx$x, pmin(dx$y, dy$y), col="purple")

Cheers,
Rich
On 5/5/05, Grace Clinton <gcll688 at yahoo.com> wrote: