[R] mixed normal distriburtion

Richard.Cotton at hsl.gov.uk Richard.Cotton at hsl.gov.uk
Tue Aug 25 14:55:26 CEST 2009

> I'm trying to draw the density function of a mixed normal distribution
> in the form of:
> .6*N(.4,.1)+ .4*N(.8,.1)
> At first I generate a random sample with size 200 by the below code:
> means = c(.4,.8)
> sds = sqrt(c(.1,.1))
> ind = sample(1:2, n, replace=TRUE, prob=c(.6,.4))
> x=rnorm(n,mean=means[ind],sd=sds[ind])
> Then I use the below code for drawing the graph:
> plot(density(x))
> The plot doesn't seem to be belonging to the desired distribution,
> because there is just one mode in it (I've seen the real graph of this
> mixed normal in a paper, it has two clear distinct modes). Even the
> hist() doesn't draw a plot similar to the real graph. I think the
> generation code isn't correct. Is it? (I've asked the generation code
> here!)

The code is fine - the reason you can't see two peaks is that the two 
distributions overlap a lot.  Set 
means <- c(.4, 10) 
to see double peaks more clearly.


Mathematical Sciences Unit


This message contains privileged and confidential inform...{{dropped:22}}

More information about the R-help mailing list