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

From: Rich FitzJohn <rich.fitzjohn_at_gmail.com>
Date: Thu 05 May 2005 - 09:12:55 EST

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, to=lim) dy <- density(y, from=lim, to=lim)

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@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@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
>

```--
Rich FitzJohn