Learn R Programming

SynthesizerPlus (version 0.4.0)

r_mvdist: Generate data from a copula with arbitrary marginal distributions

Description

r_mvdist() combines a copula (see r_copula()) with user-specified marginal distributions, returning a data frame. This is a flexible way to simulate mixed-type data sets with known dependence, e.g. for simulation studies or to benchmark synthesizers.

Each element of margins is either

  • a quantile function function(p) (e.g. from margin_categorical() or margin_empirical()),

  • a character string naming a distribution with a q* function, e.g. "norm", "gamma", "pois", or

  • a list with element dist and further parameters, e.g. list(dist = "gamma", shape = 2, rate = 1).

Usage

r_mvdist(n, margins, copula = "gaussian", ..., seed = NULL)

Value

A data frame with one column per margin.

Arguments

n

Number of rows.

margins

A (named) list of marginal specifications.

copula

Copula family, see r_copula().

...

Further arguments passed to r_copula() (corr, df, theta).

seed

Optional random seed.

Examples

Run this code
df <- r_mvdist(
  500,
  margins = list(
    income = list(dist = "lnorm", meanlog = 10, sdlog = 0.5),
    age = list(dist = "unif", min = 18, max = 80),
    sector = margin_categorical(c("agri", "industry", "services"),
                                c(0.1, 0.3, 0.6))
  ),
  copula = "gaussian", corr = make_corr(3, "exchangeable", 0.4), seed = 1
)
str(df)

Run the code above in your browser using DataLab