Skip to contents

Simulate a two-component mixture model following the probability density function (pdf) l such that l = p*f + (1-p)*g, with f and g the mixture component distributions, and p the mixing weight.

Usage

twoComp_mixt(
  n = 1000,
  weight = 0.5,
  comp.dist = list("norm", "norm"),
  comp.param = list(c(mean = 0, sd = 1), c(mean = 2, sd = 1))
)

Arguments

n

Number of observations to be simulated.

weight

Weight of the first component distribution (distribution f) in the mixture.

comp.dist

A list of two elements corresponding to the component distributions (specified with R native names) involved in the mixture model. These elements respectively refer to the two component distributions f and g.

comp.param

A list of two elements corresponding to the parameters of the component distributions, each element being a list itself. The names used in each list must correspond to the native R argument names for these distributions. These elements respectively refer to the parameters of f and g distributions of the mixture model.

Value

An object of class 'twoComp_mixt', containing eight attributes: 1) the number of simulated observations, 2) the simulated mixture data, 3) the support of the distributions, 4) the name of the component distributions, 5) the name of the parameters of the component distributions and their values, 6) the mixing proportion, 7) the observations coming from the first component, 8) the observations coming from the second component.

Author

Xavier Milhaud xavier.milhaud.research@gmail.com

Examples

## Mixture of continuous random variables:
sim.X <- twoComp_mixt(n = 2000, weight = 0.5, comp.dist = list("norm", "norm"),
                      comp.param = list(list("mean"=3, "sd"=0.5), list("mean"=0, "sd"=1)))
sim.Y <- twoComp_mixt(n = 1200, weight = 0.7, comp.dist = list("norm", "exp"),
                      comp.param = list(list("mean"=-3, "sd"=0.5), list("rate"=1)))
print(sim.X)
#> 
#> Call:twoComp_mixt(n = 2000, weight = 0.5, comp.dist = list("norm", 
#>     "norm"), comp.param = list(list(mean = 3, sd = 0.5), list(mean = 0, 
#>     sd = 1)))
#> 
#> Component distributions:  norm norm 
#> Support of the component distributions:  Continuous Continuous 
#> 
#> Value of the component parameters: 
#> mean   sd mean   sd 
#>  3.0  0.5  0.0  1.0 
#> 
#> Mixing proportion: 0.5 
#> 
#> Number of observations:  2000 
#> 
#> Simulated data (first 10 obs.): 
#>  -0.7150779 2.546695 2.660536 -1.164181 -1.191731 0.4044483 0.8294714 0.4137783 -0.1398042 0.1491471 
#> 
#> Simulated observations coming from the 1st component (first 10 obs.): 
#>  2.546695 2.660536 2.041057 4.031235 2.845054 2.404258 3.055581 3.433731 2.409525 4.470783 
#> 
#> Simulated observations coming from the 2nd component (first 10 obs.): 
#>  -0.7150779 -1.164181 -1.191731 0.4044483 0.8294714 0.4137783 -0.1398042 0.1491471 1.086495 -0.2689515 
#> 
print(sim.Y)
#> 
#> Call:twoComp_mixt(n = 1200, weight = 0.7, comp.dist = list("norm", 
#>     "exp"), comp.param = list(list(mean = -3, sd = 0.5), list(rate = 1)))
#> 
#> Component distributions:  norm exp 
#> Support of the component distributions:  Continuous Continuous 
#> 
#> Value of the component parameters: 
#> mean   sd rate 
#> -3.0  0.5  1.0 
#> 
#> Mixing proportion: 0.7 
#> 
#> Number of observations:  1200 
#> 
#> Simulated data (first 10 obs.): 
#>  0.2506613 -2.383249 -3.093249 -3.205463 -3.073667 -3.243673 1.147312 0.2987454 -2.750992 -3.13395 
#> 
#> Simulated observations coming from the 1st component (first 10 obs.): 
#>  -2.383249 -3.093249 -3.205463 -3.073667 -3.243673 -2.750992 -3.13395 -3.452523 -3.053947 -2.485374 
#> 
#> Simulated observations coming from the 2nd component (first 10 obs.): 
#>  0.2506613 1.147312 0.2987454 0.3306957 1.344798 0.4035772 1.730138 6.317996 2.581977 0.05541038 
#> 
plot(sim.X, xlim=c(-5,5), ylim=c(0,0.5))
plot(sim.Y, add.plot = TRUE, xlim=c(-5,5), ylim=c(0,0.5), col = "red")


## Mixture of discrete random variables:
sim.X <- twoComp_mixt(n = 2000, weight = 0.5, comp.dist = list("multinom", "multinom"),
                      comp.param = list(list("size"=1, "prob"=c(0.3,0.4,0.3)),
                      list("size"=1, "prob"=c(0.1,0.2,0.7))))
print(sim.X)
#> 
#> Call:twoComp_mixt(n = 2000, weight = 0.5, comp.dist = list("multinom", 
#>     "multinom"), comp.param = list(list(size = 1, prob = c(0.3, 
#>     0.4, 0.3)), list(size = 1, prob = c(0.1, 0.2, 0.7))))
#> 
#> Component distributions:  multinom multinom 
#> Support of the component distributions:  Discrete Discrete 
#> 
#> Value of the component parameters: 
#>  size prob1 prob2 prob3  size prob1 prob2 prob3 
#>   1.0   0.3   0.4   0.3   1.0   0.1   0.2   0.7 
#> 
#> Mixing proportion: 0.5 
#> 
#> Number of observations:  2000 
#> 
#> Obtained multinomial mixture distribution: 
#>  400 579 1021 
#> 
plot(sim.X)