How I can generate a random sample from a mixed norml distribution? (i.e. a mixed normal distribution in the form f(x)=.6*N(1,5)+.4*N(2,3) in which N(A,B) is a normal distribution with mean A and variance B.) -- Free e-mail accounts at http://zworg.com
On 8/14/2009 10:31 AM, mahdiyeh wrote:> How I can generate a random sample from a mixed norml distribution? > (i.e. a mixed normal distribution in the form f(x)=.6*N(1,5)+.4*N(2,3) > in which N(A,B) is a normal distribution with mean A and variance B.)Set up the means and SDs as constant vectors: means <- c(1,2) sds <- sqrt(c(5,3)) Select indicators of which version to use: n <- 100 # sample size ind <- sample(1:2, n, replace=TRUE, prob=c(0.6, 0.4)) Now get your samples: x <- rnorm(n, mean=means[ind], sd=sds[ind]) Duncan Murdoch> > -- > Free e-mail accounts at http://zworg.com > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
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!) -- Free e-mail accounts at http://zworg.com
Seemingly Similar Threads
- Help with generating data from a 'not quite' Normal distriburtion
- migration 1.2.5 -> 2.0.5 load increased dramatically
- How to estimate the residual SD for each sample separately in mixed-effects model?
- options in 'rnorm' to set the lower bound of normal distribution to 0 ?
- enableJIT() and internal R completions (was: [ESS-bugs] ess-mode 12.03; ess hangs emacs)