## Mixture of two exponentials (with means 1/3 and 1/7) with equal
## probabilities.
rmixture(10, 0.5, expression(rexp(3), rexp(7)))
rmixture(10, 42, expression(rexp(3), rexp(7))) # same
## Mixture of two lognormals with different probabilities.
rmixture(10, probs = c(0.55, 0.45),
models = expression(rlnorm(3.6, 0.6),
rlnorm(4.6, 0.3)))
## Building the model expressions in the following example
## works as 'rate' is defined in the parent frame of
## 'rmixture'.
probs <- c(2, 5)
g <- function(n, p, rate)
rmixture(n, p, expression(rexp(rate[1]), rexp(rate[2])))
g(10, probs, c(3, 7))
## The following example does not work: 'rate' does not exist
## in the evaluation frame of 'rmixture'.
f <- function(n, p, model) rmixture(n, p, model)
h <- function(n, p, rate)
f(n, p, expression(rexp(rate[1]), rexp(rate[2])))
if (FALSE) h(10, probs, c(3, 7))
## Fix: substitute the values in the model expressions.
h <- function(n, p, rate)
{
models <- eval(substitute(expression(rexp(a[1]), rexp(a[2])),
list(a = rate)))
f(n, p, models)
}
h(10, probs, c(3, 7))
Run the code above in your browser using DataLab