Learn R Programming

MixtureInf (version 1.0-1)

rnormmix: Random sample from a mixture of normals

Description

Generate a random sample for a mixture of univariate normal distributions.

Usage

rnormmix(n, alpha, mean, sd = rep(1, length(alpha)))

Arguments

n
number of samples
alpha
vector of mixture probabilities, with length equal to $m$, the desired number of components.
mean
vector of means of each component.
sd
vector of standard deviations of each component.

Value

  • Returns a vector of length $n$ of samples from an $m$-component mixture of univariate normal distributions.

See Also

Functions for mixtures of other standard distributions, such as rbinommix for the binomial and rexpmix for the exponential distribution.

Examples

Run this code
# Draw random samples from a mixture of two equally likely normal
# distributions, one with mean = -2, s.d. = 1, and the other with
# mean=2, s.d. = 4
x <- rnormmix(200, c(0.5, 0.5), c(-2, 2), c(1, 4))
hist(x, probability=TRUE)
lines(density(x), col='red')

Run the code above in your browser using DataLab