# [R] intersection of 2 density curves

David Winsemius dwinsemius at comcast.net
Thu Oct 8 17:16:22 CEST 2009

```It should also be remembered that there will often be multiple
intersections of such density estimates.

I hope this is not being done in support of a data-driven
discretization of two group comparisons. Such practices are to be
eschewed (as they are even worse than obfuscation).

--
David Winsemius.

On Oct 8, 2009, at 10:01 AM, Rainer M Krug wrote:

> On Thu, Oct 8, 2009 at 3:48 PM, Monica Pisica
> <pisicandru at hotmail.com>wrote:
>
>>
>>
>> Hi,
>>
>> I would like to find out the coordinates of the intersection points
>> of 2
>> density curves. I did a search but i didn't get any significant
>> results. I
>> really hope some of you have some ideas. here it is an example:
>>
>> set.seed(123)
>> x1 <- rnorm(100, 1, 1)
>> x2 <- rnorm(100, 0, 1)
>> d1 <- density(x1)
>> d2 <- density(x2)
>>
>> plot(d1, col = 2)
>> lines(d2, col = 4)
>>
>>
> if you specify n, from ant to in density() and keep them the same,
> then the
> density estimates are done for the same x-coordinates. Therefore you
> can
> compare d1\$y with d2\$y to deternine, where they intersect:
>
> d2 <- density(x2, from=-3, to=5, n=2048)
> d1 <- density(x1, from=-3, to=5, n=2048)
> plot(d1, col = 2)
> lines(d2, col = 4)
> d1\$y < d2\$y
> intersX <- d1\$x[as.logical(abs(diff(d1\$y < d2\$y)))]
> intersY <- d1\$y[as.logical(abs(diff(d1\$y < d2\$y)))]
> abline(v=inters)
> abline(h=intersY)
>
> Cheers,
>
> Rainer
>
>
>> Now i would like to know the coordinates of the intersection point.
>> I did
>> some "trial and error" and i come up with these .... (that means i
>> did
>> several vertical and horizontal lines through the graph until
>> visually i
>> "hit" the intersection point)
>>
>> abline(v = 0.35, lty = 2)
>> abline(h = 0.34, lty = 2)
>>
>> I really appreciate any hints you may have, thanks so much,
>>
>> Monica
>> _________________________________________________________________
>> Your E-mail and More On-the-Go. Get Windows Live Hotmail Free.
>> http://clk.atdmt.com/GBL/go/171222985/direct/01/
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>
>
> --
> Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation
> Biology,
> UCT), Dipl. Phys. (Germany)
>
> Centre of Excellence for Invasion Biology
> Natural Sciences Building
> Office Suite 2039
> Stellenbosch University
> Main Campus, Merriman Avenue
> Stellenbosch
> South Africa
>
> Cell:           +27 - (0)83 9479 042
> Fax:            +27 - (0)86 516 2782
> Fax:            +49 - (0)721 151 334 888
> email:          Rainer at krugs.de
>
> Skype:          RMkrug
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help