Simulate from a mixture of univariate normal distributions.
Usage
rnormmix(n, lambda=1, mu=0, sigma=1)
Arguments
- n
Number of cases to simulate.
- lambda
Vector of mixture probabilities, with length equal to \(m\),
the desired number of components (subpopulations). This is assumed to sum
to 1; if not, it is normalized.
- mu
Vector of means.
- sigma
Vector of standard deviations.
Value
rnormmix returns an \(n\)-vector sampled from an \(m\)-component
mixture of univariate normal distributions.
Examples
##Generate data from a 2-component mixture of normals.
set.seed(100)
n <- 500
lambda <- rep(1, 2)/2
mu <- c(0, 5)
sigma <- rep(1, 2)
mixnorm.data <- rnormmix(n, lambda, mu, sigma)
##A histogram of the simulated data.
hist(mixnorm.data)