# [R] Comparing distributions

Joris Meys jorismeys at gmail.com
Wed Jun 23 23:38:03 CEST 2010

```A qqplot would indeed help. ?ks.test for more formal testing, but be
aware: You should also think about what you call similar
distributions. See following example :

set.seed(12345)
x1 <- c(rnorm(100),rnorm(150,3.3,0.7))
x2 <- c(rnorm(140,1,1.2),rnorm(110,3.3,0.6))
x3 <- c(rnorm(140,2,1.2),rnorm(110,4.3,0.6))
d1 <-density(x1)
d2 <- density(x2)
d3 <- density(x3)

xlim <- 1.2*c(min(x1,x2,x3),max(x1,x2,x3))
ylim <- 1.2*c(0,max(d1\$y,d2\$y,d3\$y))

op <- par(mfrow=c(1,3))
plot(d1,xlim=xlim,ylim=ylim)
lines(d2,col="red")
lines(d3,col="blue")
qqplot(x1,x2)
qqplot(x2,x3)
par(op)

# formal testing
ks.test(x1,x2)
ks.test(x2,x3)

# relocate x3
x3b <- x3 - mean(x3-x2)
x3c <- x3 - mean(x3-x1)

# formal testing
ks.test(x2,x3b)
ks.test(x1,x3c)

# test location
t.test(x2-x1)
t.test(x3-x2)
t.test(x3-x1)

Cheers
Joris

On Wed, Jun 23, 2010 at 9:33 PM, Ralf B <ralf.bierig at gmail.com> wrote:
> I am trying to do something in R and would appreciate a push into the
> right direction. I hope some of you experts can help.
>
> I have two distributions obtrained from 10000 datapoints each (about
> 10000 datapoints each, non-normal with multi-model shape (when
> eye-balling densities) but other then that I know little about its
> distribution). When plotting the two distributions together I can see
> that the two densities are alike with a certain distance to each other
> (e.g. 50 units on the X axis). I tried to plot a simplified picture of
> the density plot below:
>
>
>
>
> |
> |                                                         *
> |                                                      *     *
> |                                                   *    +   *
> |                                              *     +     +  *
> |                     *        +           *   +            +  *
> |                 *        +*     +   *  +                   + *
> |              *       +       *     +                           +*
> |           *       +                                               +*
> |        *       +                                                    +*
> |     *      +                                                          + *
> |  *      +                                                               + *
> |___________________________________________________________________
>
>
> What I would like to do is to formally test their similarity or
> otherwise measure it more reliably than just showing and discussing a
> plot. Is there a general approach other then using a Mann-Whitney test
> which is very strict and seems to assume a perfect match. Is there a
> test that takes in a certain 'band' (e.g. 50,100, 150 units on X) or
> are there any other similarity measures that could give me a statistic
> about how close these two distributions are to each other ? All I can
> say from eye-balling is that they seem to follow each other and it
> appears that one distribution is shifted by a amount from the other.
> Any ideas?
>
> Ralf
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> and provide commented, minimal, self-contained, reproducible code.
>

--
Joris Meys
Statistical consultant

Ghent University
Faculty of Bioscience Engineering
Department of Applied mathematics, biometrics and process control

tel : +32 9 264 59 87
Joris.Meys at Ugent.be
-------------------------------
Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php

```