# [R] Questions about the intersection area under two kernel densities

Rich FitzJohn rich.fitzjohn at gmail.com
Thu May 5 01:12:55 CEST 2005

```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:
> Hi there,
>
> I am working on a project which needs the value of the interaction area under two distributions( eatimated by kernel density estimators).
>
> For example:
>
> x<-rnorm(100,0,1)
> y<-rnorm(100,0.2,1)
> density(x) # This produces the summary of dependent variable and independent variable.
>
> How can I get the individual values of variables and reform a curve to calculate the area under curve?
>
> Is there a way to get aroud this and give me the solution?
>
> Thanks a lot.
>
> Grace
>
> ---------------------------------
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help