[R] Mixture of Distributions

(Ted Harding) Ted.Harding at manchester.ac.uk
Thu Feb 21 22:39:56 CET 2008


On 21-Feb-08 20:58:25, Evgenia wrote:
> 
> Dear R users, 
> I would like to sample from a mixture distribution
> p1*f1+p2*f2+p3*f3 with f1,f2,f3 three different forms
> of distributions. I know that in the case of two
> distributions I have to sample the mixture compoment
> membership.
> 
> How can I weight my three distributions with their
> respective probabilities?
> 
> Evgenia

There are several ways in which you could write code
to do this, but all amount to the fact that each value
sampled from such a mixture is obtained by
a) choose between f1, f2, f3 at random according to the
   probabilities p1, p2, p3 (it is assumed p1+p2+p3=1).
b) sample 1 value from whichever of f1, f2, f3 was chosen.

Suggestion:

Suppose the functions rf1(n), rf2(n), rf3(n) respectively
sample n values from f1, f2 and f3.

S0 <- cbind(rf1(n),rf2(n),rf3(n))
ix <- sample(c(1,2,3),n,3,prob=c(p1,p2,p3),replace=TRUE)

S  <- S0[,ix]

will produce n values, each of which is a sample of size 1
from the mixture.

Example:
rf1 <- function(n){rnorm(n,0,1)} ## normal distribution with mean=0
rf2 <- function(n){rnorm(n,4,1)} ## normal distribution with mean=4
rf3 <- function(n){rnorm(n,9,1)} ## normal distribution with mean=9

p1 <- 0.2; p2 <- 0.3; p3 <-0.5
S0 <- cbind(rf1(500),rf2(500),rf3(500))
ix <- sample(c(1,2,3),500,prob=c(p1,p2,p3),replace=TRUE)
S  <- S0[,ix]
hist(S,breaks=50)

Hoping this helps.
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 21-Feb-08                                       Time: 21:39:53
------------------------------ XFMail ------------------------------



More information about the R-help mailing list