[R] mixed normal distriburtion

Duncan Murdoch murdoch at stats.uwo.ca
Fri Aug 14 17:36:44 CEST 2009


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.




More information about the R-help mailing list