Skip to content
Prev 163884 / 398506 Next

How to calculate the distance between two density functions

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)