Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

rmixture

Simulation from Discrete Mixtures


Description

Generate random variates from a discrete mixture of distributions.

Usage

rmixture(n, probs, models, shuffle = TRUE)

Arguments

n

number of random variates to generate. If length(n) > 1, the length is taken to be the number required.

probs

numeric non-negative vector specifying the probability for each model; is internally normalized to sum 1. Infinite and missing values are not allowed. Values are recycled as necessary to match the length of models.

models

vector of expressions specifying the simulation models with the number of variates omitted (see details). Models are recycled as necessary to match the length of probs.

shuffle

logical; should the random variates from the distributions be shuffled?

Details

rmixture generates variates from a discrete mixture, that is random variable with a probability density function of the form

f(x) = p_1 f_1(x) + ... + p_n f_n(x),

where f_1, …, f_n are densities and p_1 + … + p_n = 1.

The values in probs will be internally normalized to be used as probabilities p_1 + … + p_n.

The specification of simulation models uses the syntax of rcomphierarc. Models f_1, …, f_n are expressed in a semi-symbolic fashion using an object of mode expression where each element is a complete call to a random number generation function, with the number of variates omitted.

The argument of the random number generation functions for the number of variates to simulate must be named n.

If shuffle is FALSE, the output vector contains all the random variates from the first model, then all the random variates from the second model, and so on. If the order of the variates is irrelevant, this cuts the time to generate the variates roughly in half.

Value

A vector of random variates from the mixture with density f(x).

Author(s)

See Also

rcompound to simulate from compound models.

rcomphierarc to simulate from compound hierarchical models.

Examples

## 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)))

actuar

Actuarial Functions and Heavy Tailed Distributions

v3.1-2
GPL (>= 2)
Authors
Vincent Goulet [cre, aut], Sébastien Auclair [ctb], Christophe Dutang [aut], Nicholas Langevin [ctb], Xavier Milhaud [ctb], Tommy Ouellet [ctb], Alexandre Parent [ctb], Mathieu Pigeon [aut], Louis-Philippe Pouliot [ctb], Jeffrey A. Ryan [aut] (Package API), Robert Gentleman [aut] (Parts of the R to C interface), Ross Ihaka [aut] (Parts of the R to C interface), R Core Team [aut] (Parts of the R to C interface), R Foundation [aut] (Parts of the R to C interface)
Initial release
2021-03-30

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.