# [R] How to calculate the distance between two density functions

David Winsemius dwinsemius at comcast.net
Fri Dec 5 18:13:52 CET 2008

```A similar question was posed and answered:

http://finzi.psych.upenn.edu/R/Rhelp02a/archive/119793.html

Two aspects needed to be addressed ... specifying the same domain, and
getting the x-values to "line up" prior to the subtraction (or
whatever function is desired).

What are you going to do when the two functions cross?

d1 <- dnorm(seq(-2,2,by=.1))
d2 <- dnorm(seq(-2,2,by=.1), mean=2)
plot(seq(-2,2,by=.1),d1)
lines(seq(-2,2,by=.1),d2)

---- or----

d4 <- dnorm(seq(-4,4,by=.1))
d5 <- dnorm(seq(-4,4,by=.1), sd=5)
plot(seq(-4,4,by=.1),d4)
lines(seq(-4,4,by=.1),d5)

--
David Winsemius
On Dec 5, 2008, at 8:59 AM, Chang Jia-Ming wrote:

> Dear all,
>
>  I wrote the following code to calculate the density functions for
> two data
> sets, respectively.
>
>  den_str <-density(str_data\$Similarity);
>  den_non_str <-density(nonstr_data\$Similarity);
>
>  However, I would like to knowing the difference between den_str and
> den_non_str, that is, the difference between the region under the
> curve of
> the den_str and the region under the curve of the den_non_str.
>
> How to do?
>
> Thank you for help.
>
> Jia-Ming
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help